/* KMB 2003 Nov 28, Dec 04
 AR1 model simulation for testing autocorrelation programs.
 gcc -Wall -O3 ar1.c -o ar1 -lm
 ./ar1 $n 10 1  | cols -c1 -n1 | p --bitmap-size=300x300 -h0.7 -w0.7
 mcopy -o ar1.c a:
 FIXME general alpha
*/

#include <stdlib.h>
#include <stdio.h>
#include "rng-kmb.c"
#include <sys/timeb.h>
#include <sys/time.h>
long time();

int main(int argc, char* argv[]) {
  double alpha=0.5,x=0,sd=0.125;
  int i,n=1000;
  if (argc>1) n =atoi(argv[1]);
  if (argc>2) sd=atof(argv[2]); // noise sd
  _x=time(); // seed by time
  fprintf(stderr,"ar1: n=%d noise std dev=%g seed=%ld\n",n,sd,_x);
  for (i=0; i<1000; i++) { // preiterate
    x=alpha*x+sd*normal01();
  }
  for (i=0; i<n; i++) { // iterate
    printf("%8g\n",x);
    x=alpha*x+sd*normal01();
  }
  return 0;
}

long time() {
  struct timeval tv[1];
  gettimeofday(tv,NULL);
  return tv->tv_usec+1000000*tv->tv_sec;
}
