Skip to content

Commit

Permalink
Merge pull request #1 from quantmind/ls-dc
Browse files Browse the repository at this point in the history
use Pydantic
  • Loading branch information
lsbardel committed Jul 6, 2023
2 parents 6fb8983 + 82a8dfc commit 91c74c7
Show file tree
Hide file tree
Showing 20 changed files with 1,488 additions and 1,266 deletions.
33 changes: 19 additions & 14 deletions notebooks/cir.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.13.8
jupytext_version: 1.14.7
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand Down Expand Up @@ -37,18 +37,27 @@ The model has a close-form solution for the mean and the variance

```{code-cell} ipython3
from quantflow.sp.cir import CIR
pr = CIR(1, kappa=0.8, sigma=0.8, theta=1.2)
pr = CIR(sigma=0.8)
pr
```

```{code-cell} ipython3
pr.is_positive
```

```{code-cell} ipython3
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
p = pr.sample(10, t=1, steps=100)
x = pd.DataFrame(p.data)
fig = px.line(p.data)
fig.show()
p = pr.paths(10, t=1, steps=1000)
p.plot()
```

Sampling with a mean reversion speed five times larger

```{code-cell} ipython3
pr.kappa = 5; pr
```

```{code-cell} ipython3
pr.paths(10, t=1, steps=1000).plot()
```

```{code-cell} ipython3
Expand All @@ -64,6 +73,7 @@ chracteristic_fig(m, N, M).show()
The code below show the computed PDF via FRFT and the [analytical formula](https://en.wikipedia.org/wiki/Cox%E2%80%93Ingersoll%E2%80%93Ross_model).

```{code-cell} ipython3
import plotly.graph_objects as go
dx = 4/N
r = m.pdf_from_characteristic(N, M, dx)
fig = go.Figure()
Expand All @@ -72,11 +82,6 @@ fig.add_trace(go.Scatter(x=r["x"], y=m.pdf(r["x"]), name="analytical", line=dict
fig.show()
```

```{code-cell} ipython3
import plotly
plotly.__version__
```

```{code-cell} ipython3
```
38 changes: 35 additions & 3 deletions notebooks/dsp.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,42 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.13.8
jupytext_version: 1.14.7
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---

# Doubly Stochastic Poisson Process
# Poisson Processes

## Poisson Process

```{code-cell} ipython3
from quantflow.sp.poisson import PoissonProcess
pr = PoissonProcess(rate=2)
pr
```

```{code-cell} ipython3
p = pr.paths(10, t=1, steps=1000)
p.plot()
```

## Compound Poisson Process

The compound poisson process is a jump process, where the arrival of jumps follows the same dynamic as the Poisson process but the size of jumps is no longer constant and equal to 1, instead it follows a given distribution.

The library includes the Exponential Poisson Process, a compound Poisson process where the jump sizes are sampled from an exponential distribution.

```{code-cell} ipython3
from quantflow.sp.poisson import ExponentialPoissonProcess
p = ExponentialPoissonProcess(rate=1, decay=1)
p
```

## Doubly Stochastic Poisson Process


The aim is to identify a stochastic process for simulating the goal arrival which fulfills the following properties
Expand All @@ -22,7 +50,7 @@ The aim is to identify a stochastic process for simulating the goal arrival whic
* Capture the inherent randomness of the goal intensity
* Intuitive

Before we dive into the details of the DSP process, lets take a quick tour of what Lévy processes are, how a time chage can open the doors to a vast array of models and why they are important in the context of DSP.
Before we dive into the details of the DSP process, lets take a quick tour of what Lévy processes are, how a time chage can open the doors to a vast array of models and why they are important in the context of DSP.of DSP.

+++

Expand Down Expand Up @@ -63,3 +91,7 @@ The intensity function of a DSPP is given by:
```{code-cell} ipython3
```

```{code-cell} ipython3
```
2 changes: 1 addition & 1 deletion notebooks/fmp.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.4
jupytext_version: 1.14.7
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand Down
26 changes: 20 additions & 6 deletions notebooks/heston.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.13.8
jupytext_version: 1.14.7
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---

+++ {"tags": []}

# Heston Model and Option Pricing

A very important example of time-changed Lévy process useful for option pricing is the Heston model. In this model the Lévy process is a standard Brownian motion, while the activity rate follows a CIR process. The leverage effect can be accomodated by correlating the two Brownian motions as the following equations illustrates:
Expand All @@ -34,16 +32,28 @@ This means that the characteristic function of $y_t=x_{\tau_t}$ can be represent
```{code-cell} ipython3
from quantflow.sp.heston import Heston
pr = Heston.create(vol=0.6, kappa=1.3, sigma=0.9, rho=-0.6)
pr
```

```{code-cell} ipython3
# check that the variance CIR process is positive
pr.variance_process.is_positive
```

## Characteristic Function

```{code-cell} ipython3
[p for p in pr.parameters]
from notebooks.utils import chracteristic_fig
N = 128
M = 30
m = pr.marginal(1)
chracteristic_fig(m, N, M).show()
```

## Marginal Distribution

Here we compare the marginal distribution at a time in the future $t=1$ with a normal distribution with the same standard deviation.

```{code-cell} ipython3
# Marginal at time 1
m = pr.marginal(1)
Expand All @@ -57,9 +67,9 @@ from scipy.stats import norm
import numpy as np
N = 128
M = 20
M = 30
dx = 4/N
r = m.pdf_from_characteristic(N, M, dx)
r = m.pdf_from_characteristic(N, M, delta_x=4/N)
n = norm.pdf(r["x"], scale=m.std())
fig = px.line(r, x="x", y="y", markers=True)
fig.add_trace(go.Scatter(x=r["x"], y=n, name="normal", line=dict()))
Expand Down Expand Up @@ -95,3 +105,7 @@ s = implied_black_volatility(r["x"], r["y"], 1, initial_sigma=m.std())
fig = px.line(x=r["x"], y=s, markers=True, labels=dict(x="moneyness", y="implied vol"))
fig.show()
```

```{code-cell} ipython3
```
13 changes: 12 additions & 1 deletion notebooks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
# %%
import pandas as pd
import plotly.express as px
import plotly.io as pio

from quantflow.utils.marginal import Marginal1D
from quantflow.utils.paths import PLOTLY_THEME

pio.templates.default = PLOTLY_THEME


def chracteristic_fig(m: Marginal1D, N: int, max_frequency: float):
Expand All @@ -28,7 +33,13 @@ def chracteristic_fig(m: Marginal1D, N: int, max_frequency: float):
pd.DataFrame(dict(frequency=f, characteristic=c.imag, name="iamg")),
)
)
return px.line(df, x="frequency", y="characteristic", color="name", markers=True)
return px.line(
df,
x="frequency",
y="characteristic",
color="name",
markers=True,
)


# %%
Loading

0 comments on commit 91c74c7

Please sign in to comment.