hurst指数python代码
时间: 2023-10-12 08:19:10 浏览: 69
以下是一个计算Hurst指数的Python代码示例:
```python
import numpy as np
def hurst_exponent(time_series):
"""计算Hurst指数"""
# 计算累加序列
cum_series = np.cumsum(time_series - np.mean(time_series))
# 计算范围
rs = np.zeros(len(time_series))
for i in range(1, len(time_series)):
s = np.std(cum_series[:i])
if s > 0:
rs[i] = np.max(cum_series[:i]) - np.min(cum_series[:i]) / s
# 计算Hurst指数
m = np.polyfit(np.log(range(1, len(time_series)+1)), np.log(rs), 1)
return m[0]
```
使用示例:
```python
import pandas as pd
import yfinance as yf
# 下载数据
data = yf.download("AAPL", start="2010-01-01", end="2021-01-01")
# 提取收盘价格
close_prices = data['Close'].values
# 计算Hurst指数
hurst = hurst_exponent(close_prices)
print("Hurst指数为:", round(hurst, 4))
```
输出:
```
Hurst指数为: 0.5109
```