Skip to content

Instantly share code, notes, and snippets.

View al6x's full-sized avatar

Alex Kraft al6x

  • Australia
View GitHub Profile
@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),
@al6x
al6x / js_call.ts
Last active December 20, 2024 09:11
export {}
type Req = { id?: string, target: string, args?: unknown[] }
type Res = { id?: string, is_error: true, error: string } | { id?: string, is_error: false, result: unknown }
const target_cache: { [name: string]: unknown } = {}
async function process({ id, target: path, args }: Req): Promise<Res> {
if (!(path in target_cache)) {
const [module_name, ...names] = path.split('.')
if (names.length < 1) return { id, is_error: true, error: `Invalid path: ${path}` }
// Use 'Prettify Symbols Mode' VSCode Extension to display `foo7` as `foo?`
declare global {
function p(...args: unknown[]): void
function inspect(v: unknown): void
/** Converts anytihng to compact format `{ a: 1, b: [1, 'c']}`,
* could be parsed with the `from_s`, relaxed json or yaml. */
function to_s(v: unknown): string
/** Parses both json, relaxed json, and output of the `to_s`, example `{ k: 1 }` */
function from_s<T = unknown>(v: string): T
function equal7<T>(a: T, b: T): boolean