Skip to content

Instantly share code, notes, and snippets.

View Athe-kunal's full-sized avatar
🎯
Focusing

Astarag Mohapatra Athe-kunal

🎯
Focusing
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
class RotaryPositionalEncoding(nn.Module):
d_model: int
max_len: int = 5000
def setup(self):
#position of the token
position = np.arange(0, self.max_len, dtype=np.float32)[:,None]
#10000^(-2(i-1)/d), where is the embedding index
theta_vals = 10000**((-2*np.arange(1,self.d_model//2 + 1) - 1)/self.d_model)
m_theta = position * theta_vals
from __future__ import annotations
import gymnasium as gym
import numpy as np
from numpy import random as rd
class StockTradingEnv(gym.Env):
def __init__(
self,
def annuity_bond_price(coupon_rate,ytm,FV,time_horizon,pay_period):
coupon_payment = (coupon_rate*FV/100)/pay_period
coupon_rate = coupon_rate/pay_period
coupon_periods = time_horizon*pay_period
ytm = ytm/pay_period
# annuity_payment = coupon_payment*((1-(1/(1+ytm/100)**coupon_periods))/(ytm))*100
# annuity_payment = coupon_payment*(1/(ytm/100))*(1-1/(1+(ytm/100))**coupon_periods)
annuity_payment = 0
for i in range(1,coupon_periods+1):
import numpy as np
import risk_kit as erk
import ipywidgets as widgets
from IPython.display import display
import pandas as pd
import matplotlib.pyplot as plt
def gbm(n_years = 10, n_scenarios=1000, mu=0.07, sigma=0.15, steps_per_year=12, s_0=100.0, prices=True):
"""
Evolution of Geometric Brownian Motion trajectories, such as for Stock Prices through Monte Carlo
# @Author: Astarag Mohapatra
import ray
assert (
ray.__version__ > "2.0.0"
), "Please install ray 2.2.0 by doing 'pip install ray[rllib] ray[tune] lz4' , lz4 is for population based tuning"
from pprint import pprint
from ray import tune
import shap
import logging
from shapash.backend import BaseBackend
logging.getLogger('shap').setLevel(logging.WARNING) # Disable info logs
def predict_func(X):
return tuned_model_ppo.predict(X,deterministic=True)[0]*env_kwargs['hmax']
class CustomShapBackend(BaseBackend):
def __init__(self, model, data, preprocessing=None):
def preprocessing(background_df:pd.DataFrame,features_dict):
e_explain_gym = StockTradingEnv(df = background_df, **env_kwargs)
explain_env, _ = e_explain_gym.get_sb_env()
done = False
obs = explain_env.reset()
state_df = pd.DataFrame()
actions_df = pd.DataFrame()
while not done:
action, _states = tuned_model_ppo.predict(obs,deterministic=True)
obs, reward, done, info = explain_env.step(action*env_kwargs['hmax'])
def preprocessing(background_df:pd.DataFrame,features_dict):
e_explain_gym = StockTradingEnv(df = background_df, **env_kwargs)
explain_env, _ = e_explain_gym.get_sb_env()
done = False
obs = explain_env.reset()
state_df = pd.DataFrame()
actions_df = pd.DataFrame()
while not done:
action, _states = tuned_model_ppo.predict(obs,deterministic=True)
obs, reward, done, info = explain_env.step(action*env_kwargs['hmax'])
import random
def makeRQM(arr,x,y):
print(arr[x:y+1])
length = len(arr)
Ns = []
for N in range(0,len(arr)+1):
if 2**N>=length:
break
else:
Ns.append(N)