Tuesday, October 5, 2010

Expected stopping time with Monte carlo simulation on naive stock price model

In one of my stochastic model assignments, I was told to find the expected stopping time for which a stock will reach a certain price,
the stock price movement is piece-wise linear and therefore naive, I solved it using monte carlo simulation, though it should be noted that an analytical solution is possible but of an order of difficulty higher. This example highlights the popularity of simulation over analytics in the finance sector.


/****
Chee Tji Hun
Question:
Consider a naive model for a stock that has a support level of $20.
Suppose also that the stock price moves randomly with a downward bias when
the price is above $20 and randomly with an upward bias when the price is below $20.
The probabilities for stock price movement is as follows:

Price(t+1) = price(t) + 1 with probability of 1/3 if price > 20, 9/10 if price = 20, 2/3 if price < 20
Price(t+1) = price(t) - 1 with probability of 2/3 if price > 20, 1/10 if price = 20, 1/3 if price < 20

Calculate the expected time for the stock price to fall from $25 through the support level of $20 all the way down to $18

******/

#include <iostream>
#include <time.h>
#include <vector>
using namespace std;

int main(){
unsigned int iter = 1000000; //amount of times to run simulation
srand(unsigned int(time(NULL))); //seed the random generator

int sum = 0; //summed total number of days for (iter) num of simulations.
int startPrice = 25;//start simulation at this price
int endPrice = 18; //stop simulation when price hits this
double oneTenth = 1.0/10.0;//probabilities
double twoThirds = 2.0/3.0;//probabilities

//run the simulation iter times
for (unsigned int i=0;i<iter;i++){

int price = startPrice;
int days=0; //days counter, counts the number of days to acheive end price
while(price>endPrice){//when not end price keep going
double x = ((rand()%32767)/32766.0); //x is a random double from 0 to 1
//price action above 20
if(price>20){

if(x<twoThirds){
price--;
}else{
price++;
}
}else if(price==20){ //price action at 20
if(x<oneTenth){
price--;
}else{
price++;
}
}else if(price<20){//price action under 20
if(x<twoThirds){
price++;
}else{
price--;
}
}
days++;
}//end while, end price reached

sum+=days;//add number of days to summed total
}//end for, completed all the simulations

cout<<double(sum)/double(iter);//avg number of days to achieve end price
}

No comments:

Post a Comment