## Simple crude Monte-Carlo in C#

Key words: Monte-Carlo, C#, call option by simulation, Box-Muller function

This article describes a simple crude pricing of call option using Monte-carlo simulation.
Taking into account GBM described in the article Stock price simulation, we now develope a code in C# that takes all inputs similar to Black-Scholes inputs and loop over all the path in order to get the final option price.
Random number will be drawn from N(0,1) distribution using the Box-Muller algorithm.
This transformation takes uniformly distributed random variables as inputs and outputs a new set of random variables with a Normal N(0,1)distribution.

The final form will take 6 inputs and one output:

The main function will take all inputs and perform all the work:

private void button1_Click(object sender, EventArgs e)
{
double Volatility = Convert.ToDouble(textBox4.Text);
double Maturity = Convert.ToDouble(textBox1.Text);
double Spot = Convert.ToDouble(textBox3.Text);
double IR = Convert.ToDouble(textBox5.Text);
double NumberPaths = Convert.ToDouble(textBox6.Text);
double Strike = Convert.ToDouble(textBox2.Text);

double Var = Volatility * Volatility * Maturity;
double SpotTick = Spot * Math.Exp(IR * Maturity – 0.5 * Var);

double FinalSpot = 0;
double Payoff =0;
double Results = 0;
double price = 0;
double NormalVariable = 0;
for (double i = 0; i < NumberPaths; i++) { NormalVariable = BoxMuller(); FinalSpot = SpotTick * Math.Exp(Math.Sqrt(Var) * NormalVariable); if ((FinalSpot - Strike) < 0) Payoff = 0; else Payoff = FinalSpot - Strike; Results = Payoff+Results; } price = Results / NumberPaths; price = price * Math.Exp(-IR * Maturity); textBox7.Text = Convert.ToString(price); } Box-Muller function is simply written: double BoxMuller() { double result_value = 0; double x1,x2,squared; do{ Random rand = new Random(); x1 = 2.0 * rand.NextDouble() - 1.0; x2 = 2.0 * rand.NextDouble() - 1.0; squared=x1*x1+x2*x2; } while(squared>=1.0);

result_value = x1 * Math.Sqrt(-2 * Math.Log(squared) / squared);

return result_value;
}

I used simple Black-Scholes calculator in order to estimate and compare the results.
The Black-Scholes shows 6.36 price for a call option. For simplicity sake, the input parameters where taken as it follows from the picture below:

As can be seen, the result is almost identical with the Number of simulation = 1 000 000 in our example.
When running the program many times I saw different results, so number of simulation should be increased as well in order to get more precise value.
It converges to the Black-Scholes results if the number of simulation is around 10 millions or more.

ПОСЛЕДНИЕ СТАТЬИ
РУБРИКИ