Fayssal El Mofatiche commited on
Commit
0fa667d
·
1 Parent(s): 440981f

finish stylized facts

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -1
  2. standa.ipynb +107 -84
requirements.txt CHANGED
@@ -4,4 +4,5 @@ requests
4
  yfinance
5
  cufflinks
6
  plotly
7
- seaborn
 
 
4
  yfinance
5
  cufflinks
6
  plotly
7
+ seaborn
8
+ scipy
standa.ipynb CHANGED
@@ -1118,7 +1118,7 @@
1118
  " sigma = df[\"log_rtn\"].std()\n",
1119
  " norm_pdf = scs.norm.pdf(r_range, loc=mu, scale=sigma)\n",
1120
  "\n",
1121
- " fig, ax = plt.subplots(1, 2, figsize=(16, 8))\n",
1122
  "\n",
1123
  " # histogram\n",
1124
  " sns.distplot(df.log_rtn, kde=False, norm_hist=True, ax=ax[0])\n",
@@ -1145,17 +1145,11 @@
1145
  " df : DataFrame\n",
1146
  " DataFrame with returns\n",
1147
  " \"\"\"\n",
1148
- " fig, ax = plt.subplots(1, 2, figsize=(16, 8))\n",
1149
  "\n",
1150
- " # squared returns\n",
1151
- " df[\"sq_rtn\"] = df[\"log_rtn\"] ** 2\n",
1152
- " df[\"sq_rtn\"].plot(ax=ax[0])\n",
1153
- " ax[0].set_title(\"Volatility clustering\", fontsize=16)\n",
1154
  "\n",
1155
- " # absolute returns\n",
1156
- " df[\"abs_rtn\"] = df[\"log_rtn\"].abs()\n",
1157
- " df[\"abs_rtn\"].plot(ax=ax[1])\n",
1158
- " ax[1].set_title(\"Volatility clustering\", fontsize=16)\n",
1159
  "\n",
1160
  " sns.despine()\n",
1161
  " plt.tight_layout()\n",
@@ -1199,6 +1193,94 @@
1199
  " return pn.widgets.Tabulator(stats_df, show_index=True)\n",
1200
  "\n",
1201
  "\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1202
  "def get_stylized_facts_charts(ticker: str = TICKER_DEFAULT):\n",
1203
  " \"\"\"\n",
1204
  " Create panel with stylized facts charts. The stylized facts are:\n",
@@ -1225,79 +1307,14 @@
1225
  " # Volatility clustering\n",
1226
  " fig3 = plot_volatility_clustering(df)\n",
1227
  "\n",
1228
- " fig, ax = plt.subplots(3, 2, figsize=(15, 10))\n",
1229
- "\n",
1230
- " # plot returns distribution\n",
1231
- " sns.histplot(\n",
1232
- " df[\"log_rtn\"],\n",
1233
- " ax=ax[0, 0],\n",
1234
- " kde=True,\n",
1235
- " stat=\"density\",\n",
1236
- " color=\"darkblue\",\n",
1237
- " alpha=0.5,\n",
1238
- " bins=50,\n",
1239
- " )\n",
1240
- " ax[0, 0].set_title(\"Returns distribution\")\n",
1241
- "\n",
1242
- " # plot returns distribution\n",
1243
- " sns.histplot(\n",
1244
- " df[\"log_rtn\"],\n",
1245
- " ax=ax[0, 1],\n",
1246
- " kde=True,\n",
1247
- " stat=\"density\",\n",
1248
- " color=\"darkblue\",\n",
1249
- " alpha=0.5,\n",
1250
- " bins=50,\n",
1251
- " )\n",
1252
- " ax[0, 1].set_title(\"Returns distribution\")\n",
1253
- "\n",
1254
- " # plot returns distribution\n",
1255
- " sns.histplot(\n",
1256
- " df[\"log_rtn\"],\n",
1257
- " ax=ax[0, 1],\n",
1258
- " kde=True,\n",
1259
- " stat=\"density\",\n",
1260
- " color=\"darkblue\",\n",
1261
- " alpha=0.5,\n",
1262
- " bins=50,\n",
1263
- " )\n",
1264
- " ax[0, 1].set_title(\"Returns distribution\")\n",
1265
- "\n",
1266
- " # plot returns distribution\n",
1267
- " sns.histplot(\n",
1268
- " df[\"log_rtn\"],\n",
1269
- " ax=ax[0, 1],\n",
1270
- " kde=True,\n",
1271
- " stat=\"density\",\n",
1272
- " color=\"darkblue\",\n",
1273
- " alpha=0.5,\n",
1274
- " bins=50,\n",
1275
- " )\n",
1276
- " ax[0, 1].set_title(\"Returns distribution\")\n",
1277
- "\n",
1278
- " # plot returns distribution\n",
1279
- " sns.histplot(\n",
1280
- " df[\"log_rtn\"],\n",
1281
- " ax=ax[0, 1],\n",
1282
- " kde=True,\n",
1283
- " stat=\"density\",\n",
1284
- " color=\"darkblue\",\n",
1285
- " alpha=0.5,\n",
1286
- " bins=50,\n",
1287
- " )\n",
1288
- " ax[0, 1].set_title(\"Returns distribution\")\n",
1289
- "\n",
1290
- " # plot returns distribution\n",
1291
- " sns.histplot(\n",
1292
- " df[\"log_rtn\"],\n",
1293
- " ax=ax[0, 1],\n",
1294
- " kde=True,\n",
1295
- " stat=\"density\",\n",
1296
- " color=\"darkblue\",\n",
1297
- " alpha=0.5,\n",
1298
- " bins=50,\n",
1299
- " )\n",
1300
- " ax[0, 1].set_title(\"Returns distribution\")\n",
1301
  "\n",
1302
  " return pn.Column(\n",
1303
  " pn.pane.Markdown(\"## 1. Non-Gaussian distribution of returns\"),\n",
@@ -1307,7 +1324,13 @@
1307
  " pn.pane.Markdown(\"## 2. Volatility clustering\"),\n",
1308
  " fig3,\n",
1309
  " pn.pane.Markdown(\"## 3. Absence of autocorrelation\"),\n",
1310
- " fig,\n",
 
 
 
 
 
 
1311
  " )"
1312
  ]
1313
  },
 
1118
  " sigma = df[\"log_rtn\"].std()\n",
1119
  " norm_pdf = scs.norm.pdf(r_range, loc=mu, scale=sigma)\n",
1120
  "\n",
1121
+ " fig, ax = plt.subplots(1, 2)\n",
1122
  "\n",
1123
  " # histogram\n",
1124
  " sns.distplot(df.log_rtn, kde=False, norm_hist=True, ax=ax[0])\n",
 
1145
  " df : DataFrame\n",
1146
  " DataFrame with returns\n",
1147
  " \"\"\"\n",
 
1148
  "\n",
1149
+ " # create new figure\n",
1150
+ " fig, ax = plt.subplots(1, 1)\n",
 
 
1151
  "\n",
1152
+ " df[\"log_rtn\"].plot(title=\"Daily returns\", ax=ax)\n",
 
 
 
1153
  "\n",
1154
  " sns.despine()\n",
1155
  " plt.tight_layout()\n",
 
1193
  " return pn.widgets.Tabulator(stats_df, show_index=True)\n",
1194
  "\n",
1195
  "\n",
1196
+ "def plot_abscense_of_autocorrelation(df):\n",
1197
+ " \"\"\"\n",
1198
+ " Plot absence of autocorrelation\n",
1199
+ "\n",
1200
+ " Parameters\n",
1201
+ " ----------\n",
1202
+ " df : DataFrame\n",
1203
+ " DataFrame with returns\n",
1204
+ " \"\"\"\n",
1205
+ "\n",
1206
+ " N_LAGS = 50\n",
1207
+ " SIGNIFICANCE_LEVEL = 0.05\n",
1208
+ "\n",
1209
+ " # create new figure\n",
1210
+ " fig, ax = plt.subplots(1, 1)\n",
1211
+ "\n",
1212
+ " acf = smt.graphics.plot_acf(\n",
1213
+ " df[\"log_rtn\"], lags=N_LAGS, alpha=SIGNIFICANCE_LEVEL, ax=ax\n",
1214
+ " )\n",
1215
+ "\n",
1216
+ " sns.despine()\n",
1217
+ " plt.tight_layout()\n",
1218
+ "\n",
1219
+ " return fig\n",
1220
+ "\n",
1221
+ "\n",
1222
+ "def plot_small_and_decreasing_autocorrelations(df):\n",
1223
+ " \"\"\"\n",
1224
+ " Plot small and decreasing autocorrelations\n",
1225
+ "\n",
1226
+ " Parameters\n",
1227
+ " ----------\n",
1228
+ " df : DataFrame\n",
1229
+ " DataFrame with returns\n",
1230
+ " \"\"\"\n",
1231
+ "\n",
1232
+ " fig, ax = plt.subplots(2, 1)\n",
1233
+ " SIGNIFICANCE_LEVEL = 0.05\n",
1234
+ " N_LAGS = 50\n",
1235
+ "\n",
1236
+ " smt.graphics.plot_acf(\n",
1237
+ " df[\"log_rtn\"] ** 2, lags=N_LAGS, alpha=SIGNIFICANCE_LEVEL, ax=ax[0]\n",
1238
+ " )\n",
1239
+ " ax[0].set(title=\"Autocorrelation Plots\", ylabel=\"Squared Returns\")\n",
1240
+ "\n",
1241
+ " smt.graphics.plot_acf(\n",
1242
+ " np.abs(df[\"log_rtn\"]), lags=N_LAGS, alpha=SIGNIFICANCE_LEVEL, ax=ax[1]\n",
1243
+ " )\n",
1244
+ " ax[1].set(ylabel=\"Absolute Returns\", xlabel=\"Lag\")\n",
1245
+ "\n",
1246
+ " sns.despine()\n",
1247
+ " plt.tight_layout()\n",
1248
+ "\n",
1249
+ " return fig\n",
1250
+ "\n",
1251
+ "\n",
1252
+ "def plot_leverage_effect(df):\n",
1253
+ " \"\"\"\n",
1254
+ " Plot leverage effect\n",
1255
+ "\n",
1256
+ " Parameters\n",
1257
+ " ----------\n",
1258
+ " df : DataFrame\n",
1259
+ " DataFrame with returns\n",
1260
+ " \"\"\"\n",
1261
+ "\n",
1262
+ " df[\"rolling_std_252\"] = df[[\"log_rtn\"]].rolling(window=252).std()\n",
1263
+ " df[\"rolling_std_21\"] = df[[\"log_rtn\"]].rolling(window=21).std()\n",
1264
+ "\n",
1265
+ " fig, ax = plt.subplots(3, 1, figsize=(18, 15), sharex=True)\n",
1266
+ "\n",
1267
+ " df[\"Close\"].plot(ax=ax[0])\n",
1268
+ " ax[0].set(title=\"Price time series\", ylabel=\"Price ($)\")\n",
1269
+ "\n",
1270
+ " df[\"log_rtn\"].plot(ax=ax[1])\n",
1271
+ " ax[1].set(ylabel=\"Log returns\")\n",
1272
+ "\n",
1273
+ " df[\"rolling_std_252\"].plot(ax=ax[2], color=\"r\", label=\"Rolling Volatility 252d\")\n",
1274
+ " df[\"rolling_std_21\"].plot(ax=ax[2], color=\"g\", label=\"Rolling Volatility 21d\")\n",
1275
+ " ax[2].set(ylabel=\"Moving Volatility\", xlabel=\"Date\")\n",
1276
+ " ax[2].legend()\n",
1277
+ "\n",
1278
+ " sns.despine()\n",
1279
+ " plt.tight_layout()\n",
1280
+ "\n",
1281
+ " return fig\n",
1282
+ "\n",
1283
+ "\n",
1284
  "def get_stylized_facts_charts(ticker: str = TICKER_DEFAULT):\n",
1285
  " \"\"\"\n",
1286
  " Create panel with stylized facts charts. The stylized facts are:\n",
 
1307
  " # Volatility clustering\n",
1308
  " fig3 = plot_volatility_clustering(df)\n",
1309
  "\n",
1310
+ " # Absence of autocorrelation\n",
1311
+ " fig4 = plot_abscense_of_autocorrelation(df)\n",
1312
+ "\n",
1313
+ " # Small and decreasing autocorrelations of squared and absolute returns\n",
1314
+ " fig5 = plot_small_and_decreasing_autocorrelations(df)\n",
1315
+ "\n",
1316
+ " # Leverage effect\n",
1317
+ " fig6 = plot_leverage_effect(df)\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1318
  "\n",
1319
  " return pn.Column(\n",
1320
  " pn.pane.Markdown(\"## 1. Non-Gaussian distribution of returns\"),\n",
 
1324
  " pn.pane.Markdown(\"## 2. Volatility clustering\"),\n",
1325
  " fig3,\n",
1326
  " pn.pane.Markdown(\"## 3. Absence of autocorrelation\"),\n",
1327
+ " fig4,\n",
1328
+ " pn.pane.Markdown(\n",
1329
+ " \"## 4. Small and decreasing autocorrelations of squared and absolute returns\"\n",
1330
+ " ),\n",
1331
+ " fig5,\n",
1332
+ " pn.pane.Markdown(\"## 5. Leverage effect\"),\n",
1333
+ " fig6,\n",
1334
  " )"
1335
  ]
1336
  },