LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
RS.m
1function H = RS(sequence,isplot)
2%
3% 'RS' estimate the hurst parameter of a given sequence with R/S method.
4%
5% Inputs:
6% sequence: the input sequence for estimate
7% isplot: whether display the plot. without a plot if isplot equal to 0
8% Outputs:
9% H: the estimated hurst coeffeient of the input sequence
10
11% Author: Chu Chen
12% Version 1.0, 03/10/2008
13% chen-chu@163.com
14%
15
16if nargin == 1
17 isplot = 0;
18end
19
20N = length(sequence);
21dlarge = floor(N/5);
22dsmall = max(10,log10(N)^2);
23D = floor(logspace(log10(dsmall),log10(dlarge),50));
24D = unique(D);
25n = length(D);
26x = zeros(1,n);
27y = zeros(1,n);
28
29R = cell(1,n);
30S = cell(1,n);
31for i = 1:n
32 d = D(i);
33 m = floor(N/d);
34 R{i} = zeros(1,m);
35 S{i} = zeros(1,m);
36 matrix_sequence = reshape(sequence(1:d*m),d,m);
37
38 Z1 = cumsum(matrix_sequence);
39 Z2 = cumsum(repmat(mean(matrix_sequence),d,1));
40 R{i} = (max(Z1-Z2)-min(Z1-Z2));
41 S{i} = std(matrix_sequence);
42
43 if min(R{i})==0 || min(S{i}) ==0
44 continue;
45 end
46
47 x(i) = log10(d);
48 y(i) = mean(log10(R{i}./S{i}));
49end
50
51% fit a line with middle part of sequence
52index = x~=0;
53x = x(index);
54y = y(index);
55n2 = length(x);
56cut_min = ceil(3*n2/10);
57cut_max = floor(9*n2/10);
58
59X = x(cut_min:cut_max);
60Y = y(cut_min:cut_max);
61p1 = polyfit(X,Y,1);
62Yfit = polyval(p1,X);
63H = (Yfit(end)-Yfit(1))/(X(end)-X(1));
64
65if isplot ~= 0
66 figure,hold on;
67 bound = ceil(log10(N));
68 axis([0 bound 0 0.75*bound]);
69
70 temp = (1:n).*index;
71 index = temp(index);
72 for i = 1:n2
73 plot(x(i),log10(R{index(i)}./S{index(i)}),'b.');
74 end
75
76 x = linspace(0,bound,10);
77 y1 = 0.5*x;
78 y2 = x;
79 h1 = plot(x,y1,'b--','LineWidth',2);
80 h2 = plot(x,y2,'b-.','LineWidth',2);
81 plot(X,Yfit,'r-','LineWidth',3);
82 %legend([h1,h2],'slope 1/2','slope 1',4)
83 xlabel('log10(blocks of size m)'),ylabel('log10(R/S)'),title('R/S Method');
84end