# R script for option pricing using a two state model # Written for the class Mathematics of Finance, fall 2015 # http://www.math.nyu.edu/faculty/goodman/teaching/MathFin/index.html # Author: Jonathan Goodman cat("Binary one period put option pricer\n") # Have the user type in the parameters of the put. # Give the user a prompt for each parameter then read it in as a string cat("Enter the spot price of the stock") S_0_String <- readLines(con=stdin(),1) cat("Enter the strike price of the put option") K_String <- readLines(con=stdin(),1) cat("Enter the risk free interest rate, in percent per year") r_String <- readLines(con=stdin(),1) cat("Enter the time to maturity, in years") T_String <- readLines(con=stdin(),1) # as.numeric(string) returns the number described by the string S_0 = as.numeric(S_0_String) K = as.numeric(K_String) r = as.numeric(r_String) T = as.numeric(T_String) u = 1.2 # get these from the user too d = .8 S_u = u*S_0 # S_u is the price at time T if the stock goes up S_d = d*S_0 # S_d is the price at time T if the stock goes down P_u = max( 0, K - S_u) # Cash flows for the put at expiration in the ... P_d = max( 0, K - S_d) # ... up and down states. B_T = exp(.01*r*T) # The value of the risk free asset at time T, if B_0 = 1 q_u = .3 # Risk neutral probabilities -- put in the correct formulas q_d = .7 # The put price is the discounted expected cash flow in the risk neutral measure f = ( q_u*P_u + q_d*P_d)/B_T C1 = sprintf("Spot price is S_0 = %6.2f\n", S_0) # C1 is confirmation line 1 C2 = sprintf("Strike price is K = %6.2f\n", K) # add confirmation lines for ... C5 = sprintf("The put price is f = %6.2f\n", f) # other variables cat( C1) cat( C2) cat( C5)