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:
2026-04-05 17:11:32 +02:00
parent 25a392df48
commit 63a9cb5273
62 changed files with 5376 additions and 0 deletions
@@ -121,3 +121,72 @@ def linspace(start: float, stop: float, num: int) -> list:
return [start]
step = (stop - start) / (num - 1)
return [start + i * step for i in range(num)]
def estimate_hawkes(arrivals: list[int], max_lag: int = 30) -> dict:
"""Estima parámetros de un proceso Hawkes desde autocorrelación de arrivals.
Ajusta exponencial a*exp(-b*lag) sobre la ACF.
Retorna dict con alpha, beta, branching_ratio, acf.
"""
import numpy as np
from scipy.optimize import curve_fit
arr = np.array(arrivals, dtype=float)
mean_a = np.mean(arr)
var_a = np.var(arr)
if var_a == 0:
return {'alpha': 0.0, 'beta': 1.0, 'branching_ratio': 0.0, 'acf': [1.0]}
acf = [1.0] + [
float(np.mean((arr[lag:] - mean_a) * (arr[:-lag] - mean_a)) / var_a)
for lag in range(1, max_lag)
]
lags = np.arange(1, max_lag)
acf_vals = np.array(acf[1:])
if acf_vals[0] <= 0.01:
return {'alpha': 0.0, 'beta': 1.0, 'branching_ratio': 0.0, 'acf': acf}
exp_decay = lambda x, a, b: a * np.exp(-b * x)
try:
popt, _ = curve_fit(exp_decay, lags, acf_vals, p0=[0.5, 0.5], maxfev=5000)
alpha_est, beta_est = abs(popt[0]), abs(popt[1])
except RuntimeError:
alpha_est, beta_est = 0.0, 1.0
branching = alpha_est / beta_est if beta_est > 0 else 0.0
return {
'alpha': round(alpha_est, 4),
'beta': round(beta_est, 4),
'branching_ratio': round(branching, 4),
'acf': acf,
}
def estimate_pareto_alpha(values: list[float], x_min_percentile: float = 90.0) -> dict:
"""Estima el exponente alpha de una distribución Pareto via MLE.
α = n / Σ ln(xi / x_min) donde x_min es el percentil indicado.
Alpha bajo = cola más pesada = más valores extremos.
"""
import numpy as np
arr = np.array([v for v in values if v > 0], dtype=float)
if len(arr) < 10:
return {'alpha': 0.0, 'x_min': 0.0, 'n_tail': 0}
x_min = float(np.percentile(arr, x_min_percentile))
tail = arr[arr >= x_min]
if len(tail) < 2 or x_min <= 0:
return {'alpha': 0.0, 'x_min': x_min, 'n_tail': len(tail)}
alpha = float(len(tail) / np.sum(np.log(tail / x_min)))
return {
'alpha': round(alpha, 4),
'x_min': round(x_min, 6),
'n_tail': len(tail),
}