With *Monte Carlo* we can simulate a distribution by way of randomly generating a whole bunch of numbers.
In **Central Limit Theorem**, the distribution of the sum of a large number of small contributions is approximately Gaussian.

Stock prices can be described as a *random walk*. That is, a stock price is the linear combination of a number of infinitessimally small changes otherwise known as the **Wiener Process**. Each change is randomly sampled from the standard Gaussian distribution . Our predicted stock price is where ever we end up at the end of the random walk.

With Monte Carlo, we simply predict a **large** number of stock prices and calculate the pay-off for prediction. Then we average these pay-offs and apply discounting to obtain our predicted option price.

## Formula

We use the Generalised Wiener process to generate a random walk. This recursive formula describes this process:

Where:

- is the basic
**Wiener Process**. - is a time step of some very small duration
- is the stock price at the current time step
- is the stock price at the next time step

Note: is our initial stock price.

## Basic Wiener Process

The **Wiener Process** is a particular type of *Markov Process* with mean change 0 and variance rate of 1 per year.

We sample randomly from the standard Gaussian distribution and multiply by .

where is our random sample.

Values of at different time slices, , are independent.

### Random Walk

Consider the below illustration. We a see a simple random walk with approximately intervals of .

As you can see, at each step we move up or down.

At the end of the walk (), we can describe the position of random variable as:

Our final position is calculated as the linear combination of every step in the walk.

### Epsilon

We define epsilon as a **random variable** sampled from the standard gaussian (mean 0, standard devition 1).

.

This means that the value of epsilon is going to be somewhere between -1 and +1. On average, this value is going to be .

The sign of our value dictates the up/down direction of our step.

### Scaling

Our Random Walk has the duration . But we would like our steps to be as small as possible. The result is a really fine random walk.

We therefore want to be as large as possible.

Suppose we want to simulate the change in a stock price over the next year, . It would make sense to divide this time horizon by such that each timeslice represents one day.

## Java

### Sampling in Java

How do we generate epsilon in Java?

We use the `java.util.Random`

class. When instantiated, this class will generate an iterable collection of pseudorandom numbers. This collection is governed by a seed, which is set by the constructor `Random()`

.
According to the Java doc, this seed is unlikely to collide with any previous seed.

```
Random random = new Random();
```

We want to sample from the *Gaussian distribution* so we use the method `nextGaussian()`

to return the next pseudorandom sample from the Gaussian distribution.

```
double epsilon = random.nextGaussian();
```

### Basic Weiner Process

Translating all this to a method is rather simple:

```
public double basicWeinerProcess(double dt) {
Random epsilon = new Random();
// sample from random Gaussian of mean 0 and sd 1
double dz = epsilon.nextGaussian()*Math.sqrt(dt);
// return a step. value dz, size dt.
return dz;
}
```

### Random Walk

`MonteCarlo`

is an *abstract class*, which means it can contain *abstract methods*.

`simulateRandomWalk`

is an abstract method that returns two different results.
This is because we are interested in computing both Asian and European payoffs.

#### … for European payoff

A European payoff is defined as follows for calls and puts respectively:

As such, at the end of our random walk, we need only return the stock price at maturity, .

```
public double simulateRandomWalk
(int N, double S0, double dt, double interest, double sigma) {
double St = S0;
for (int t = 1; t < N; t++) {
double dz = basicWeinerProcess(dt);
St = St + (interest * St * dt) + (sigma * St * dz);
}
return St;
}
```

#### …for Asian payoff

In computing the payoff for Asian options, we use the average stock price over the entire time horizon instead of the stock price at maturity.

The implementation is similar to the above case except we keep a *partial total* of stock prices in the `while`

loop.

At the end of the loop, we take this total and divided by the number of steps. In doing so, we return the average stock price.

```
public double simulateRandomWalk
(int N, double S0, double dt, double interest, double sigma) {
double St = S0;
double partialTotal = S0;
for (int t = 1; t < N; t++) {
double dz = basicWeinerProcess(dt);
St = St + (interest * St * dt) + (sigma * St * dz);
partialTotal += St;
}
return partialTotal/N;
}
```