This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-- 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)): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import MinMaxScaler | |
scaler = MinMaxScaler() | |
scaler.fit(train) | |
train_scaled = scaler.transform(train) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
OlderNewer