feat: funciones Python datascience, finance, cybersecurity y pipelines
Datascience: aggregate_by_group, deduplicate_entities/relations, detect_drift, diff_entities/relations, extract_entities/relations_llm, hotness_score, melt, merge_graphs, pivot, build_entity/relation_schema_prompt. Finance: avellaneda_stoikov_quotes, generate_gbm_prices, generate_taker_order, hawkes_intensity + módulo finance.py. Cybersecurity: envelope_encrypt/decrypt + módulo cybersecurity.py. Pipelines: extraction_pipeline, monte_carlo_market, run_market_sim. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -135,3 +135,104 @@ def annualized_volatility(returns: list, periods_per_year: float) -> float:
|
||||
mean = sum(returns) / n
|
||||
variance = sum((r - mean) ** 2 for r in returns) / (n - 1)
|
||||
return math.sqrt(variance) * math.sqrt(periods_per_year)
|
||||
|
||||
|
||||
def generate_gbm_prices(
|
||||
initial_price: float,
|
||||
n_ticks: int,
|
||||
sigma: float,
|
||||
mu: float = 0.0,
|
||||
jump_intensity: float = 0.0,
|
||||
jump_size_std: float = 0.05,
|
||||
seed: int = 42,
|
||||
) -> list:
|
||||
"""Genera serie de precios fundamentales con Geometric Brownian Motion + jump-diffusion.
|
||||
|
||||
S(t+1) = S(t) * exp((mu - sigma^2/2)*dt + sigma*sqrt(dt)*Z + J*N)
|
||||
donde Z ~ N(0,1), N ~ Bernoulli(jump_intensity), J ~ N(0, jump_size_std)
|
||||
"""
|
||||
import numpy as np
|
||||
rng = np.random.default_rng(seed)
|
||||
prices = [0.0] * n_ticks
|
||||
prices[0] = initial_price
|
||||
dt = 1.0
|
||||
for t in range(1, n_ticks):
|
||||
z = rng.standard_normal()
|
||||
gbm = (mu - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * z
|
||||
jump = 0.0
|
||||
if jump_intensity > 0 and rng.random() < jump_intensity:
|
||||
jump = rng.normal(0, jump_size_std)
|
||||
prices[t] = prices[t - 1] * np.exp(gbm + jump)
|
||||
return prices
|
||||
|
||||
|
||||
def avellaneda_stoikov_quotes(
|
||||
mid_price: float,
|
||||
inventory: float,
|
||||
gamma: float,
|
||||
sigma: float,
|
||||
spread_base: float,
|
||||
n_levels: int = 3,
|
||||
qty_base: float = 10.0,
|
||||
) -> list:
|
||||
"""Genera ordenes de market maker usando el modelo Avellaneda-Stoikov.
|
||||
|
||||
Precio de reserva: r = mid - inventory * gamma * sigma^2
|
||||
Half spread: delta = spread_base/2 + gamma * sigma^2/2
|
||||
|
||||
Retorna lista de dicts con keys: side, price, qty
|
||||
"""
|
||||
reservation = mid_price - inventory * gamma * sigma**2
|
||||
half_spread = spread_base / 2 + gamma * sigma**2 / 2
|
||||
orders = []
|
||||
for level in range(n_levels):
|
||||
offset = level * half_spread * 0.5
|
||||
qty = qty_base * (1 + level * 0.5)
|
||||
bid_price = round(reservation - half_spread - offset, 2)
|
||||
ask_price = round(reservation + half_spread + offset, 2)
|
||||
if bid_price > 0:
|
||||
orders.append({'side': 'buy', 'price': bid_price, 'qty': qty})
|
||||
if ask_price > 0:
|
||||
orders.append({'side': 'sell', 'price': ask_price, 'qty': qty})
|
||||
return orders
|
||||
|
||||
|
||||
def generate_taker_order(
|
||||
alpha: float = 2.0,
|
||||
size_min: float = 1.0,
|
||||
size_max: float = 100.0,
|
||||
buy_prob: float = 0.5,
|
||||
seed: int | None = None,
|
||||
) -> dict:
|
||||
"""Genera una market order de taker con tamano power-law (Pareto).
|
||||
|
||||
P(size > x) ~ x^(-alpha). Alpha bajo = mas ballenas.
|
||||
Retorna dict con keys: side, qty
|
||||
"""
|
||||
import numpy as np
|
||||
rng = np.random.default_rng(seed)
|
||||
side = 'buy' if rng.random() < buy_prob else 'sell'
|
||||
raw_size = (rng.pareto(alpha) + 1) * size_min
|
||||
size = min(round(raw_size, 1), size_max)
|
||||
return {'side': side, 'qty': size}
|
||||
|
||||
|
||||
def hawkes_intensity(
|
||||
base_rate: float,
|
||||
hawkes_alpha: float,
|
||||
hawkes_beta: float,
|
||||
event_times: list,
|
||||
current_time: float,
|
||||
) -> float:
|
||||
"""Calcula la intensidad lambda(t) de un proceso de Hawkes en el tiempo actual.
|
||||
|
||||
lambda(t) = base_rate + sum(alpha * exp(-beta * (t - ti)))
|
||||
donde ti son los tiempos de eventos pasados.
|
||||
"""
|
||||
import numpy as np
|
||||
excitation = sum(
|
||||
hawkes_alpha * np.exp(-hawkes_beta * (current_time - ti))
|
||||
for ti in event_times
|
||||
if ti < current_time
|
||||
)
|
||||
return max(0.0, base_rate + excitation)
|
||||
|
||||
Reference in New Issue
Block a user