See the business website
   www.hamedhaseliproduct.webs.com

Hamed Haseli's website

welcome to my website

Share this page

 

Source codes

R code to produce Bootstrap AR(5) results using matrix notations:


# ---- GENERATING AR COEFFICIENTS ---#

generateAR<-function (n=1) {

  repeat

  {

    minroots =0

    ar=runif(n,-1,1)

    minroots <- min(Mod(polyroot(z=c(1,-ar))))

    if(minroots > 1) break

  }

  return(ar)

}


# ---- start bootstrap ----#

par(mfrow=c(1,2),las=2,cex=.5);

n=100

nv=5

m=matrix(ncol=n,nrow=nv)

v=generateAR(nv)

for (t in 1:n){

  ars<-arima.sim(model=list(ar=v),n=2000);

  ars=scale(ars)

  #acf(ars);pacf(ars)

  x=makematrix(ars,nv,mean(ars))

  y=ars

  l=lm(y~x+0)

  for (i in 1:nv){

    m[i,t]=l$coeff[i]

  }

}

matplot(m,pch=c('+'))

abline(h=v,lty=1:length(y))

axis(side=2,at=v)

boxplot(t(m),notch=TRUE,axes=FALSE,outline=FALSE)

axis(side=2,at=round(v,3))

abline(h=v,lty=1:length(y))

The code below produces numerical results for H'pHq matrix. Note that this code simulates REGARMA ten times and compute H'pHq. 


require(Mparcor)

ar=12;ma=11

p=15;m=10000;

H=matrix(0,ncol=ma,nrow=ar)


for ( i in 1:10){

  x=matrix(runif(m*p),ncol=p)

  y=rnorm(m)

  n=1

  x2=x[-(1:n),];x2=scale(x2)

  y2=y[-(1:n)];y2=scale(y2)

  Hp=makematrix(y2,ar,rname='Hp')

  adl=lm(y2~cbind(Hp,x2)+0)

  yy=cbind(Hp,x2) %*% adl$coefficients

  ey=y2-yy

  colMeans(t(ey) %*% cbind(Hp,x2))

  Hq=makematrix(ey,ma,rname='Hq')

  H=H+(t(Hp) %*% Hq/length(y2))

  #print(round(colSums(H/i),3))

}

H=H/10

plot(diag(H),type='b')



Auto backup script for TexStudio.

Auto backup script for TexStudio.

To use this script create a folder named 'backup' within the same directory as your project directory. If you have multiple files, don't forget to create a backup folder within each project file.


%SCRIPT

var Debug = false; //default = false

var DoSave = true; //default = true

var Interval = 60000*15;// set the time in milliseconds. 60000=1mins



registerAsBackgroundScript("auto_save");

setTimeout(TimedFunction,Interval);



function TimedFunction() {

if (Debug) { alert('timer expired') };

SaveAllDocuments();

setTimeout(TimedFunction,Interval); //rearm the timed function

}


function SaveAllDocuments(){

if (Debug) { alert('SaveAllDocuments called') };

var NumOfDocs = documents.length;

if (Debug) { alert('NumOfDocs='+NumOfDocs) };

for (var i = 0; i < NumOfDocs; i++) {

SaveDocument(i, documents[i]);

};

};


function SaveDocument(i, Document) {

var t = new Date();

var tt='_'+t.getSeconds()+'_'+t.getMinutes()+'_'+t.getHours()+'_'+t.getDate()+'_'+(t.getMonth() +1)+'_'+t.getFullYear()+'_';

var CurEditor = Document.editorView.editor;

var CurFileName = CurEditor.fileName();

if (Debug) { alert('i='+i+' FileName='+CurFileName) };

var fname=(CurFileName.replace(/^.*[\\\/]/, ''));

var newName =CurFileName.replace(fname,'backup/'+fname+ tt+'_bak.tex');

if (Debug) { alert('i='+i+' newName='+newName) };

if (DoSave) { CurEditor.saveCopy(newName); };

};



Generate REGARMA(1,1) results


library(REGARMA)

#rm(list=ls(all=TRUE))


iterate=100

n=1000

beta=c(-.712,.123,-.093);nbeta=length(beta)

phi=-.612;    nphi=length(phi)

theta=.361;  ntheta=length(theta)

THETA=c(phi,theta)



bmatrix=matrix(ncol=(nphi+ntheta+nbeta),nrow=iterate)

LMbmatrix=matrix(ncol=nbeta,nrow=iterate)


for(i in 1:iterate){

  y=REGARMA:::sim.regARMA(n=n,beta=beta,phi=phi,theta=theta,var.error=.5,x.independent = FALSE)

  Hp=REGARMA:::makematrix(y$y,p=nphi,beforestart.mean = 0)

  epsilon=y$y-predict(lm(y$y~y$x+Hp+0))

  Hq=REGARMA:::makematrix(epsilon,p=ntheta)

  lmm=lm(y$y~Hp+Hq+y$x+0)

  bmatrix[i,]=as.vector(lmm$coefficients)  

  LMbmatrix[i,]=as.vector(lm(y$y~y$x+0)$coefficients) 

}



boxplot(bmatrix,outline=FALSE,ylab='Parameter space',ylim=c(min(bmatrix)-.2,max(bmatrix)+.4),add=FALSE) 


legend('topright',lty=1:(nbeta+1), legend=paste('beta',1:nbeta,'=',round(beta,3),sep=' '), bty='n',cex=.85) 

legend('topleft',lty=4:5,          legend=paste(c('phi','theta'),'=',round(c(phi,theta),3),sep='.'), bty='n',cex=.85) 



abline(h=beta,pch=1,lty=1:nbeta)

abline(h=theta,pch=2,lty=5)

abline(h=phi,pch=3,lty=6)

abline(v=2.5)



Below is a sample output of this code!