def plot_price_LSMC(S_0, K, T, dt, mu, rf, sigma, paths):
    LSMC_call = []
    LSMC_put = []

    for S in np.linspace(S_0 * 0.8, S_0 * 1.2, 20):
        for type in ["put", "call"]:
            if type == "call":
                price_matrix = GBM(T, dt, paths, mu, sigma, S)
                LSMC_call.append(LSMC(price_matrix, K, rf, paths, T, dt, type))
            elif type == "put":
                price_matrix = GBM(T, dt, paths, mu, sigma, S)
                LSMC_put.append(LSMC(price_matrix, K, rf, paths, T, dt, type))

    plt.plot(np.linspace(S_0 * 0.8, S_0 * 1.2, 20),
             LSMC_call,
             "--",
             label="LSMC call")
    plt.plot(np.linspace(S_0 * 0.8, S_0 * 1.2, 20),
             LSMC_put,
             "--",
             label="LSMC put")

    plt.legend()
    plt.title("Value of the option - LSMC")
    plt.xlabel("Asset price, St")
    plt.ylabel("Option value")
    plt.show()
def plot_volatility_GBMvsLSMC(S_0, K, T, dt, mu, r, q, sigma, paths):
    LSMC_call = []
    LSMC_put = []
    BSM_call = []
    BSM_put = []

    for sigma in np.linspace(0, sigma * 2, 20):
        for type in ["put", "call"]:
            if type == "call":
                price_matrix = GBM(T, dt, paths, mu, sigma, S_0)
                LSMC_call.append(LSMC(price_matrix, K, r, paths, T, dt, type))
            elif type == "put":
                price_matrix = GBM(T, dt, paths, mu, sigma, S_0)
                LSMC_put.append(LSMC(price_matrix, K, r, paths, T, dt, type))
        call, put = BSM(S_0, K, r, q, sigma, T)
        BSM_put.append(put)
        BSM_call.append(call)

    plt.plot(np.linspace(0, sigma * 2, 20), LSMC_call, "--", label="LSMC call")
    plt.plot(np.linspace(0, sigma * 2, 20),
             BSM_call,
             label="BSM call",
             alpha=0.7)
    plt.plot(np.linspace(0, sigma * 2, 20), LSMC_put, "--", label="LSMC put")
    plt.plot(np.linspace(0, sigma * 2, 20),
             BSM_put,
             label="BSM put",
             alpha=0.7)

    plt.legend()
    plt.title("Analytical solutions BSM vs LSMC of european option")
    plt.xlabel("Volatility")
    plt.ylabel("Option value")
    plt.show()
def plot_strike_LSMC(S_0, K, T, dt, mu, rf, sigma, paths):
    price_matrix = GBM(T, dt, paths, mu, sigma, S_0)
    LSMC_call = []
    LSMC_put = []

    for K in np.linspace(K - K / 4, K + K / 4, 20):
        for type in ["put", "call"]:
            if type == "call":
                LSMC_call.append(LSMC(price_matrix, K, rf, paths, T, dt, type))
            elif type == "put":
                LSMC_put.append(LSMC(price_matrix, K, rf, paths, T, dt, type))

    plt.plot(np.linspace(K - K / 4, K + K / 4, 20),
             LSMC_call,
             "--",
             label="LSMC call")
    plt.plot(np.linspace(K - K / 4, K + K / 4, 20),
             LSMC_put,
             "--",
             label="LSMC put")

    plt.legend()
    plt.title("Strike price vs option value - LSMC")
    plt.xlabel("Strike price")
    plt.ylabel("Option value")
    plt.show()
def convergence_american_perpetual(T, dt, paths, mu, sigma, S_0, type):
    T = T + 1
    lsmc_call = []
    confidence_interval_up = []
    confidence_interval_down = []
    x = np.linspace(1, T, 14)

    for T in x:
        # slice = int(T*dt)
        price_matrix = GBM(T, dt, paths, mu, sigma, S_0)
        val = LSMC(price_matrix, K, rf, paths, T, dt, type)
        lsmc_call.append(val)
        # confidence_interval_up.append(val + 1.96 * st_dev / np.sqrt(paths))
        # confidence_interval_down.append(val - 1.96 * st_dev / np.sqrt(paths))

    lsmc_call = np.array(lsmc_call, dtype=float)

    def func(x, a, b):
        return a * np.log(x) + b

    popt, pcov = curve_fit(func, x, lsmc_call)
    plt.plot(sorted(x), func(sorted(x), *popt), "k", linestyle="dashed")

    plt.plot(x, lsmc_call, "x", c="skyblue")
    # plt.fill_between(x, confidence_interval_up, confidence_interval_down, "b", alpha=0.1)
    plt.axhline(y=perpetual_american(K, S_0, q, r, sigma), c="r")
    plt.title("Convergence of the LSMC to perpetual American option")
    plt.xlabel("Years")
    plt.ylabel("Option value")
    plt.plot()
    plt.show()
def plot_volatility_LSMC(S_0, K, T, dt, mu, rf, sigma, paths):
    LSMC_call = []
    LSMC_put = []

    for sigma in np.linspace(0, sigma * 2, 20):
        for type in ["put", "call"]:
            if type == "call":
                price_matrix = GBM(T, dt, paths, mu, sigma, S_0)
                LSMC_call.append(LSMC(price_matrix, K, rf, paths, T, dt, type))
            elif type == "put":
                price_matrix = GBM(T, dt, paths, mu, sigma, S_0)
                LSMC_put.append(LSMC(price_matrix, K, rf, paths, T, dt, type))

    plt.plot(np.linspace(0, sigma * 2, 20), LSMC_call, "--", label="LSMC call")
    plt.plot(np.linspace(0, sigma * 2, 20), LSMC_put, "--", label="LSMC put")

    plt.legend()
    plt.title("Volatility vs option value - LSMC")
    plt.xlabel("Volatility")
    plt.ylabel("Option value")
    plt.show()
def plot_maturity_LSMC(S_0, K, T, dt, mu, rf, sigma, paths):
    LSMC_call = []
    LSMC_put = []

    for time in np.linspace(T, T * 4, 20, dtype=int):
        for type in ["put", "call"]:
            if type == "call":
                price_matrix = GBM(time, dt, paths, mu, sigma, S_0)
                LSMC_call.append(
                    LSMC(price_matrix, K, rf, paths, time, dt, type))
            elif type == "put":
                price_matrix = GBM(time, dt, paths, mu, sigma, S_0)
                LSMC_put.append(
                    LSMC(price_matrix, K, rf, paths, time, dt, type))

    plt.plot(np.linspace(0, T * 4, 20), LSMC_call, "--", label="LSMC call")
    plt.plot(np.linspace(0, T * 4, 20), LSMC_put, "--", label="LSMC put")

    plt.legend()
    plt.title("Time to maturity vs option value - LSMC")
    plt.xlabel("Maturity")
    plt.ylabel("Option value")
    plt.show()
def american_perpetual2(S_0, K, q, r, sigma, T, dt, paths, mu, type):
    S = np.linspace(S_0 * 0.7, S_0 * 1.3, 10)
    lsmc = []
    perp = []
    for s in S:
        price_matrix = GBM(T, dt, paths, mu, sigma, s)
        val = LSMC1(price_matrix, K, rf, paths, T, dt, type)
        lsmc.append(val)

        x = perpetual_american(K, s, q, r, sigma)
        perp.append(x)

    plt.plot(S, perp, label="analytical")
    plt.plot(S, lsmc, label="continuation value")
    plt.xlim(S_0 * 0.7 - 10, S_0 * 1.3 + 10)
    plt.ylim(40, 130)
    plt.xlabel("Stock price")
    plt.ylabel("Option value")
    plt.legend()
    plt.show()
def american_vs_european(S_0, K, T, dt, mu, rf, sigma, paths):
    LSMC_call = []
    LSMC_put = []
    BSM_call = []
    BSM_put = []

    for S in np.linspace(S_0 * 0.8, S_0 * 1.2, 20):
        for type in ["put", "call"]:
            price_matrix = GBM(T, dt, paths, mu, sigma, S)
            if type == "call":
                val = LSMC(price_matrix, K, rf, paths, T, dt, type)
                LSMC_call.append(val)
            elif type == "put":
                val = LSMC(price_matrix, K, rf, paths, T, dt, type)
                LSMC_put.append(val)
        call, put = BSM(S, K, rf, q, sigma, T)
        BSM_put.append(put)
        BSM_call.append(call)

    plt.plot(np.linspace(S_0 - S_0 / 2, S_0 + S_0 / 2, 20),
             LSMC_call,
             "--",
             label="LSMC call")
    plt.plot(np.linspace(S_0 - S_0 / 2, S_0 + S_0 / 2, 20),
             BSM_call,
             label="BSM call",
             alpha=0.5)
    plt.plot(np.linspace(S_0 - S_0 / 2, S_0 + S_0 / 2, 20),
             LSMC_put,
             "--",
             label="LSMC put")
    plt.plot(np.linspace(S_0 - S_0 / 2, S_0 + S_0 / 2, 20),
             BSM_put,
             label="BSM put",
             alpha=0.5)

    plt.legend()
    plt.title("European option - BSM formulas vs LSMC algorithm")
    plt.ylabel("Option value")
    plt.xlabel("Asset price, St")
    plt.show()
Exemple #9
0
from LSMC.LSMC_faster import LSMC
from LSMC.LSMC_faster import GBM
import pandas as pd

K = 40
dt = 50
paths = 50000
mu = 0.06
r = 0.06
value = []

for S in [36, 44]:
    for T in [1, 2]:
        for sigma in [0.2, 0.4]:
            for i in range(5):
                price_matrix = GBM(T, dt, paths, mu, sigma, S)
                value.append(LSMC(price_matrix, K, r, paths, T, dt, "put"))

df = pd.DataFrame(data=value)
df.to_excel("data_table2.xlsx")
Exemple #10
0
sigma_MR = 0.15

#MR1 = MR1(T, dt, paths, sigma_MR, S_0, theta, Sbar)
#MR2 = MR2(T, dt, paths, sigma_MR, S_0, theta, Sbar)
#GBM = GBM(T, dt, paths, mu_GBM, sigma_GBM, S_0)
# MR3 = MR3(T, dt, paths, sigma_g, sigma_e, S_0, theta_e, theta_g, Sbar, LR_0)

#plt.plot(np.linspace(0, N, N+1), MR1, label="MR1", c="y", alpha=0.2)
#plt.plot(np.linspace(0, N, N+1), MR2, label="MR2", c="b", alpha=0.2)
#plt.plot(np.linspace(0, N+1, N+1), GBM, label="GBM", c="r", alpha=0.1)
# plt.plot(np.linspace(0, N+1, N+1), MR3, label="MR3", c="y")

sigma = 0.20
mu = 0.00
S_0 = 10
Sbar = 10
theta = 0.2

GBM, MR = MRvsGBM(T, dt, paths, sigma, S_0, theta, Sbar, mu)
df = pd.DataFrame(columns=["GBM", "MR"])

df["GBM"] = GBM.tolist()
df["MR"] = MR.tolist()

plt.plot(np.linspace(0, N + 1, N + 2), GBM, label="GBM")
plt.plot(np.linspace(0, N + 1, N + 2), MR, label="MR")
plt.title("MR vs GBM")

plt.legend()
plt.show()
df.to_excel("GBMvsMR.xlsx")
for i in range(len(df["K"])):
    # implied volatility based on European option since BSM
    imp_vol.append(iv(df["market price"][i], S_0, df["K"][i], T, mu, "c"))

vol_smile = dict(zip(df["K"], imp_vol))

for K in vol_smile:
    strike_price = K
    volatility = vol_smile[K]

    # option values using Black Scholes Merton --> thus European option
    call, put = BSM(S_0, strike_price, r, q, volatility, T)
    ov_BSM.append(call)

    # option values using Finite Differencing --> American option
    stocks, call_prices = American_call_grid(S_0, T, r, volatility, q, steps, K)
    ov_FD.append(call_prices[steps])

    # option values using Least Squares Monte Carlo --> American option
    price_matrix = GBM(T, dt, paths, mu, volatility, S_0)
    ov_LSMC.append(LSMC(price_matrix, K, r, paths, T, dt, "call"))


ov = pd.DataFrame(columns=["actual price", "K", "BSM", "FD", "LSMC"])
ov["actual price"] = df["market price"]
ov["K"] = df["K"]
ov["BSM"] = ov_BSM
ov["FD"] = ov_FD
ov["LSMC"] = ov_LSMC

df.to_excel("ov_compare.xlsx")