#include "libscl.h"
#include "svmod.h"

using namespace std;
using namespace scl;

void svmod::set_parms(const realmat& theta)
{
  if(theta.size() != 3) error("Error, svmod, wrong dimension for theta");
  phi = theta[1];
  sigma = theta[2];
  beta = theta[3];
}

realmat svmod::get_parms() const
{
  realmat theta(3,1);
  theta[1] = phi;
  theta[2] = sigma;
  theta[3] = beta;
  return theta;
}

REAL svmod::draw_x0(INT_32BIT& seed) const
{
  return (sigma/sqrt(1.0-phi*phi))*unsk(seed);
}

REAL svmod::draw_xt(REAL xlag, INT_32BIT& seed) const
{
  return phi*xlag + sigma*unsk(seed);
}

REAL svmod::prob_yt(REAL yt, REAL xt) const
{
  const REAL roottwopi = sqrt(6.283195307179587);
  REAL sd = beta*exp(xt);
  REAL z = yt/sd;
  return exp(-0.5*z*z)/(roottwopi*sd);
}

sample svmod::draw_sample(INTEGER n, INT_32BIT& seed) const
{
  sample s(n);
  s.x0 = draw_x0(seed);
  REAL xlag = s.x0;
  for(INTEGER t=1; t<=n; ++t) {
    s.x[t] = draw_xt(xlag, seed);
    s.y[t] = beta*exp(s.x[t])*unsk(seed);
    xlag = s.x[t];
  }
  return s;
}
  
