This file contains hidden or 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 = np.column_stack([train_df[FT_COLS].values]) | |
hmm_model = GaussianHMM(n_components=3, covariance_type="full", | |
n_iter=1000, random_state=SEED).fit(_df) | |
hidden_states = hmm_model.predict(_df) | |
print("Means and vars of each hidden state") | |
for i in range(hmm_model.n_components): | |
print(f'{i}th hidden state') | |
print('mean: ', (hmm_model.means_[i])) | |
print('var: ', np.diag(hmm_model.covars_[i])) |
This file contains hidden or 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
states = (pd.DataFrame(hidden_states, columns=['states'], index=train_df.index) | |
.join(train_df, how='inner') | |
.assign(mkt_cret=train_df.sret.cumsum()) | |
.reset_index(drop=False) | |
.rename(columns={'index':'Date'})) | |
sns.set(font_scale=1.5) | |
sns.set_style('white', style_kwds) | |
order = np.arange(model.n_components) | |
fg = sns.FacetGrid(data=states, hue='states', hue_order=order, |
This file contains hidden or 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_df = data_df.loc[: '2019-01-01'].dropna() | |
test_df = data_df.loc['2019-01-01': ].dropna() | |
X_train = train_df[FT_COLS].values | |
X_test = test_df[FT_COLS].values | |
model = mix.GaussianMixture(n_components=N_COMPONENTS, | |
covariance_type="full", | |
n_init=100, | |
random_state=SEED).fit(X_train) |
This file contains hidden or 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
import numpy as np | |
import pandas as pd | |
import pandas_datareader.data as web | |
import scipy.stats as scs | |
import matplotlib as mpl | |
from matplotlib import cm | |
import matplotlib.pyplot as plt | |
from matplotlib.dates import YearLocator, MonthLocator | |
import seaborn as sns |
This file contains hidden or 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_gen = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=40, shear_range=0.2, zoom_range=0.2, | |
horizontal_flip=True, vertical_flip=True, rescale=1./255., | |
validation_split=0.2) | |
train_generator = train_gen.flow_from_directory(TRAIN_DIR, target_size=IMG_SIZE, batch_size=32, | |
class_mode='categorical', subset='training') | |
valid_generator = train_gen.flow_from_directory(TRAIN_DIR, target_size=IMG_SIZE, batch_size=32, | |
class_mode='categorical', subset='validation') | |
cnn_model2 = tf.keras.Sequential([ | |
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)), |
This file contains hidden or 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
import tensorflow as tf | |
import tensorflow_docs as tfdocs | |
cnn_model = tf.keras.Sequential([ | |
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=train_imgs.shape[1: 4]), | |
tf.keras.layers.MaxPooling2D(2,2), | |
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'), | |
tf.keras.layers.MaxPooling2D(2,2), | |
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'), | |
tf.keras.layers.MaxPooling2D(2,2), |
This file contains hidden or 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
lstm_model = tf.keras.Sequential([ | |
layers.LSTM(32, return_sequences=True, input_shape=(n_steps, 1)), | |
layers.LSTM(32, return_sequences=True), | |
layers.Dropout(0.2), | |
layers.LSTM(32, return_sequences=True), | |
layers.LSTM(32), | |
layers.Dropout(0.2), | |
layers.Dense(1) | |
]) |
This file contains hidden or 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
import numpy as np | |
import tensorflow as tf | |
from tensorflow import keras | |
from tensorflow.keras import layers | |
# require for installation: !pip install -q git+https://github.com/tensorflow/docs | |
import tensorflow_docs as tfdocs | |
import tensorflow_docs.plots | |
import tensorflow_docs.modeling |
This file contains hidden or 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
import pandas as pd | |
import numpy as np | |
import random | |
from sklearn.datasets import make_regression | |
import tensorflow as tf | |
from tensorflow import keras | |
from tensorflow.keras import layers | |
# require for installation: !pip install -q git+https://github.com/tensorflow/docs |
This file contains hidden or 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_chunk(X, y, chunkrows): | |
X_chunk, y_chunk = X[chunkrows], y[chunkrows] | |
return X_chunk, y_chunk | |
def _iter_minibatch(X, y, chunk_size): | |
''' | |
Construct minibatch generator | |
''' | |
_start = 0 |
NewerOlder