Skip to content

Instantly share code, notes, and snippets.

@andreachello
Created May 17, 2022 01:28
Show Gist options
  • Save andreachello/db5e9ec9ab601129265ab3099b8ef628 to your computer and use it in GitHub Desktop.
Save andreachello/db5e9ec9ab601129265ab3099b8ef628 to your computer and use it in GitHub Desktop.
def get_barrier_price(strike_num):
for i in range(1,num_steps+1):
# fill out the first value with our initial stock price
term_val[i-1][0] = np.full((num_simulations*i), S_0)
for j in range (1,num_of_months):
# update current month to reflect the monthly simulation we are currently in
current_month = (j-1)/12
norm_array = norm.rvs(size = num_simulations*i)
term_val[i-1][j] = terminal_shareprice(term_val[i-1][j-1],risk_free,sigma,norm_array,dT)
# Compute discounted barrier Price of the option
mbarrier_val[i-1] = discounted_call_payoff(term_val[i-1][12],strike,risk_free,T-current_time)
# use the above formula to calculate the values of the barrier option
## get array of booleans for when stock is knocked out or not
knock_out_array = (np.max(term_val[i-1],axis = 0) < barrier)
## times it by the value of the previously calculated barrier option
mbarrier_val[i-1] = mbarrier_val[i-1] * knock_out_array
# compute mean and standard deviation of entire path
mbarrier_estimates[i-1] = np.mean(mbarrier_val[i-1])
mbarrier_std[i-1] = np.std(mbarrier_val[i-1]/np.sqrt(i*num_simulations))
return np.mean(mbarrier_estimates)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment