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:
@@ -0,0 +1,48 @@
|
||||
---
|
||||
name: avellaneda_stoikov_quotes
|
||||
kind: function
|
||||
lang: py
|
||||
domain: finance
|
||||
version: "1.0.0"
|
||||
purity: pure
|
||||
signature: "avellaneda_stoikov_quotes(mid_price: float, inventory: float, gamma: float, sigma: float, spread_base: float, n_levels: int, qty_base: float) -> list[dict]"
|
||||
description: "Genera ordenes de market maker usando el modelo Avellaneda-Stoikov. Calcula precio de reserva y half spread optimos segun inventario y volatilidad."
|
||||
tags: [simulation, market-making, avellaneda-stoikov, montecarlo, finance, order-book]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: ""
|
||||
imports: []
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "python/functions/finance/finance.py"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```python
|
||||
orders = avellaneda_stoikov_quotes(
|
||||
mid_price=100.0,
|
||||
inventory=0.0,
|
||||
gamma=0.1,
|
||||
sigma=0.02,
|
||||
spread_base=0.5,
|
||||
n_levels=3,
|
||||
qty_base=10.0,
|
||||
)
|
||||
# [
|
||||
# {'side': 'buy', 'price': 99.75, 'qty': 10.0},
|
||||
# {'side': 'sell', 'price': 100.25, 'qty': 10.0},
|
||||
# ...
|
||||
# ]
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Funcion pura — sin aleatoriedad.
|
||||
`gamma` controla la aversion al riesgo de inventario: mayor gamma = spreads mas amplios.
|
||||
`inventory` positivo sesga los quotes hacia venta (reduce inventario largo).
|
||||
Cada nivel adicional ensancha el spread en `half_spread * 0.5` y aumenta la cantidad en `qty_base * 0.5`.
|
||||
Ordenes con precio <= 0 se descartan automaticamente.
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
name: generate_gbm_prices
|
||||
kind: function
|
||||
lang: py
|
||||
domain: finance
|
||||
version: "1.0.0"
|
||||
purity: pure
|
||||
signature: "generate_gbm_prices(initial_price: float, n_ticks: int, sigma: float, mu: float, jump_intensity: float, jump_size_std: float, seed: int) -> list[float]"
|
||||
description: "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)."
|
||||
tags: [simulation, gbm, price, montecarlo, finance, stochastic]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: ""
|
||||
imports: [numpy]
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "python/functions/finance/finance.py"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```python
|
||||
prices = generate_gbm_prices(
|
||||
initial_price=100.0,
|
||||
n_ticks=1000,
|
||||
sigma=0.02,
|
||||
mu=0.0,
|
||||
jump_intensity=0.01,
|
||||
jump_size_std=0.05,
|
||||
seed=42,
|
||||
)
|
||||
# prices[0] == 100.0
|
||||
# len(prices) == 1000
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Funcion pura — el seed fija el resultado deterministicamente.
|
||||
`jump_intensity=0.0` desactiva los saltos (GBM puro).
|
||||
`dt=1.0` por tick (tiempo discreto). Para tiempo continuo, ajustar sigma y mu en consecuencia.
|
||||
Requiere numpy para la generacion de numeros aleatorios y el calculo de exp.
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
name: generate_taker_order
|
||||
kind: function
|
||||
lang: py
|
||||
domain: finance
|
||||
version: "1.0.0"
|
||||
purity: pure
|
||||
signature: "generate_taker_order(alpha: float, size_min: float, size_max: float, buy_prob: float, seed: int | None) -> dict"
|
||||
description: "Genera una market order de taker con tamano distribuido segun power-law (Pareto). Alpha bajo produce ordenes mas grandes (ballenas)."
|
||||
tags: [simulation, taker, power-law, montecarlo, finance, order-book]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: ""
|
||||
imports: [numpy]
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "python/functions/finance/finance.py"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```python
|
||||
order = generate_taker_order(
|
||||
alpha=2.0,
|
||||
size_min=1.0,
|
||||
size_max=100.0,
|
||||
buy_prob=0.5,
|
||||
seed=42,
|
||||
)
|
||||
# {'side': 'buy', 'qty': 3.7}
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Funcion pura cuando se fija seed. Con seed=None el resultado es no deterministico.
|
||||
La distribucion Pareto con alpha=2 modela bien la distribucion empirica de tamaños de ordenes en mercados reales.
|
||||
`size_max` actua como techo (clipping) para evitar ordenes extremas.
|
||||
Retorna dict con keys: `side` ('buy' o 'sell') y `qty` (float redondeado a 1 decimal).
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
name: hawkes_intensity
|
||||
kind: function
|
||||
lang: py
|
||||
domain: finance
|
||||
version: "1.0.0"
|
||||
purity: pure
|
||||
signature: "hawkes_intensity(base_rate: float, hawkes_alpha: float, hawkes_beta: float, event_times: list[float], current_time: float) -> float"
|
||||
description: "Calcula la intensidad lambda(t) de un proceso de Hawkes en el tiempo actual. Modela la autocorrelacion temporal de eventos de mercado (rafagas de ordenes)."
|
||||
tags: [simulation, hawkes, stochastic-process, montecarlo, finance, point-process]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: ""
|
||||
imports: [numpy]
|
||||
tested: false
|
||||
tests: []
|
||||
test_file_path: ""
|
||||
file_path: "python/functions/finance/finance.py"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```python
|
||||
intensity = hawkes_intensity(
|
||||
base_rate=1.0,
|
||||
hawkes_alpha=0.8,
|
||||
hawkes_beta=2.0,
|
||||
event_times=[0.5, 1.2, 1.8],
|
||||
current_time=2.5,
|
||||
)
|
||||
# Intensidad > base_rate por excitacion de eventos pasados
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Funcion pura — determinista dado el mismo historial de eventos.
|
||||
`hawkes_alpha` controla la magnitud del salto de intensidad por evento.
|
||||
`hawkes_beta` controla la velocidad de decaimiento (mayor beta = decaimiento mas rapido).
|
||||
La condicion de estabilidad del proceso es hawkes_alpha < hawkes_beta.
|
||||
Eventos con ti >= current_time se ignoran automaticamente.
|
||||
Retorna max(0.0, ...) para garantizar intensidad no negativa.
|
||||
Reference in New Issue
Block a user