// Security Price Sample Path
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <iomanip.h>
//***************************RANDOMIZER************************
#include <time.h>
#include <fstream.h>
unsigned long urand0 (void);
unsigned long urand (void);
void init_generator(unsigned seed);
static unsigned long rand_x[56], rand_y[256], rand_z;
static long rand_j, rand_k;
void init_generator(unsigned seed)
{
int i;
rand_x[1] = 1;
if(seed)
rand_x[2] = seed;
else
rand_x[2] = time (NULL);
for (i=3; i<56; ++i) rand_x[i] = rand_x[i-1] + rand_x[i-2];
rand_j = 24;
rand_k = 55;
for (i=255; i>=0; --i)
urand0 (); //run loop for a while
for (i=255; i>=0; --i)
rand_y[i] = urand0 ();
rand_z = urand0 ();
}
unsigned long urand0 (void)
{
if (--rand_j == 0) rand_j = 55;
if (--rand_k == 0) rand_k = 55;
return rand_x[rand_k] += rand_x[rand_j];
}
unsigned long urand (void)
{
int i;
i = rand_z % 256;
rand_z = rand_y[i];
if (--rand_j == 0) rand_j = 55;
if (--rand_k == 0) rand_k = 55;
rand_y[i] = rand_x[rand_k] += rand_x[rand_j];
return rand_z;
}
//*****************************END*****************************
double normal_rand (void);
//****************************NORMAL***************************
double normal_rand (void)
{
static int flag = 0;
static double z, a = 2147483648.0;
double v1, v2, s;
if (flag) {
flag = 0;
return z;
}
flag = 1;
do {
v1 = urand()/a - 1;
v2 = urand()/a - 1;
}
while ((s = v1*v1 + v2*v2) > 1.0);
s = sqrt (-2.0 * log(s) / s);
z = v1 * s;
return v2 * s;
}
//******************************END****************************
double dist[19] =
{
-1.645, -1.282, -1.037, -0.842, -0.674, -0.524, -0.385, -0.253, -0.126, 0.0,
0.126, 0.253, 0.385, 0.524, 0.674, 0.842, 1.037, 1.282, 1.645
};
int main()
{
const int N = 250;
const double sigma = 0.1,mu = 0.06;
double S = 1.0, dS, dX, dt = 1.0/N;
fstream output;
output.open("path.dat",ios::out);
if(!output)
{
cout<<"unable to open file"<<endl;
return 0;
}
clrscr();
int i;
init_generator(0);
output.setf(ios::showpoint);
output<<setprecision(6)<<S<<endl;
cout<<setprecision(6)<<S<<endl;
for (i=0; i<N; ++i) {
dX = normal_rand() * sqrt (dt);
dS = sigma*S*dX + mu*S*dt;
S += dS;
output<<S<<endl;
cout<<S<<endl;
}
output.close();
}