Seite 1 von 1

Black-Scholes; Monte-Carlo-Sim

Verfasst: Donnerstag 3. September 2020, 13:21
von doki1994
Guten Tag,
kann mir bitte jemand helfen ?
Ich würde gerne mehrere Pfade auf einmal simulieren.
Ich habe hinbekommen einen Pfad zu simulieren...

#Black-Scholes-Dynamik
import math
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib as mlp
import matplotlib.pyplot as plt
from scipy.stats import norm
import statsmodels.api as sm
import pylab

def asset_price(S,drift,volatility,dt,X):
return S*math.exp((drift-(volatility**2)/2)*dt + volatility*math.sqrt(dt)*X)


S_0 = 18.49
drift = -0.0011963815
volatility = 0.02
dt = 1/252
N_days = 252


path_matrix = np.zeros((N_days+1,1))
path_matrix[0,0] = S_0

random_variable = np.random.randn(N_days)

for i in range(N_days):
path_matrix[i+1,0] = asset_price(path_matrix[i,0],drift,volatility,dt,random_variable)

path_matrix

plt.plot(path_matrix)

Re: Black-Scholes; Monte-Carlo-Sim

Verfasst: Freitag 4. September 2020, 10:52
von bords0
Schritt 1: Code in Code-Tags setzen (im Editor den Button </> anklicken). Die Vorschau ist dein Freund! Dann wird aus dem Index i nicht mehr "ab jetzt kursiv".
Schritt 2: Überflüssige Importe löschen

Schritt 3: Wo jetzt bei der Größe "...,1" steht, die 1 durch die Pfadanzahl ersetzen
Schritt 4: Wo jetzt bei der Größe "...,0" steht, will man mit der ganzen Zeile auf einmal rechnen - deshalb das ",0" einfach weglassen.
Schritt 5: math.exp kann nicht mit np.arrays umgehen - deshalb math.exp durch np.exp ersetzen.
Schritt 6: Damit die Matrix ausgegeben wird, muss man sie mit print ausgeben.

Ergebnis:

Code: Alles auswählen

#Black-Scholes-Dynamik
import math
import numpy as np
import matplotlib.pyplot as plt

def asset_price(S,drift,volatility,dt,X):
    return S*np.exp((drift-(volatility**2)/2)*dt + volatility*math.sqrt(dt)*X)


S_0 = 18.49
drift = -0.0011963815
volatility = 0.02
dt = 1/252
N_days = 252

NUMPATHS = 100  # Anzahl der Pfade

path_matrix = np.zeros((N_days+1, NUMPATHS))
path_matrix[0] = S_0

random_variable = np.random.randn(N_days, NUMPATHS)

for i in range(N_days):
    path_matrix[i + 1] = asset_price(path_matrix[i], drift,volatility, dt, random_variable[i])

print(path_matrix)

plt.plot(path_matrix)

Re: Black-Scholes; Monte-Carlo-Sim

Verfasst: Sonntag 6. September 2020, 14:42
von doki1994
Hey vielen vielen Dank !!
Sehr verständlich :)

Re: Black-Scholes; Monte-Carlo-Sim

Verfasst: Sonntag 6. September 2020, 23:59
von doki1994
Eine Frage wie kann man die Volatilität für jedes tag ersetzen mit (stdev+ 0.001*S_{t}) ersetzen S_{t} falls wir eine DAX-Aktie haben für einen Zeitraum von 252.
Falls wir 01.01.18-01.01-19 als historische Werte mit yahoofinance ziehen und dann Simulation für 01.01.19-01.01-20 machen wollen.