Skip to content

Instantly share code, notes, and snippets.

@barron9
barron9 / 6_spring_1_mass.py
Last active December 8, 2025 13:37
nonlinear ODE @1DOF example with Kane's
import sympy as sp
from sympy.physics.mechanics import KanesMethod, Particle, ReferenceFrame, Point, dynamicsymbols
t = dynamicsymbols._t
x, y = dynamicsymbols('x y')
u1, u2, u3 = dynamicsymbols('u1 u2 u3') # generalized speeds: xdot, ydot
# Angular velocity
theta_ = dynamicsymbols('theta') # rotation about z
thetad = dynamicsymbols('theta', 1)
kd = [u1 - x.diff(t), u2 - y.diff(t), u3 - theta_.diff()]
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
# --- parameters ---
m = 5.0
I_th = 0.12
k_y = 300.0
k_th = 12.0
rho = 1.225
@barron9
barron9 / attention.py
Last active October 25, 2025 08:59
attention mechanism exmaple/cos v similarities/ Softmax(Q.KT).V
import numpy as np
np.random.seed(42)
# Expanded sentence: "The cat sat on the mat and the dog chased the cat."
words = ["The", "cat", "sat", "on", "the", "mat", "and", "dog", "chased", "the", "cat"]
# Randomly initialized Q, K, V matrices with 8-dimensional vectors
Q = {word: np.random.rand(81) for word in words}
K = {word: np.random.rand(81) for word in words}
V = {word: np.random.rand(81) for word in words}
@barron9
barron9 / garch.py
Last active October 22, 2025 22:55
gjr-garch(1,1),egarch + tailrisk + VaR +ES
import matplotlib.pyplot as plt
import numpy as np
from arch import arch_model
import numpy as np
import pandas as pd
from arch import arch_model
import yfinance as yf
import seaborn as sns
# data = yf.download("PEKGY.IS",end="2025-10-10",start="2020-01-01")
@barron9
barron9 / lppls.py
Last active October 21, 2025 14:43
(LPPLS) model
import numpy as np
# Your real price data (daily, for example)
# Example: prices = np.array([100.5, 101.2, 102.3, ..., 120.4])
prices = np.array([
2.04,
2.24,
2.46,
2.37,
2.6,
@barron9
barron9 / matmul_interface.cpp
Last active October 7, 2025 11:10
gpu cpu matrix multiple compare / single cuda core
#include <torch/extension.h>
#include <cuda_runtime.h>
#include <c10/cuda/CUDAStream.h>
// Declare the kernel launcher (in .cu)
void matmul_kernel_launcher(const float* A, const float* B, float* C, int N, cudaStream_t stream);
// C++ wrapper
void launch_matmul_kernel(torch::Tensor A, torch::Tensor B, torch::Tensor C, int N) {
const float* A_ptr = A.data_ptr<float>();
#include <benchmark/benchmark.h>
#include <map>
#include <queue>
#include <deque>
#include <string>
#include <functional>
#include <iostream>
#include <iomanip>
// ----------- MatchingEngine and Order Definitions -----------
@barron9
barron9 / titanic_kaggle_contest_classif.py
Last active February 21, 2025 10:27
Titanic - Machine Learning from Disaster / score 0.80382/ feature mods.
import re
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
from sklearn.impute import SimpleImputer
import pandas as pd
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import train_test_split
import xgboost as xgb
@barron9
barron9 / inverted-pendulum-v5.py
Last active December 31, 2024 22:36
q solution for inverted pendulum
import gymnasium as gym
import numpy as np
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
import pickle
import os
env_train = gym.make('InvertedPendulum-v5')
# Define the dimensions of your state space
dim1 = 5
@barron9
barron9 / TD methods.py
Created December 30, 2024 13:48
TD methods.py
# Q-learning Update rule
def q_learning_update(Q, state, action, reward, next_state, lr, discount):
"""
Q-learning update rule
Q(s, a) <- Q(s, a) + α * [r + γ * max_a Q(s', a) - Q(s, a)]
"""
# Compute the Q-value update using Q-learning formula
Q[state][action] += lr * (reward + discount * np.max(Q[next_state]) - Q[state][action])
return Q