Statistical test involving a detrending function

39 Views Asked by At

I am a MMATLAB beginner and I am trying to practice with the code below: I randomly generated x and y variable to implement the following formula in matlab : R =

where the quantity Y_bar and bar{X} are obtained according to the following formula : averaging

in the code below after generating x and y I calculate the difference between each element and its predecessor to apply the formula R

Since the second X_bar which appears inside the parenthesisis needs kn more elements I dropped kn elements from my for loop in order to have enough elements in the vector.

Here my attempt:


x       = randn(20000,1);
y       = randn(20000,1);
delta_x = diff(x);
delta_y = diff(y);
  time  = length(x);
    ln  = floor(time^(.5));
    kn  = floor(time^(.5)/2);    
   
    
    dt_detrend = zeros(length(delta_x)-kn,1);
for i = 1:length(delta_x)-kn
    dt_detrend(i,1) = delta_x(i+kn) - mean(delta_x(i:i+kn));
end
delta_y(length(dt_detrend)+1:end) = [];
 n = length(delta_y);
% Preallocate delta2_bar as a matrix

Y_bar = zeros(n-ln,1);
X_bar  = zeros(n-ln,1);
X__bar_detrend   = zeros(n-ln,1);

for i = 1:n-ln
    idx_start = floor(ln/2);
    idx_end = ln - 1;
    j_idx = floor(ln/2) - 1;
    k_idx = kn-1;

    Y_bar(i) = (1/ln) * (sum(delta_y(i+idx_start  : i + idx_end)) - sum(delta_y(i: i+j_idx)));
    X_bar(i) = (1/ln) * (sum(delta_x(i+idx_start: i+idx_end)) - sum(delta_x(i+0 : i+ j_idx)));

    
    X2_bar = zeros(kn, 1);

    for j = 0:k_idx
        X2_bar(j+1) = (1/ln) * (sum(delta_x(i+idx_start +j : i + j + idx_end)) - sum(delta_x(i+j : i+j_idx+j)));
    end

   
     X__bar_detrend(i) = X_bar(i)-mean(X2_bar);
end
X__bar_detrend = X__bar_detrend(ln:ln:end);
Y_bar          =  Y_bar(ln:ln:end);
R = sum(Y_bar.*X__bar_detrend)


The thing that is driving me crazy is that in the solution posted they compute the test thanks to a MATLAB function(not provided). here the solution

for i = 1:length(delta_x)-kn
    dt_detrend(i,1) = delta_x(i+kn) - mean(delta_x(i:i+kn));
end
delta_y(length(dt_detrend)+1:end) = [];
[dY_bar,dX_bar]  = average2(delta_x, delta_y, ln);
dY_bar           = dY_bar(ln:ln:end);
dX_bar           = dX_bar(ln:ln:end);
dX_bar_detrend   = dX_bar - mean(dX_bar);
R                = sum(dY_bar.*dX_bar_detrend )

how is possible to compute dt_bar_detrend with only dX_bar input given that the second term is shifted of kn elements?

0

There are 0 best solutions below