-
-
Save kvalv/1760e0de4ebeecefa1dfeec3f4acac6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def BarrierAmerican(self, n_steps, opt_type, H, American=False, comp_fun=None, kappa=None, var_mean=None, var_var=None, return_Hs=False): | |
| ''' | |
| For exercise 4c. | |
| ''' | |
| S, K, vol, r, T, div = self.S, self.K, self.vol, self.r, self.T, self.div | |
| S0 = S | |
| h = T / n_steps | |
| u = np.exp((r-div)*h + vol*np.sqrt(h)) | |
| d = np.exp((r-div)*h - vol*np.sqrt(h)) | |
| def P(i, n): | |
| if S0*u**i * d**(n-i) >= H: | |
| return 1 | |
| return np.exp((-2/((vol**2)*n*h))* abs(np.log(S0/H)*np.log((S0*(u**i)*(d**(n-i)))/H))) | |
| probs = np.array([P(i, n_steps-0) for i in range(n_steps+1)]) | |
| S_T = np.array([S0*u**i * d**(n_steps-i) for i in range(n_steps+1)]) | |
| V_T = np.array([max(s-K, 0) for s in S_T]) * (1 - probs) | |
| def replicating_portfolio_value(Vu, Vd): | |
| delta = np.exp(-div*h) * (Vu-Vd) / (S*(u-d)) | |
| B = np.exp(-r*h) * (u*Vd - d*Vu) / (u - d) | |
| return delta*S + B | |
| def step_backward(V_T): | |
| ''' | |
| V_T: a list of values for the option at step T. Two values next | |
| to each other represent the binomial values that they might have. | |
| returns: Option values for the underlying given prices at step T-1 | |
| ''' | |
| V_t = [] | |
| for Vu, Vd in zip(V_T, V_T[1:]): | |
| Vu, Vd = Vd, Vu | |
| V_t.append(replicating_portfolio_value(Vu, Vd)) | |
| V_t = np.array(V_t) | |
| return V_t | |
| for t in np.arange(n_steps)[::-1]: | |
| V_T = step_backward(V_T) | |
| # compare with immediate exercise | |
| S_T = [S0*u**i * d**(t-i) for i in range(t+1)] | |
| V_T = [max(v_t, (0 if s_t >= H else s_t - K)) for v_t, s_t in zip(V_T, S_T)] | |
| # import ipdb; ipdb.set_trace(); | |
| return V_T[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment