<jupyter_start><jupyter_text>Brownian Motion<jupyter_code>T = 1.0
N = 1000
dt = T/N
dW = zeros(N)
W = zeros(N)

dW = sqrt(dt) * random.normal(size=N)
W = dW.cumsum()
plot(W)
T = 1.0
N = 1000
M = 10
dt = T/N
dW = zeros((N, M))
W = zeros((N, M))

random.seed(100)

for i in range(M):
    dW = sqrt(dt) * random.normal(size=N)
    W[:,i] = dW.cumsum()
    plot(W[:,i])<jupyter_output><empty_output><jupyter_text>Function of W(t)<jupyter_code>T = 1.0
N = 100
M = 500
dt = T/N
dW = zeros((N, M))
W = zeros((N, M))

t = linspace(0,T,N)
U = W.copy()
for i in range(M):
    dW = sqrt(dt) * random.normal(size=N)
    W[:,i] = dW.cumsum()
    U[:,i] = exp(t + 0.5 * W[:,i])

for i in random.choice(arange(M),size=5):
    plot(t, U[:,i])
plot(t, U.mean(axis=1), lw=5)
plot(t, U.mean(axis=1))
plot(t, exp(9.0/8.0 * t))
print norm((U.mean(axis=1)- exp(9.0/8.0 * t)),ord=inf)
plot(t, abs(U.mean(axis=1) - exp(9.0/8.0 * t)))<jupyter_output><empty_output><jupyter_text>Ito and Stratonovich Integrals<jupyter_code>T = 1.0
N = 1000
dt = T/N
dW = zeros(N)
W = zeros(N)

dW = sqrt(dt) * random.normal(size=N)
W = dW.cumsum()
W = insert(W,0,0)

ito = sum(W[0:-1] * dW)

strat = sum((0.5 * (W[0:-1] + W[1:]) + 0.5 * sqrt(dt) * random.normal(size=N)) * dW)

itoerr = abs(ito - 0.5*(W[-1]**2-T))
straterr = abs(strat - 0.5 * W[-1]**2)

print "Ito:", ito
print "Strat:", strat
print "Error of Ito:",itoerr
print "Error of Strat:", straterr<jupyter_output>Ito: 0.284702555399
Strat: 0.79074137579
Error of Ito: 0.00318051864766
Error of Strat: 0.00921933903828<jupyter_text>Euler Maruyama para la Ec. Black-Scholes.<jupyter_code>gamma=2
mu=1
Xzero=1

T=1
N=2**7
dt = float(T)/N
t = linspace(0,T,N+1)

dW = sqrt(dt) * random.randn(1,N)
W = cumsum(dW)

Xtrue = Xzero * exp((gamma-0.5*mu**2)*t[1:] + mu* W)
Xtrue = insert(Xtrue,0,Xzero)

figure(figsize=(8,8))
plot(t,Xtrue)

R=4
Dt = R*dt
L = float(N)/R
Xem = zeros(L+1) # Euler Maruyama Solution 
Xem[0] = Xzero

for j in range(1,int(L)+1):
    Winc = sum(dW[0][range(R*(j-1), R*j)]) # subsample
    Xem[j] = Xem[j-1] + Dt * gamma * Xem[j-1] + mu * Xem[j-1] * Winc

emerr=np.abs(Xem[-1]-Xtrue[-1])
print "Error at endpoint: ", emerr

plot(np.linspace(0,T,L+1),Xem,'r--*')<jupyter_output>Error at endpoint:  0.0321733814685
{'fraction_non_alphanumeric': 0.22237076648841356, 'fraction_numerical': 0.07575757575757576, 'mean_word_length': 4.184757505773672, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '175150', 'license': 'MIT', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1139, 'n_words': 224, 'path': 'Metodos Computacionales Avanzados/weeks/06/code/stoch.ipynb', 'repo_name': 'aess14/Cursos-Uniandes', 'size': '2244'}