Skip to content

Commit

Permalink
Add plot to paths
Browse files Browse the repository at this point in the history
  • Loading branch information
lsbardel committed Jul 6, 2023
1 parent a875a52 commit 507db57
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
20 changes: 8 additions & 12 deletions notebooks/cir.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The model has a close-form solution for the mean and the variance

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

Expand All @@ -46,23 +46,18 @@ pr.is_positive
```

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

Sampling with a mean reversion speed five times larger

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

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

```{code-cell} ipython3
Expand All @@ -78,7 +73,8 @@ 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
dx = 8/N
import plotly.graph_objects as go
dx = 4/N
r = m.pdf_from_characteristic(N, M, dx)
fig = go.Figure()
fig.add_trace(go.Scatter(x=r["x"], y=r["y"], mode="markers", name="computed"))
Expand Down
11 changes: 10 additions & 1 deletion notebooks/dsp.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ kernelspec:

## Poisson Process

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

```{code-cell} ipython3
```

## Compound Poisson Process

Expand Down
4 changes: 1 addition & 3 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 Down
12 changes: 11 additions & 1 deletion quantflow/utils/paths.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from typing import Self
from typing import Any, Self

import numpy as np
import pandas as pd
from pydantic import BaseModel, ConfigDict, Field
from scipy.integrate import cumtrapz

try:
import plotly.express as px # type: ignore
except ImportError:
px = None


class Paths(BaseModel):
"""Paths of a stochastic process"""
Expand Down Expand Up @@ -48,3 +53,8 @@ def integrate(self) -> Self:
return self.__class__(
t=self.t, data=cumtrapz(self.data, dx=self.dt, axis=0, initial=0)
)

def plot(self) -> Any:
if px is None:
raise ImportError("plotly is not installed")
return px.line(self.data, title="Paths")

0 comments on commit 507db57

Please sign in to comment.