GitHub

@@ -440,12 +440,144 @@ We leave you to investigate the [set of available routines](http://docs.scipy.or

440440441441

## Exercises

442442443+

The first few exercises concern pricing a European call option under the

444+

assumption of risk neutrality. The price satisfies

445+446+

$$

447+

P = \beta^n \mathbb E \max\{ S_n - K, 0 \}

448+

$$

449+450+

where

451+452+

1. $\beta$ is a discount factor,

453+

2. $n$ is the expiry date,

454+

2. $K$ is the strike price and

455+

3. $\{S_t\}$ is the price of the underlying asset at each time $t$.

456+457+

For example, if the call option is to buy stock in Amazon at strike price $K$, the owner has the right (but not the obligation) to buy 1 share in Amazon at price $K$ after $n$ days.

458+459+

The payoff is therefore $\max\{S_n - K, 0\}$

460+461+

The price is the expectation of the payoff, discounted to current value.

462+463+464+

```{exercise-start}

465+

:label: sp_ex01

466+

```

467+468+

Suppose that $S_n$ has the [log-normal](https://en.wikipedia.org/wiki/Log-normal_distribution) distribution with parameters $\mu$ and $\sigma$. Let $f$ denote the density of this distribution. Then

469+470+

$$

471+

P = \beta^n \int_0^\infty \max\{x - K, 0\} f(x) dx

472+

$$

473+474+

Plot the function

475+476+

$$

477+

g(x) = \beta^n \max\{x - K, 0\} f(x)

478+

$$

479+480+

over the interval $[0, 400]$ when `μ, σ, β, n, K = 4, 0.25, 0.99, 10, 40`.

481+482+

```{hint}

483+

:class: dropdown

484+485+

From `scipy.stats` you can import `lognorm` and then use `lognorm(x, σ, scale=np.exp(μ)` to get the density $f$.

486+

```

487+488+

```{exercise-end}

489+

```

490+491+

```{solution-start} sp_ex01

492+

:class: dropdown

493+

```

494+495+

Here's one possible solution

496+497+

```{code-cell} ipython3

498+

from scipy.integrate import quad

499+

from scipy.stats import lognorm

500+501+

μ, σ, β, n, K = 4, 0.25, 0.99, 10, 40

502+503+

def g(x):

504+

return β**n * np.maximum(x - K, 0) * lognorm.pdf(x, σ, scale=np.exp(μ))

505+506+

x_grid = np.linspace(0, 400, 1000)

507+

y_grid = g(x_grid)

508+509+

fig, ax = plt.subplots()

510+

ax.plot(x_grid, y_grid, label="$g$")

511+

ax.legend()

512+

plt.show()

513+

```

514+515+

```{solution-end}

516+

```

517+518+

```{exercise}

519+

:label: sp_ex02

520+521+

In order to get the option price, compute the integral of this function numerically using `quad` from `scipy.optimize`.

522+523+

```

524+525+

```{solution-start} sp_ex02

526+

:class: dropdown

527+

```

528+529+

```{code-cell} ipython3

530+

P, error = quad(g, 0, 1_000)

531+

print(f"The numerical integration based option price is {P:.3f}")

532+

```

533+534+

```{solution-end}

535+

```

536+537+

```{exercise}

538+

:label: sp_ex03

539+540+

Try to get a similar result using Monte Carlo to compute the expectation term in the option price, rather than `quad`.

541+542+

In particular, use the fact that if $S_n^1, \ldots, S_n^M$ are independent

543+

draws from the lognormal distribution specified above, then, by the law of

544+

large numbers,

545+546+

$$ \mathbb E \max\{ S_n - K, 0 \}

547+

\approx

548+

\frac{1}{M} \sum_{m=1}^M \max \{S_n^m - K, 0 \}

549+

$$

550+551+

Set `M = 10_000_000`

552+553+

```

554+555+

```{solution-start} sp_ex03

556+

:class: dropdown

557+

```

558+559+

Here is one solution:

560+561+

```{code-cell} ipython3

562+

M = 10_000_000

563+

S = np.exp(μ + σ * np.random.randn(M))

564+

return_draws = np.maximum(S - K, 0)

565+

P = β**n * np.mean(return_draws)

566+

print(f"The Monte Carlo option price is {P:3f}")

567+

```

568+569+570+

```{solution-end}

571+

```

572+573+574+443575

```{exercise}

444576

:label: sp_ex1

445577446578

In {ref}`this lecture <functions>`, we discussed the concept of {ref}`recursive function calls <recursive_functions>`.

447579448-

Try to write a recursive implementation of homemade bisection function {ref}`described above <bisect_func>`.

580+

Try to write a recursive implementation of the homemade bisection function {ref}`described above <bisect_func>`.

449581450582

Test it on the function {eq}`root_f`.

451583

```

Read the original on github.com ↗