Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created September 11, 2019 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/fcdb8cfb570bb0abfb55f01d405f6107 to your computer and use it in GitHub Desktop.
Save yuyasugano/fcdb8cfb570bb0abfb55f01d405f6107 to your computer and use it in GitHub Desktop.
Matplotlib distribution for pct_change
# pct_change
f = lambda x: 1 if x>0.0001 else -1 if x<-0.0001 else 0 if -0.0001<=x<=0.0001 else np.nan
y = df.rename(columns={'close': 'y'}).loc[:, 'y'].pct_change(1).shift(-1).fillna(0)
X = df.copy()
y_ = pd.DataFrame(y.map(f), columns=['y'])
df_ = pd.concat([df, y_], axis=1)
# check the shape
print('----------------------------------------------------------------------------------------')
print('X shape: (%i,%i)' % X.shape)
print('y shape: (%i,%i)' % y_.shape)
print('----------------------------------------------------------------------------------------')
print(y_.groupby('y').size())
print('y=1 up, y=0 stay, y=-1 down')
print('----------------------------------------------------------------------------------------')
t_bool = df_['y'] == 1
f_bool = df_['y'] == -1
pd.set_option('display.max_rows', 200)
# histgram drawing
y_std = (y - y.mean())/y.std() # Standalization
fig = plt.figure(figsize=(15,5))
ax = fig.add_subplot(1, 1, 1)
ax.set_title('Distribution of pct_change')
ax.set_xlabel('pct_change')
ax.set_ylabel('freq')
ax.set_ylim(0, 1.0)
ax.hist(y_std, bins=100, range=(-5,5), density=True)
ax.grid()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment