- 阅读权限
- 255
- 威望
- 7 级
- 论坛币
- -234475 个
- 通用积分
- 124.0224
- 学术水平
- 3783 点
- 热心指数
- 3819 点
- 信用等级
- 3454 点
- 经验
- 150207 点
- 帖子
- 7546
- 精华
- 32
- 在线时间
- 1327 小时
- 注册时间
- 2013-2-3
- 最后登录
- 2022-2-24
|
哥们 这样的代码也好意思发上来???这才是真正的Hurst代码:
程序源码:M文件HurstFactorization.M - function [FactorMatrix,FactorNum]=HurstFactorization(x)
- %hurstFactorization
- %code by ariszheng@gmail.com
- %2008-10-07
- %因子分解, 以4开始以X/4结束
- %floor函数表示四舍五入
- N=floor(x/4);
- %方案数量初始为0
- FactorNum=0;
- %因子分解, 以4开始以X/4结束
- for i=4:N
- %i可以被x整除,即得到一组分解方案
- if mod(x,i)==0
- %方案数量+1
- FactorNum=FactorNum+1;
- %将可行方案存储到FactorMatrix中
- FactorMatrix(FactorNum,:)=[i,x/i];
- end
- end
复制代码
函数测试240共有14个分段方案 - >>X=240
- %调用HurstFactorization函数
- >>[FactorMatrix,FactorNum]=HurstFactorization(x)
- %分解方案序列
- FactorMatrix =
- 4 60
- 5 48
- 6 40
- 8 30
- 10 24
- 12 20
- 15 16
- 16 15
- 20 12
- 24 10
- 30 8
- 40 6
- 48 5
- 60 4
- FactorNum =
- 14
复制代码
步骤二:Hurst指数计算 时间序列Hurst指数计算函数: HurstExponent=HurstCompute(Xtimes) 输入参数: Xtimes:时间序列数据 输出参数: HurstExponent:为二元向量,第一元素为时间序列的hurst指数, 第二元素为回归分析常数项。 注释:回归模型 程序源码:M文件HurstCompute.M - function HurstExponent=HurstCompute(Xtimes)
- %HurstCompute
- %code by ariszheng@gmail.com
- %2008-10-07
- %example HurstExponent=HurstCompute(rand(1,240))
- %输入参数为Xtimes
- LengthX=length(Xtimes);
- %进行因式分解
- [FactorMatrix,FactorNum]=HurstFactorization(LengthX);
- %定义LogRS,为方便计算变量的初始一般为0
- LogRS=zeros(FactorNum,1);
- %定义LogN
- LogN=zeros(FactorNum,1);
- %分组计算
- for i=1:FactorNum
- %根据因式分解方案,将数量进行分组
- %例如 FactorMatrix(i,:)=[8 30]
- %将240个元素的列向量,转换为8X30的矩阵
- dataM=reshape(Xtimes,FactorMatrix(i,:));
- %计算矩阵每列的均值
- MeanM=mean(dataM);
- %执行
- SubM =dataM - repmat( MeanM,FactorMatrix(i,1),1) ;
- RVector=zeros(FactorMatrix(i,2),1);
- SVector=zeros(FactorMatrix(i,2),1);
- %计算(R/S)n的累加
- for j=1:FactorMatrix(i,2)
- %SubVector=zeros(FactorMatrix(i,1),1);
- SubVector=cumsum( SubM(:,j));
- RVector(j)=max(SubVector)-min(SubVector);
- SVector(j)=std( dataM(:,j) );
- End
- %分别计算LogRS、LogN
- LogRS(i)=log( sum( RVector./SVector)/ FactorMatrix(i,2) );
- LogN(i)=log( FactorMatrix(i,1) );
- end
- %使用最小二乘法进行回归,计算赫斯特指数HurstExponent
- HurstExponent=polyfit(LogN,LogRS,1);
- 函数测试,M文件test HurstCompute.M
- 测试方法生成一组布朗运动序列,计算布朗运动序列对数序列的hurst指数,共测试10次。
- %test HurstCompute
- %测试10次
- testNum=10;
- %并将结果存储在result中
- result =zeros(testNum,2);
- for i=1:testNum
- n=120*i;
- dt=1;
- %生存长度不同的的布朗运动序列
- y=cumsum(dt^0.5.*randn(1,n)); % standard Brownian motion
- %计算每组序列的Hurst值
- result(i,:)=HurstCompute(log(y));
- end
- %画图
- plot(1:testNum,result(:,1),'*')
- 测试结果图像:
复制代码
步骤三:移动平均hurst指数计算 使用计算上证指数1991-9-9至2008-10-8上证综指时间序列数据,计算其给定移动平均长度的hurst指数。编写MoveHurst.m函数,其中cyclength为计算周期,用户可根据需求进行修改。 例如计算120个交易日的Husrt指数,使用的数据为[t-119,t]的价格数据即可,移动平均的意思为根据t的向前移动,计算指数的数据[t-119,t]的价格数据同时根据t进行移动。 - %MoveHurst
- %code by ariszheng 2011-5-3
- %读取数据
- [Prices, dates] =xlsread('shindex.xls');
- %数据长度
- DataLength=length(Prices);
- cyclength=120; %计算周期
- %数据长度是否大于计算周期,若只有100个数据
- %不可能计算出120计算周期的Hurst指数的
- if cyclength > DataLength
- plot(1:100,1:100,'r*',1:100,100:-1:1,'ro');
- text(10,50,'Number of data must biger than Cycle Length','FontSize',28);
- else
- plot(1:0.1:10,sin(1:0.1:10),'r*',1:0.1:10,-sin(1:0.1:10),'ro');
- logData=log(Prices);
- %将价格数据转换为对数数据
- logData=logData(DataLength:-1:1);
- %计算价格的对数数据对应的每日收益率
- IndexReturn=[0;logData(2:DataLength)-logData(1:DataLength-1)];
- hurstE=zeros(DataLength,1);
- hurstE(1:cyclength-1)=NaN; %前cyclength-1个日的Hurst指数为NaN
- %计算移动的hurst指数
- for i=1:( DataLength-cyclength+1 )
- HurstExponent=HurstCompute( IndexReturn (i:i+cyclength-1) );
- hurstE(cyclength+i-1)=HurstExponent(1);
- end
- %将数据转换为时间序列,进行时间序列数据的画图
- fts = fints(dates,[hurstE(DataLength:-1:1) logData (DataLength: -1 :1)], {'HurstExponent','logIndex'});
- chartfts(fts);
- end
复制代码
|
-
总评分: 经验 + 160
论坛币 + 60
学术水平 + 5
热心指数 + 9
信用等级 + 5
查看全部评分
|