Created
September 11, 2019 12:58
-
-
Save yuyasugano/fcdb8cfb570bb0abfb55f01d405f6107 to your computer and use it in GitHub Desktop.
Matplotlib distribution for pct_change
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
# 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