You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A quick and dirty syntax translation / conversion reference guide to ease the transition between Python and Julia. This is not meant as a reference to the language. For that you should read the manual.
Some important differences
Arrays in Julia are indexed starting from 1.
In Julia classes (i.e. types) don't own methods. Methods are implementations of generic functions and are invoked in a "static style", i.e. instead of Python's str1.rstrip(), we will have rstrip( str1 ), instead of file1.close(), close( file1 ).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Spot = 36
σ = 0.2
n = 100000
m = 10
K = 40
r = 0.06
T = 1
order = int(sys.argv[1])
Δt = T / m
zeros = np.zeros(n)
t_span = np.round(np.arange(Δt, T + Δt, Δt), 6)
def first_one_np(x):
import numpy as np
original = x
x = np.greater(x, 0.)
n_columns = x.shape[1]
batch_size = x.shape[0]
x_not = 1 - x
sum_x = np.minimum(np.cumprod(x_not, axis=1), 1.)
ones = np.ones((batch_size, 1))
lag = sum_x[:, :(n_columns - 1)]
lag = np.column_stack([ones, lag])
return original * (lag * x)
def chebyshev_basis(x, k):
B = {}
B[0] = np.ones_like(x)
B[1] = x
for n in range(2, k):
B[n] = 2 * x * B[n - 1] - B[n - 2]
return np.column_stack(list(B.values()))
def scale(x):
xmin = x.min()
xmax = x.max()
a = 2 / (xmax - xmin)
b = -0.5 * a * (xmin + xmax)
return a * x + b
import numpy as np
import time
import sys
Spot = 36
σ = 0.2
n = 100000
m = 10
K = 40
r = 0.06
T = 1
order = int(sys.argv[1])
Δt = T / m
zeros = np.zeros(n)
t_span = np.round(np.arange(Δt, T + Δt, Δt), 6)
def first_one_np(x):
import numpy as np
original = x
x = np.greater(x, 0.)
n_columns = x.shape[1]
batch_size = x.shape[0]
x_not = 1 - x
sum_x = np.minimum(np.cumprod(x_not, axis=1), 1.)
ones = np.ones((batch_size, 1))
lag = sum_x[:, :(n_columns - 1)]
lag = np.column_stack([ones, lag])
return original * (lag * x)
def chebyshev_basis(x, k):
B = {}
B[0] = np.ones_like(x)
B[1] = x
for n in range(2, k):
B[n] = 2 * x * B[n - 1] - B[n - 2]
def scale(x):
xmin = x.min()
xmax = x.max()
a = 2 / (xmax - xmin)
b = -0.5 * a * (xmin + xmax)
return a * x + b
==============================================================================
%% Specify the stochastic process
==============================================================================
def advance(S):
dB = np.sqrt(Δt) * np.random.normal(size=[n])
out = S + r * S * Δt + σ * S * dB
return out
def main():
S = {0.: Spot * np.ones([n])}
# poisson = {0.: 0.}
t0 = time.time()
main()
t1 = time.time()
print((t1 - t0) * 4 * 1000) # Multiply by four bc we need the greeks