Skip to content

Instantly share code, notes, and snippets.

View andreachello's full-sized avatar
:octocat:
Building

Andrea Chello andreachello

:octocat:
Building
View GitHub Profile
d_1_stock = (np.log(V_0/debt) + (risk_free + sigma**2/2)*(T))/(sigma_firm*np.sqrt(T))
d_2_stock = d_1_stock - sigma*np.sqrt(T)
analytic_callprice = S_0*norm.cdf(d_1_stock) - strike*np.exp(-risk_free*(T))*norm.cdf(d_2_stock)
d_1 = (np.log(V_0/debt) + (risk_free + sigma_firm**2/2)*(T))/(sigma_firm*np.sqrt(T))
d_2 = d_1 - sigma_firm*np.sqrt(T)
default_prob = norm.cdf(-d_2)
for i in range(len(corr_tested)):
correlation = corr_tested[i]
if (correlation == 1 or correlation == -1):
norm_vec_0 = norm.rvs(size = 50000)
norm_vec_1 = correlation*norm_vec_0
corr_norm_matrix = np.array([norm_vec_0, norm_vec_1])
else:
corr_matrix = np.array([[1, correlation], [correlation,1]])
def terminal_value(S_0, risk_free_rate, sigma, Z, T):
"""
Generates the terminal share price given some random normal values, Z
"""
return S_0*np.exp((risk_free_rate-sigma**2/2)*T+sigma*np.sqrt(T)*Z)
def call_payoff(S_T, K):
"""
Function for evaluating the call price in Monte Carlo Estimation
"""
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
import random as r
# Market Information
risk_free = 0.1
# Share Specific Information
S_0 = 100
# using historical returns to project future returns
for i in range(t_simulations):
rand_samp = uniform.rvs(size=365)*(len(price_path)-1)
rand_samp = [int(x) for x in rand_samp]
share_returns = hist_lret[rand_samp]
s_term = hist_s0*np.exp(np.sum(share_returns, axis=0))
hist_portret[i] = (np.sum(s_term) - hist_portval)/hist_portval
# Sorting portfolio returns
# current portfolio value as the sum of the most recent share price
hist_s0 = price_path[-1]
hist_portval = np.sum(hist_s0)
# initialize a vector to capture simulated portfolio returns
hist_portret = [None]*t_simulations
# determining historical log returns
hist_lret = np.log(price_path[1:]) - np.log(price_path[0: -1])
def share_path(S_0, risk_free_rate, sigma, Z, dT):
"""
Generates the terminal share price given some random normal values, Z
"""
return S_0*np.exp(np.cumsum((risk_free_rate - sigma**2/2)*dT + sigma*np.sqrt(dT)*Z,1))
# generate synthetic share data
Z_histdata = norm.rvs(size = [3,5*365])
corr_Z = np.transpose(np.matmul(L, Z_histdata))
# Calculating portfolio returns
port_return = (portval_future - portval_current)/portval_current
# Sorting the Returns
port_return = np.sort(port_return)
# Determining VaR
mVar_estimate = -port_return[int (np.floor(alpha*t_simulations))-1]
def terminal_shareprice(S_0, risk_free_rate, sigma, Z, T):
"""
Generates the terminal share price given some random normal values, z
"""
# It returns an array of terminal stock prices.
return S_0*np.exp((risk_free_rate-sigma**2/2)*T+sigma*np.sqrt(T)*Z)