from xrand by Anis Ben Hassen
compute p-values of the variance ratio test statistics using simulation

[V,Z1,Z2,p1,p2]=xrand(x,k,n)
function [V,Z1,Z2,p1,p2]=xrand(x,k,n)
% -----------------------------------------------------------------
% Elaborated by : BEN HASSEN Anis 
% "Institut Suprieur de Gestion de Tunis" (ISG Tunis)
% University of Tunis
% 41, rue de la Libert - Cit Bouchoucha - C.P. : 2000 Le Bardo
% Tunisia
% University e-mail: http://www.isg.rnu.tn/
% Personal e-mail: benhassenanis@yahoo.com
% _________________________________________________________________
% January 14, 2004.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Output:
% V: variance ratio -- Z1: Homoscedastic statistic -- Z2: Heteroscedastic
% statistic -- p1: p-value under the RW null and the Homoscedastic
% assumption -- p1: p-value under the RW null and the Heteroscedastic
% assumption.
%******************************************************************
% Input:
% x: a column vector of time series (prices Pt)
% k: an integer > 1
% n: number of simulation based on "randomization" procedure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

t=cputime;
[V,Z1,Z2]=vrt(x,k);
rt1=diff(log(x));
T=length(rt1);
m=k*(T-k+1)*(1-k/T);
moy=mean(rt1);
v=var(rt1);
j=1:k-1;
vec1=(2/k*(k-j)).^2;
Zk=zeros(1,n);
Zhk=Zk;
for i=1:n,
	r1=rt1(randperm(T));
	rtk=rkp(r1,k);
	VR=1/m*sum((rtk-k*moy).^2)/v;
	Zk(i)=sqrt(T)*(VR-1)*(2*(2*k-1)*(k-1)/(3*k))^(-.5);
	rst=(r1-moy).^2;
    aux=zeros(1,k-1);
	for j=1:k-1,
	 aux(j)=rst(j+1:T)'*rst(1:T-j);
	end
	vec2=aux/((T*v))^2;
	Zhk(i)=(VR-1)*(vec1*vec2')^(-.5);
end
p1=sum(Zk>=Z1)/n;
p2=sum(Zhk>=Z2)/n;
Time_elapsed=cputime-t;

Contact us at files@mathworks.com