<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 iPython Cookbook - Monte Carlo Pricing I - Call (Normal model)
Pricing a call option with Monte Carlo (Normal model)
Step1:  Those are our option and market parameters
Step2:  We now define our payoff function using a closure
Step3:  We now generate a set of Standard Gaussian variables $z$ as a basis for our simulation...
Step4:  ...and transform it in a Gaussian variable with the right mean and standard deviation, ie a variable that is distributed according to $N(forward,vol)$
Step5:  We first look at the histogram of the spot prices $x$
Step6:  We now determine the payoff values from our draws of the final spot price. Note that we need to use the map command rather than simply writing po = payoff(x). The reason for this is that this latter form is not compatible with the if statement in our payoff function. We also already compute the forward value of the option, which is simply the average payoff over all simulations.
Step7:  Now we produce the histogram of the payoffs
Step8:  In the next step we compute our "Greeks", ie a number of derivatives of the forward value with respect to the underlying parameters. What is crucial here is that those derivative are calculated on the same draw random numbers $z$, otherwise the Monte Carlo sampling error will dwarf the signal. The sensitivities we compute are to increase / decrease the forward by one currency unit (for Delta and Gamma) and to increase the volatility by one currency unit (for Vega)
Step9:  Licence and version
(c) Stefan Loesch / oditorium 2014; all rights reserved 
(license)
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import matplotlib.pyplot as plt
Explanation: iPython Cookbook - Monte Carlo Pricing I - Call (Normal model)
Pricing a call option with Monte Carlo (Normal model)
End of explanation
strike   = 100
forward  = 110
vol      = 50
Explanation: Those are our option and market parameters: the exercise price of the option strike, the forward price of the underlying security forward and its volatility vol (note that we are using a Gaussian model here, not a lognormal one, so the volatility number is in units of currency, not in percent)
End of explanation
def call(k=100):
    def payoff(spot):
        if spot > k:
            return spot - k
        else:
            return 0
    return payoff
payoff = call(k=strike)
#payoff(110)
Explanation: We now define our payoff function using a closure: the variable payoff represents a function with one parameter spot with the strike k being frozen at whatever value it had when the outer function call was called to set payoff
End of explanation
N = 10000
z = np.random.standard_normal((N))
#z
Explanation: We now generate a set of Standard Gaussian variables $z$ as a basis for our simulation...
End of explanation
x = forward + vol * z
#x
Explanation: ...and transform it in a Gaussian variable with the right mean and standard deviation, ie a variable that is distributed according to $N(forward,vol)$
End of explanation
hg0=np.histogram(x, bins=50)
xvals0 = np.delete(hg0[1], -1)
plt.bar(xvals0,hg0[0], width=0.5*(xvals0[1]-xvals0[0]))
plt.title('forward distribution')
plt.xlabel('forward')
plt.ylabel('occurrences')
plt.show()
Explanation: We first look at the histogram of the spot prices $x$
End of explanation
po = list(map(payoff,x))
fv = mean(po)
#po
Explanation: We now determine the payoff values from our draws of the final spot price. Note that we need to use the map command rather than simply writing po = payoff(x). The reason for this is that this latter form is not compatible with the if statement in our payoff function. We also already compute the forward value of the option, which is simply the average payoff over all simulations.
End of explanation
hg = np.histogram(po,bins=50)
xvals = np.delete(hg[1], -1)
plt.bar(xvals,hg[0], width=0.9*(xvals[1]-xvals[0]))
plt.title('payout distribution')
plt.xlabel('payout')
plt.ylabel('occurrences')
plt.show()
Explanation: Now we produce the histogram of the payoffs
End of explanation
x = forward + 1 + vol * z
po = list(map(payoff,x))
fv_plus = mean(po)
x = forward - 1 + vol * z
po = list(map(payoff,x))
fv_minus = mean(po)
x = forward + (vol + 1) * z
po = list(map(payoff,x))
fv_volp = mean(po)
print ("Forward     =  %f" % forward)
print ("Strike      =  %f" % strike)
print ("Volatility  =  %f" % vol)
print ("PV          =  %f" % fv)
print ("Delta       =  %f" % ((fv_plus - fv_minus)/2))
print ("Gamma       =  %f" % ((fv_plus + fv_minus - 2 * fv)))
print ("Vega        =  %f" % ((fv_volp - fv)))
Explanation: In the next step we compute our "Greeks", ie a number of derivatives of the forward value with respect to the underlying parameters. What is crucial here is that those derivative are calculated on the same draw random numbers $z$, otherwise the Monte Carlo sampling error will dwarf the signal. The sensitivities we compute are to increase / decrease the forward by one currency unit (for Delta and Gamma) and to increase the volatility by one currency unit (for Vega)
End of explanation
import sys
print(sys.version)
Explanation: Licence and version
(c) Stefan Loesch / oditorium 2014; all rights reserved 
(license)
End of explanation
<END_TASK>