/* Hypercube Vibrator for any multi-dimensional N(0,1) data */ new; cls; data=LHss(10,3); /* For example, data is LHS U(0,1) */ nn=10; xx=LHvibrator(data,nn); library pgraph; graphset; pqgwin auto; _plctrl=-1; title("before"); xyz(data[.,1],data[.,2],data[.,3]); title("after"); xyz(xx[.,1],xx[.,2],xx[.,3]); print data~xx; /* See what's going on. */ /* ** LHvibrator.txt - Hypercube Vibrator for any multi-dimensional N(0,1) data. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Vibrates U(0,1) data within each virtual hypercube. ** ** Format: xx=LHvibrator(data,nn); ** ** Input: data matrix, n x dim matrix of any U(0,1) data ** ** nn scalar, number of segments in each dimension ** ** ** Output: xx matrix, n x dim matrix of resulting U(0,1) data ** ** Notice: This might not have any effect at all, * but adds some randomness(vibration) to multi-dimensional U(0,1) data. */ proc LHvibrator(data,nn); local n,dim,y,x,xx; n=rows(data); dim=cols(data); /* avoid 1 and adjust it */ if not data<1; data=data.*(data.<1)+0.99999999.*(data.==1); endif; /* main */ y=floor(data*nn); /* location index y=0,1,2,...,nn-1 */ x=rndu(n,dim)/nn; /* randomized position within each hypercube */ xx=y/nn+x; /* back them to multi-dimensional U(0,1) */ retp(xx); endp; /* This is an example. Any U(0,1) sequence. */ /* ** LHss.txt - Latin Hypercube U(0,1). ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Calculates Latin Hypercube numbers in a very easy way. new algorithm. ** ** Format: x=LHss(nn,dim); ** ** Input: nn scalar, max number of index (1,2,3,...,nn) ** ** dim scalar, dimension (1,...,dim) ** ** ** Output: x matrix , (nn^dim) x (dim) of resulting U(0,1) numbers ** ** Notice: It takes lots of memory to run. Light version of GAUSS will not work in most cases. */ proc LHss(nn,dim); local x,b,n,z,y,indexvec,d,U,i,index; /* convert x(base 10) to y(base b) */ x=seqa(1,1,nn^dim-1); b=nn; n=maxc(log(x)/log(b))+1; z=reshape(b,n,rows(x)); y=rev(recserrc(x,z))'; indexvec=y+1; /* shift all elements by 1 */ indexvec=ones(1,dim)|indexvec; /* insert 1's at the 1st row */ /* divide between 0 and 1 into nn segments and sample from each segment */ d=1/nn; U=(d*indexvec-d*(indexvec-1)).*rndu(nn^dim,dim)+d*(indexvec-1); do while NOT(U>0); /* check if U contains 0's */ U=(d*indexvec-d*(indexvec-1)).*rndu(nn^dim,dim)+d*(indexvec-1); endo; /* randomize the order as a row */ x=zeros(nn^dim,dim); i=1; do while i<=nn^dim-1; index=ceil((nn^dim-(i-1))*rndu(1,1)); x[i,.]=U[index,.]; if index==1; U=U[2:rows(U),.]; elseif index==rows(U); U=U[1:rows(U)-1,.]; else; U=U[1:(index-1) (index+1):rows(U),.]; endif; i=i+1; endo; x[nn^dim,.]=U; retp(x); endp;