/*@@@@@@@@@@@@@@@@@@@@@@@@ Binom.h @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 Random Option Pricing
 Summer 2002
 Authors: A. Korobchevsky
          S. Ferrando
 Ryerson University ALL RIGHTS RESERVED
 
 Purpose: Declares and Imlements the Binom Descrete Underlying Structure
          All functions defined inline.
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

#ifndef BINOM_H__
#define BINOM_H__

#include <cmath>
#include "../Rng/GSL_RandomGen.h" 
#include "Underlying.h"




struct Binom : Underlying
{ /************************** Binom *********************************** 
  Function: Underlying 
  Purpose: Constructor (inline)
  Param: char* - random generator abbreviated name (mt, taus, gfsr)
  Returns: None
  Note: Seed and precision are hardcoded
  *********************************************************************/ 
  Binom(char* name) { gen = new GSL_RandomGen(name, 35345, 8); }   




  /***************************** next ********************************* 
  Function: next 
  Purpose: Calculates next stock value
  Param: double - Current stock value
  Returns: Next stock value
  *********************************************************************/ 
  double next(double ST) { return gen->next() < P ? ST * D : ST * U; }




  /*************************** generatePath *************************** 
  Function: generatePath 
  Purpose: Dumps the whole path at once. Forces specified number of ups 
  Param: int - length of the path
         int - number of ups forced in the path
  Returns: Array of ups and downs
  *********************************************************************/ 
  double* generatePath(int N, int n)
  { double* path = new double[N];

    for (int i = 0; i < N; i++)
      path[i] = D;
    for (int i = 0; i < n; )
    { int index = gen->nextInt(N);
      // cout << "index = " << index << " " << i << " "; 
      if (path[index] < 1.0) 
      { path[index] = U;
        i++;
      }
    }
    return path;
  }
};

#endif
