#!/usr/bin/perl -w

print "Enter phi ( zero for MA(1) ): ";
chomp( $phi =  );
print "Enter theta ( zero for AR(1) ): ";
chomp( $theta =  );
print "Enter the number of points to generate: ";
chomp( $n =  );
if ( $n < 2 ) {
  print "$n is too small\n";
  exit 1;
}
open OUTFILE, ">armafile";
$prevX = -1 + rand 2;
print OUTFILE "$prevX\n";
@Xvec = ( $prevX );
$prevZ = -1 + rand 2;
for ($i=1; $i<$n; $i++) {
  $thisZ = -1 + rand 2;
  $thisX = $phi * $prevX + $thisZ + $theta * $prevZ;
  print OUTFILE "$thisX\n";
  push @Xvec, $thisX;
  $prevZ = $thisZ;
  $prevX = $thisX;
}

open ACFILE, ">acfile";
$m = 0;
for ($i=0; $i<$n; $i++) {
  $m += $Xvec[$i];
}
$m /= $n;

for ($h=0; $h<$n-10; $h++) {
  $sum = 0;
  for ($t=0; $t<$n-$h; $t++) {
    $sum += ($Xvec[$t+$h] - $m)*($Xvec[$t] - $m);
  }
  $sum /= $n;
  print ACFILE "$sum\n";
}