In [1]:
%load_ext autoreload
%autoreload 2
In [2]:
import pandas as pd
In [3]:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
In [4]:
r = np.random.randn((1000))
S0 = 1
S = np.cumsum(r) + S0
In [7]:
T = 2
mu = 0.
sigma = 0.01
S0 = 20
dt = 0.01
N = round(T/dt)
t = np.linspace(0, T, N)
W = np.random.standard_normal(size = N)
W = np.cumsum(W)*np.sqrt(dt) ### standard brownian motion ###
X = (mu-0.5*sigma**2)*t + sigma*W
S = S0*np.exp(X) ### geometric brownian motion ###
plt.plot(t, S)
plt.show()
In [75]:
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
In [10]:
from blackscholes import geometric_brownian_motion, blackScholes
from scipy.stats import norm
In [14]:
geometric_brownian_motion(mu=0., sigma=0.01, s0=1, dt=0.01);
In [12]:
t = 2.
dt = 0.01
N = int(round(t / dt))
np.linspace(0, t, N)
tt = np.linspace(0, t, N)
W = norm((N))
In [13]:
@interact(mu=(-0.02, 0.05, 0.01), sigma=(0.01, 0.1, 0.005), S0=(1,100,10), dt=(0.001, 0.1, 0.001))
def plot_gbm(mu, sigma, S0, dt):
s, t = geometric_brownian_motion(mu=mu, sigma=sigma, t=2, dt=dt, s0=S0)
pd.Series(t, s).plot()
plt.show()
In [16]:
df.ix[0.1:,:].gamma.plot()
In [195]:
tau = np.clip( np.linspace(1.0, .0, 101), 0.0000001, 100)
S = 1.
K = 1.
sigma = 1
df = pd.DataFrame.from_dict(blackScholes(tau, S, K, sigma))
df.index = tau
In [ ]:
@interact(mu=(-0.02, 0.05, 0.01), sigma=(0.01, 0.1, 0.005), S0=(1,100,10), dt=(0.001, 0.1, 0.001))
def plot_gbm(mu, sigma, S0, dt):
s, t = geometric_brownian_motion(mu=mu, sigma=sigma, t=2, dt=dt, s0=S0)
pd.Series(t, s).plot()
plt.show()
In [6]:
import td
In [10]:
import scipy as sp
In [ ]:
In [11]:
α = 0.05
γ = 0.1
td_learning = td.TD(α, γ)
In [12]:
d_1 = lambda σ, T, t, S, K: 1. / σ / np.sqrt(T - t) * (np.log(S / K) + 0.5 * (σ ** 2) * (T-t))
d_2 = lambda σ, T, t, S, K: 1. / σ / np.sqrt(T - t) * (np.log(S / K) - 0.5 * (σ ** 2) * (T-t))
call = lambda σ, T, t, S, K: S * sp.stats.norm.cdf( d_1(σ, T, t, S, K) ) - K * sp.stats.norm.cdf( d_2(σ, T, t, S, K) )
In [13]:
plt.plot(np.linspace(0.1, 4., 100), call(1., 1., .9, np.linspace(0.1, 4., 100), 1.))
Out[13]:
In [14]:
d_1(1., 1., 0., 1.9, 1)
Out[14]:
In [15]:
plt.plot(d_1(1., 1., 0., np.linspace(0.1, 2.9, 10), 1))
Out[15]:
In [16]:
plt.plot(np.linspace(0.01, 1.9, 100), sp.stats.norm.cdf(d_1(1., 1., 0.2, np.linspace(0.01, 1.9, 100), 1)))
plt.plot(np.linspace(0.01, 1.9, 100), sp.stats.norm.cdf(d_1(1., 1., 0.6, np.linspace(0.01, 1.9, 100), 1)))
plt.plot(np.linspace(0.01, 1.9, 100), sp.stats.norm.cdf(d_1(1., 1., 0.9, np.linspace(0.01, 1.9, 100), 1)))
plt.plot(np.linspace(0.01, 1.9, 100), sp.stats.norm.cdf(d_1(1., 1., 0.99, np.linspace(0.01, 1.9, 100), 1)))
Out[16]:
In [17]:
def iterate_series(n=1000, S0 = 1):
while True:
r = np.random.randn((n))
S = np.cumsum(r) + S0
yield S, r
In [18]:
def iterate_world(n=1000, S0=1, N=5):
for (s, r) in take(N, iterate_series(n=n, S0=S0)):
t, t_0 = 0, 0
for t in np.linspace(0, len(s)-1, 100):
r = s[int(t)] / s[int(t_0)]
yield r, s[int(t)]
t_0 = t
In [17]:
from cytoolz import take
In [18]:
import gym
import gym_bs
In [19]:
from test_cem_future import *
In [20]:
import pandas as pd
import numpy as np
In [38]:
# df.iloc[3] = (0.2, 1, 3)
df
Out[38]:
In [34]:
rwd, df, agent = noisy_evaluation(np.array([0.1, 0, 0]))
rwd
df
agent;
In [41]:
env.observation_space
Out[41]: