Skip to content

Instantly share code, notes, and snippets.

@rayh
Last active September 17, 2019 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayh/e9a36d6fb33e484f5502d95b109ce606 to your computer and use it in GitHub Desktop.
Save rayh/e9a36d6fb33e484f5502d95b109ce606 to your computer and use it in GitHub Desktop.
import kozai.compute as kc
cluster = kc.create_dask_cluster()
dask_client = cluster.client()
print(dask_client)
import dask.bag as db
import numpy as np
from dask.distributed import Client, progress
def random_walk(s0, mu, sigma, days):
dt = 1/365.
prices = np.zeros(days)
shocks = np.zeros(days)
prices[0] = s0
for i in range(1, days):
e = np.random.normal(loc=mu * dt, scale=sigma * np.sqrt(dt))
prices[i] = prices[i-1] * (1 + e)
return prices
s0 = 100
K = 100
mu = 0.02
sigma = 0.2
days = 365*4
n = 10000
try:
result = db \
.from_sequence(range(0, n), npartitions=int(n / 100)) \
.map(lambda _: max(
0,
np.average(random_walk(s0, mu, sigma, days)) - K
)) \
.to_dataframe(columns=['max']) \
['max'].mean()
progress(result)
print(result.compute())
finally:
cluster.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment