Skip to content

Instantly share code, notes, and snippets.

View rian-dolphin's full-sized avatar

Rian Dolphin rian-dolphin

  • University College Dublin
  • Dublin, Ireland
View GitHub Profile
#- How many assests to include in each portfolio
n_assets = 5
#-- How many portfolios to generate
n_portfolios = 1000
#-- Initialize empty list to store mean-variance pairs for plotting
mean_variance_pairs = []
np.random.seed(75)
#-- Loop through and generate lots of random portfolios
#-- Get annualised mean returns
mus = (1+daily_returns.mean())**252 - 1
#-- Get covariances and variances
#- Variance along diagonal of covariance matrix
#- Multiply by 252 to annualise it
#- https://quant.stackexchange.com/questions/4753/annualized-covariance
cov = daily_returns.cov()*252
#-- Plot the risk vs. return of randomly generated portfolios
#- Convert the list from before into an array for easy plotting
mean_variance_pairs = np.array(mean_variance_pairs)
risk_free_rate=0 #-- Include risk free rate here for sharpe ratio
#-- Create Plot
fig = go.Figure()
fig.add_trace(go.Scatter(x=mean_variance_pairs[:,1]**0.5,
y=mean_variance_pairs[:,0],
#- Add color scale for sharpe ratio
#-- Create random portfolio weights and indexes
#- How many assests in the portfolio
n_assets = 5
mean_variance_pairs = []
#-- Store weights and tickers for labelling
weights_list=[]
tickers_list=[]
for i in tqdm(range(1000)):
df = pd.read_csv('WMT_Earnings.csv', index_col='Date')
#-- Set index to correct format
df.index = pd.to_datetime(df.index)
#-- Put the data in chronological order
df = df.iloc[::-1]
#-- Only select data up to end of 2019
df = df[:"2019"]
def train_test(df, test_periods):
train = df[:-test_periods].values
test = df[-test_periods:].values
return train, test
test_periods = 8
train, test = train_test(df, test_periods)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(train)
train_scaled = scaler.transform(train)
train_scaled = torch.FloatTensor(train_scaled)
print(f'Original dimensions : {train_scaled.shape}')
train_scaled = train_scaled.view(-1)
print(f'Correct dimensions : {train_scaled.shape}')
def get_x_y_pairs(train_scaled, train_periods, prediction_periods):
"""
train_scaled - training sequence
train_periods - How many data points to use as inputs
prediction_periods - How many periods to ouput as predictions
"""
x_train = [train_scaled[i:i+train_periods] for i in range(len(train_scaled)-train_periods-prediction_periods)]
y_train = [train_scaled[i+train_periods:i+train_periods+prediction_periods] for i in range(len(train_scaled)-train_periods-prediction_periods)]
#-- use the stack function to convert the list of 1D tensors
class LSTM(nn.Module):
"""
input_size - will be 1 in this example since we have only 1 predictor (a sequence of previous values)
hidden_size - Can be chosen to dictate how much hidden "long term memory" the network will have
output_size - This will be equal to the prediciton_periods input to get_x_y_pairs
"""
def __init__(self, input_size, hidden_size, output_size):
super(LSTM, self).__init__()
self.hidden_size = hidden_size