Skip to content

Instantly share code, notes, and snippets.

View al6x's full-sized avatar

Alex Kraft al6x

  • Australia
View GitHub Profile
vol vol_dc k premium
0.008037 1 0.93 0.001134
0.008037 1 0.94 0.001471
0.008037 1 0.95 0.002009
0.008037 1 0.96 0.002952
0.008037 1 0.97 0.004859
0.008037 1 0.98 0.009124
0.008037 1 0.99 0.017501
0.008037 1 1.0 0.027655
0.01041 2 0.93 0.001216
@al6x
al6x / synthetic_prices.py
Created June 25, 2025 07:28
Synthetic Prices
import pandas as pd
import numpy as np
from scipy.integrate import quad
from scipy.stats import norm
import matplotlib.pyplot as plt
import math
def data_stats():
# Distrs as (weights, scales_rel) where scales_rel - relative to the main scale
@al6x
al6x / mean_vs_vol.py
Created May 16, 2025 08:19
Insanely high mean annual returns for volatile stocks in historical data
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter, LogLocator
# Data:
# lr_rf_1y_t : float — risk-free log return at time t (e.g., log(1.03) for 3%)
# lr_t2 : float — stock actual log return at time t2 = t + period_d
# ema_var_d_t : float — daily variance as log(return)^2, current estimate at time t as EMA(span=365/3)
# h_var_d : float — daily variance as log(return)^2, historical estimate over whole stock history
import './base'
p_([1, 2, 2].empty7()) // => false
p_([[2], [1]].sort_()) // => [[1], [2]]
p_([[1], [1]].uniq_()) // => [[1]]
p_(equal7([1], [1])) // => true
p_([1, 2, 3].median_()) // => 2
p_([1, 2, 3].min_()) // => 1
import pymc as pm
import arviz as az
def fit_bayesian_slow(df, t):
r_t = df['r_t'].values
mvar_t_t0 = t * 0.69 * df['mvar_d_t0'].values
r_rf_t_t0 = t * np.log(df['ar_rf_1y_t0'].values) / 365
with pm.Model() as model:
v0 = pm.HalfNormal('v0', sigma=1)
[0.07662950232169972, 0.08667983817520133, 0.08667983817520133, 0.08667983817520133, 0.18199001797952605, 0.2690013949691558, 0.1636408793113296, 0.18199001797952605, 0.18199001797952605, 0.07662950232169972, 0.07662950232169972, 0.18199001797952605, 0.18199001797952605, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.06833069950700488, 0.06833069950700488, 0.06833069950700488, 0.06833069950700488, 0.06833069950700488, 0.06833069950700488, 0.06833069950700488, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.07662950232169972, 0.08667983817520133, 0.06833069950700488, 0.1636408793113296, 0.1636408793113296, 0.1636408793113296, 0.1636408793113296, 0.2436835869848659, 0.2436835869848659, 0.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.mixture import GaussianMixture
from scipy.stats import norm
def fit_normal_mixture(*, n_components, values, random_state, n_init):
values = np.array(values).reshape(-1, 1) # Convert to 2D array
nmm = GaussianMixture(n_components, covariance_type='diag', random_state=random_state, n_init=n_init)
nmm.fit(values)
means = nmm.means_.flatten().tolist()
@al6x
al6x / diffs.json
Created January 5, 2025 10:33
Stock Price CDF
[3,2.9,2.9,3,3.1,3.2,3.1,3.4444,3.4444,3.4444,3.4444,3.2,3.7778,3.5556,3.3,3.5,3.7778,3.6667,3.4,3.3,3.3,3.2,3.3,3.3,3.4,3.3,3.4,3.4,3.4,3.0909,2.8333,3.0833,3.2727,3.2727,3.2727,3.3636,3.6364,3.5455,3.7273,4,3.9091,3.7273,3.8182,3.9091,3.7273,3.6364,3.5455,3.4545,3.5455,3.4545,3.4545,3.4545,3.6364,3.3333,3.25,3.25,3.25,3.25,3.1667,3.4545,3.4545,3.2727,3.0909,3.0909,3.0909,3.1818,3.2727,3.2727,3.2727,3.2727,3.2727,3.2727,3.2727,3.6,3.2727,3.1818,3.2727,3.1818,3.4,3.1,3.2,3.3,3.7778,3.4,3.6,3.6,3.5,3.6,3.5,3.4,3.1818,3.4,3.0909,3.1818,3.1818,3.4,3.4,3.2,3.3,3.4,3.5,3.6,3.6,3.5,3.5,3.6,3.2727,3.2727,3.2727,3.2727,3.7,3.7,3.9,4,4,4,4.2,3.9,4,4.4444,3.9,3.5455,3.5455,3.6364,4,4.1,4.1,4.1,4,4,3.7,4,4.2,4.3,4.3,4.4,4.5,4.6,4.8,5,5,4.6364,4.6364,4.5455,4.5455,4.1667,4.25,4.0833,4.0833,3.75,2.3846,2.5385,3,2.8462,2.6154,2.3846,2.2308,1.9286,2.5385,2.4286,2.5,2.3571,2.4286,2.3333,2.2,2.2,2.2,2.2667,2.3333,2.2667,2.2,2.1333,2.3571,2.1333,2.0625,1.8333,1.8333,1.7778,1.7222,1.8235,1.7647,1.8235,1.6471,1.7059,1.7647,1.941
@al6x
al6x / array.ts
Last active December 24, 2024 11:34
import './mfun'
declare global {
function fsize<T>(a: T[]): number
function fempty7<T>(a: T[]): boolean
}
mfun(Array, function fsize<T>(a: T[]): number { return a.length })
mfun(Array, function fempty7<T>(a: T[]): boolean { return a.length == 0 })
@al6x
al6x / julia.jl
Created December 21, 2024 09:29
Julia vs Ruby
table = [
sort(
map(
(c) -> (type = :Without, moneyness = c.moneyness),
filter((c) -> c.tenor == 856, normalized_jl)
)
)...,
sort(
map(
(c) -> (type = "With CPI", moneyness = c.moneyness),