用Python对股票日度收盘价计算收益率,画收益率直方图,计算收益率的均值、方差、峰度和偏度,并画acf图
时间: 2023-05-24 17:07:11 浏览: 354
python 计算数据偏差和峰度的方法
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
# 读取股票数据
data = pd.read_csv('stock_price.csv', parse_dates=['date'])
data = data.set_index('date')
# 计算收益率
returns = data.pct_change()
returns = returns.iloc[1:]
# 画收益率直方图
plt.hist(returns['close'], bins=50)
plt.title('Histogram of Daily Returns')
plt.xlabel('Daily Returns')
plt.ylabel('Frequency')
plt.show()
# 计算收益率的均值、方差、峰度和偏度
mean_return = np.mean(returns)
var_return = np.var(returns)
skewness = returns.skew()
kurtosis = returns.kurtosis()
print('Mean Return:', mean_return)
print('Variance of Return:', var_return)
print('Skewness:', skewness)
print('Kurtosis:', kurtosis)
# 画ACF图
sm.graphics.tsa.plot_acf(returns, lags=20)
plt.title('Autocorrelation Function of Daily Returns')
plt.xlabel('Lags')
plt.ylabel('ACF')
plt.show()
阅读全文