-
-
Notifications
You must be signed in to change notification settings - Fork 484
/
pn_model.py
59 lines (50 loc) · 2.19 KB
/
pn_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import hvplot
import hvplot.pandas
import numpy as np
import pandas as pd
import param
import panel as pn
class GBM(param.Parameterized):
# interface
mean = param.Number(default=5, bounds=(.0, 25.0))
volatility = param.Number(default=5, bounds=(.0, 50.0))
maturity = param.Integer(default=1, bounds=(0, 25))
n_observations = param.Integer(default=10, bounds=(2, 100))
n_simulations = param.Integer(default=20, bounds=(1, 500))
refresh = pn.widgets.Button(name="Refresh", button_type='primary')
def __init__(self, **params):
super(GBM, self).__init__(**params)
# update the plot for every changes
# @param.depends('mean', 'volatility', 'maturity', 'n_observations', 'n_simulations', watch=True)
# refresh the plot only on button refresh click
@param.depends('refresh.clicks')
def update_plot(self, **kwargs):
df_s = pd.DataFrame(index=range(0, self.n_observations))
for s in range(0, self.n_simulations):
name_s = f"stock_{s}"
df_s[name_s] = self.gbm(spot=100,
mean=self.mean/100,
vol=self.volatility/100,
dt=self.maturity / self.n_observations,
n_obs=self.n_observations)
return df_s.hvplot(grid=True, colormap='Paired', value_label="Level", legend=False)
@staticmethod
def gbm(spot: float, mean: float, vol: float, dt: float, n_obs: int) -> np.ndarray:
""" Geometric Brownian Motion
:param spot: spot value
:param mean: mean annualised value
:param vol: volatility annualised value
:param dt: time steps
:param n_obs: number of observation to return
:return: a geometric brownian motion np.array()
"""
# generate normal random
rand = np.random.standard_normal(n_obs)
# initialize the parameters
S = np.zeros_like(rand)
S[0] = spot
# loop to generate the brownian motion
for t in range(1, n_obs):
S[t] = S[t - 1] * np.exp((mean - (vol ** 2) / 2) * dt + vol * rand[t] * np.sqrt(dt))
# return the geometric brownian motion
return S