-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (64 loc) · 2.58 KB
/
Copy pathmain.py
File metadata and controls
89 lines (64 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
# @Author: Xingqi Ye
# @Time: 2019-04-07-18
import Functions
import pandas as pd
import config
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from statsmodels.stats.diagnostic import acorr_ljungbox
pd.set_option('expand_frame_repr', False)
daily_data = Functions.import_data()
code_list = daily_data.columns
code_list = code_list.drop('Date')
monthly_data = Functions.transfer_to_period_data(daily_data, 'M')
weekly_data = Functions.transfer_to_period_data(daily_data, '5B')
# calculate the daily, weekly, monthly return
daily_return_data = daily_data.copy()
weekly_return_data = weekly_data.copy()
monthly_return_data = monthly_data.copy()
for x in code_list:
daily_return_data[x] = daily_return_data[x].pct_change(1)
weekly_return_data[x] = weekly_return_data[x].pct_change(1)
monthly_return_data[x] = monthly_return_data[x].pct_change(1)
# Calculate the covariance matrix
covariance_matrix = weekly_data.cov()
covariance_matrix.to_csv(config.output_data_path + '/covHSI.csv', mode='a')
# Plot histogram for 700 HK
Tencent_return = list()
Tencent_return = daily_return_data['700 HK'].tolist()
Tencent_return = Tencent_return[1:]
num_bins = 100
fig, ax = plt.subplots()
n, bins, patches = ax.hist(Tencent_return, num_bins, density=1)
ax.set_xticks(np.arange(-0.1, 0.1, 0.02))
ax.set_yticks(np.arange(0, 88, 8))
plt.title('700 HK')
plt.savefig(config.output_data_path + '/700_HK', formate="PNG")
plt.show()
# normal test
p_value = list(map(lambda x: stats.normaltest(weekly_return_data[x][1:]).pvalue, code_list))
# print(p_value)
# Auto-correlation test
'''
Test
# p_value_auto_corr = acorr_ljungbox(weekly_return_data['101 HK'][1:], lags=[5])
# print(p_value_auto_corr)
Using Map function, return 5 array including the p-value of all the stocks on each lag value from 1 to 5
plot in different colors
'''
p_value_auto_corr = zip(*map(lambda x: acorr_ljungbox(weekly_return_data[x][1:], lags=[5])[1], code_list))
p_value_auto_corr_list = list(p_value_auto_corr)
# print(p_value_auto_corr_list)
fig, axs = plt.subplots(2, 1, constrained_layout=True)
axs[0].scatter(code_list, p_value, c='r', alpha=0.5)
axs[0].tick_params(labelsize=5, labelrotation=45)
axs[0].set_title('normal_test')
axs[1].scatter(code_list, p_value_auto_corr_list, marker='o', label='lags=5')
axs[1].tick_params(labelsize=5, labelrotation=45)
axs[1].set_title('auto_correlation test')
plt.show()
# Daily Return Calculation
daily_return_data['DateTime'] = daily_return_data['Date'].dt.year * 100 + daily_return_data['Date'].dt.month
print(daily_return_data.groupby('DateTime').std())