Pricing Barrier Options Using Monte Carlo Methods
Pricing Barrier Options Using Monte Carlo Methods
080014353
080005791
080014940
040004438
Barrier Options
Computational Finance
EC5704
Spring 2009
Table of Contents
Introduction ........................................................................................................................ 3
Background ......................................................................................................................... 3
In + Out = Vanilla............................................................................................................. 3
Volatility .......................................................................................................................... 5
Analytical Solutions......................................................................................................... 6
Monte Carlo Simulation...................................................................................................... 7
Simulation and Path Generation..................................................................................... 7
Calculating Payoffs.......................................................................................................... 9
Challenges ..................................................................................................................... 11
Variance Reduction Techniques ....................................................................................... 11
Antithetic Variates ........................................................................................................ 12
Control Variates ............................................................................................................ 13
Results............................................................................................................................... 14
Error Analysis ................................................................................................................ 15
Conclusion......................................................................................................................... 17
Appendix A: Results .......................................................................................................... 18
Appendix B: Code.............................................................................................................. 19
Introduction
This paper analyses the pricing of barrier options using Monte Carlo methods. Variance
reduction techniques are also analysed and implemented in the pricing of various
barrier options. The corresponding error is also investigated to reveal any possible
relationships between the simulation parameters and the resulting error.
Background
A barrier option is a type of path-dependent option where the payoff is determined by
whether or not the price of the stock hits a certain level during the life of the option.
There are several different types of barrier options. We consider two general types of
barrier options, ‘in’ and ‘out’ options. An ‘out’ option only pays off if the stock does not
hit the barrier level throughout the life of the option. If the stock hits a specified
barrier, then it has knocked out and expires worthless. A knock-in option on the other
hand only pays out if the barrier is crossed during the life of the option. For each knock-
out and knock-in option, we can have a down or an up option.
Knock-out Knock-in
Up and Out Up and In
Call
Down and Out Down and In
Up and Out Up and In
Put
Down and Out Down and In
We therefore have four different basic barrier options each having the typical payoffs.
It is worth pointing out that the relationship between the barrier and spot price
indicates whether the option is an up option or down option. If the barrier price is
above the spot price, then the option is an up option; if the barrier is below the spot
price, then it is a down option.
In + Out = Vanilla
For a given set of parameters, a vanilla option can be replicated by combining an in and
an out option of the same type. For example, one could combine a down and out call
with a down and in call to replicate a plain vanilla call. These are equivalent due to the
fact that if one option gets knocked out, the other is knocked in and vise versa.
This idea is illustrated in the figure below by graphing the values for an up and out put
and an up and in put using various barrier levels. The vanilla put was valued with the
same parameters using MATLAB’s built in Black-Scholes formula for pricing options,
blsprice.
Barrier Option
4.5
Up and Out
3.5 Up and In
Vanilla Put
2.5
Option Price
1.5
0.5
-0.5
50 55 60 65 70 75
Barrier
Figure 1: Spot: $50, Strike: $50, Interest Rate: 10%, Time to Expiry: 5 Months, Volatility:
40% with barriers ranging from $50-$75
It is quite clear that for any given barrier level, adding the value of an up and out put
and an up and in put totals to the value of a vanilla put. It is also apparent that both
barrier options are cheaper than their vanilla counterpart. This is because barrier
options limit the potential of either getting in the money or staying in the money,
thereby reducing the value relative to a vanilla option.
The figure also illustrates the relationship between the option price and barrier level.
For a knock-out option, increasing the absolute difference between the barrier level and
the initial spot price has the effect of increasing the value of the option. As the absolute
difference increases, the value of the option converges towards the value of a plain
vanilla option. This is simply because the probability of the option knocking out
approaches zero as the absolute difference increases. For a knock-in option, increasing
the absolute difference has the opposite effect. The probability of the option knocking
in approaches zero as the absolute difference increase thus making the option
worthless.
Volatility
Volatility plays an important role in valuing options. Barrier options are especially
sensitive to volatility. For knock-out options, increased volatility has the effect of
decreasing the option value as knock-out becomes more probable. Knock-in options
however increase in price with increased volatility as knock-in becomes more probable.
4.5
3.5
2.5
Option Value
1.5
0.5
-0.5
60
55 0.3
0.25
50
0.2
45 0.15
0.1
40 0.05
Spot Price
Volatility
For the up and out call option, the graph seems quite odd. It seems that there is only a
very small range for which the option is valuable. The peak of the curve can be
interpreted as the spot price that has the best chance of getting into the money without
hitting the barrier. However, as the volatility increases the chance of the option getting
knocked out increases and consequently the option price declines.
18
16
14
12
Option Value
10
0
60
55 0.3
0.25
50
0.2
45 0.15
0.1
40 0.05
Spot Price
Volatility
It is evident from the figure that increasing the volatility for any given spot price has the
effect of increasing the value of the up and in call option. It is also clear that as the spot
price approaches the barrier level, the value of the option increases due to the
increased probability of the option knocking in.
Analytical Solutions
The barrier options priced in this paper all have readily available analytical solutions.
The formulae for pricing the barrier options have been converted to MATLAB code to
check the Monte Carlo simulation results. See Appendix B for the code.
Monte Carlo Simulation
The basic principle behind Monte Carlo simulation is to simulate as many possible
scenarios and to average those scenarios to get an expectation.
To price options using Monte Carlo methods, many price paths are generated using the
evolution of the stock according to geometric Brownian motion:
2
S (t ) = S (0)e (( r −0.5σ ) t +σ t Z
Z ~ N (0,1)
The option payoff is then calculated for each price path. An accurate estimation of
option price is obtained by discounting the average of all the payoffs.
A function, AssetPaths, was created using MATLAB to create a matrix of asset paths.
The size of this matrix was determined by the parameters passed, namely the desired
number of simulations and the number of steps – price evolutions - required over the
specified time interval. This function returns the matrix of asset paths to be used for
pricing options.
The graph below was created using the function written for generating asset paths to
illustrate the concept behind Monte Carlo simulation. Only seven paths were generated
in this simulation to maintain clarity. In practice, thousands of simulations would be
required to accurately price an option.
Simulated Paths
70
65
60
Asset Price
55
50
45
40
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time
The graph simulating seven asset paths also shows a hypothetical barrier level to help
illustrate the concept behind barrier options. In this simulation, two of the asset paths
hit the barrier. If the matrix in this simulation were used to price a knock-out option,
particularly an up and out option, then the payoffs associated with these two asset
paths would be zero. The remaining five payoffs would be evaluated as one would
evaluate a vanilla option.
10 Steps
100 Steps
500 Steps
55
Asset Price
50
45
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time
It is clear that if there are an insufficient number of steps used over a given time
interval, a barrier might not be hit that might have otherwise been hit had the number
of steps been increased. Thus, for a given time interval, increasing the number of steps
in the simulation increases the number of price evolutions leading to an increase in the
accuracy of the simulation.
Calculating Payoffs
Once multiple asset paths have been simulated, the next step is to determine the payoff
for each asset path. This is done by evaluating each path to see whether it has hit the
barrier. The payoff is then dependent on the type of barrier option and the knowledge
of whether or not the barrier level has been hit during the life of the option. Some
options will expire worthless if the barrier is reached, others will be worthless unless the
barrier is reached. Regardless, coding this for the Monte Carlo simulation is rather
trivial once the asset path has been generated.
The amount of code required can be significantly reduced by noting that whether the
option is an up or down option is determined by the relationship between the barrier
level and the initial spot price. A barrier above the initial spot price indicates that the
option is an up option, while a barrier below indicates a down option.
This information limits the coding to just four functions to price eight different barrier
options:
• knock-out call
• knock-out put
• knock-in call
• knock-in put
Whether the option is an up or down option is then evaluated within each function by
checking if the barrier is above or below the initial spot price.
Code for pricing a knock-out call is provided to show how the payoffs were calculated.
0.08
0.07
0.06
Time Elapsed
0.05
0.04
0.03
0.02
0.01
100
80
1000
900
60 800
700
40 600
500
20 400
300
200
0 100
Number of Simulations
Number of Steps
Using the tic, toc function in MATLAB, a graphic depiction of the computational cost
of accuracy is made possible. The spikes are likely due to interference of running
background processes on the computer used to generate the graph.
Var ( X )
M
Where M is the sample size and Var(X) is the variance. There are only two ways to
decrease the size of the confidence interval. Either increase the sample size or decrease
the variance. In Monte Carlo simulations, increasing the sample size is computationally
costly as was illustrated previously. Variance reduction works by replacing X with a
random variable Y that has the same expectation of X, but with a smaller variance. We
can compute the expectation of X via Monte Carlo methods using Y. Using Y in this
instance allows for the same expectation but with a lower variance.
Antithetic Variates
Variance reduction using antithetic variates is based on the following lemma:
cov( f ( X ), f (− X )) ≤ 0
Let us now consider the case of a random variable which is of the form f (U), where U is a
standard normally distributed random variable, i.e. f(U) ~ N(0,1).
The standard normal distribution is symmetric, and hence also – U ~ N(0, 1). It then follows
obviously that f (U) ~ f (-U). In particular they have the same expectation. Therefore, in case we
want to compute the expectation of X = f (U), we can take Z = f (-U) and
f (U ) + f (− U )
Y=
2
If we now assume that the map f is monotonically increasing, then we conclude from the above
lemma that
cov( f (U ), f (−U )) ≤ 0
X +Z
Ε = Ε( X )
2
X +Z X + Z X + Z 1 1
Var = cov , = Var ( X ) + 2Cov ( X , Z ) + Var (Z ) ≤ Var ( X )
2 2 2 4 14243 123 2
≤0 =Var ( X )
Control Variates
Using control variates is another good way to reduce the variance and generate more
accurate results. Suppose we again wish to estimate E(X). If we can somehow find
another random variable Y, which is close to X in some sense, with known expected
value E(Y) then we can define a random variable Z via
Z = X + c(Y − Ε(Y ))
As we want to minimize this function we look for the value of c that does exactly that.
Straightforward calculation gives
− Cov( X , Y )
c min =
Var (Y )
In practice, however, Cov(X,Y) and Var(Y) are generally unknown so we have to estimate
them via Monte Carlo simulation.
To provide more insight into this method, consider an up and out call option. A suitable
control variate has to be chosen depending on the particular problem we are dealing
with. As we want to price a barrier option, we can assume a plain vanilla call option is a
suitable control variate. Its exact value is known as it can be calculated analytically using
the Black-Scholes formula. Furthermore, its expected value and variance can easily be
estimated.
To estimate the covariance between the up and out call price and the plain vanilla call
price, we estimate the corresponding payoffs via Monte Carlo simulations. This may be
accomplished by using a set of pilot replications.
Next, we are able to calculate the value of c that minimizes the variance of Z.
In a final step we run a Monte Carlo simulation once more to estimate E(Z) using the
previously obtained value of c.
To avoid inducing bias in the estimate of E(Z), pilot replications are used to select c and
to generate an estimate of E(Z). We run another set of replications for the final
estimation of E(Z).
Results
The results for an up and in call barrier option priced using Monte Carlo simulation
methods is presented for discussion.
It is clear that in comparing the use of antithetic variates to the standard Monte Carlo
simulation, the variance of our estimates have been significantly reduced. We halved
the number of steps used to make the comparison fair as the use of antithetic pairs
doubles the sample used for the simulation.
Table 3: Monte Carlo using Variance Reduction: Control Variates
Up and In Call
Number of Simulations
500 Steps 100 1000 10000 100000
True $8.1541
Estimated $7.9724 $8.0699 $8.1147 $8.1209
σ $1.2545 $1.1733 $1.0661 $1.0428
95% Confidence Interval
$7.7235 $8.2214 $7.9971 $8.1427 $8.0938 $8.1356 $8.1144 $8.1273
Error $0.1817 $0.0841 $0.0394 $0.0332
S = $50, K = $50, r = 10%, σ = 30%, T = 1 Barrier = $60
As expected the use of control variates greatly reduces the size of the confidence
interval. Generally, the use of control variates leads to more accurate estimations of the
option price. The results here however can be somewhat misleading. The time elapsed
in performing the simulations is not included. While variance reduction techniques
often lead to increased accuracy for a given number of simulations, the computational
cost associated with the increased accuracy is still present.
Results for all the barrier options considered are provided in Appendix A.
Error Analysis
Increasing the accuracy of Monte Carlo simulations requires a reduction of error. There
are likely to be many sources of error in a simulation, such as the estimates of the
parameters themselves or the assumptions made within the model itself, however, if
these errors can be eliminated, then one of the simplest ways to increase accuracy is to
increase the number of simulations. The use of variance reduction techniques helps
shortcut the need for an increased number of simulations.
0.5
0.45
0.4
0.35
0.3
Absolute Error
0.25
0.2
0.15
0.1
0.05 0.05
0 0.1
0 0.15
1000
2000
3000 0.2
4000
5000
6000 0.25
7000
8000
9000 0.3
10000
Volatility
Number of Simulations
Figure 7: Error associated with an up and out call: Varying volatility and number of
simulations
Spot: $50, Barrier: $60, Strike: $50, Time to Expiry: 1 Year, Interest Rate: 10%, Number
of Steps: 500.
1.4
1.2
Error
0.8
0.6
0.4
0.2
0
0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
Number of Simulations
Figure 8: Up and in call option with Spot: $50, Barrier: $60, Strike: $50, Interest Rate:
10%, Time to Expiry: 1 Year, Volatility: 30%. Errors plotted against number of
simulations used to price the option for various Monte Carlo methods.
The standard Monte Carlo method benefits greatly when the number of simulations
increase. Using Monte Carlo with antithetic variates, the number of simulations
required to obtain the same accuracy as the standard Monte Carlo method is greatly
reduced. The use of control variates quickly results in accurate estimates of the option
price. It appears as if increasing the number of simulations does virtually nothing to
reduce the error. This however may not necessarily be the case as the current figure
distorts the interpretation for the control variates result. In any case, it is clear that the
Monte Carlo method using control variates is much more effective at estimating the
option price for a given number of simulations. However, the time elapsed to value the
option is not considered and would surely factor into the evaluation of which method is
ultimately more efficient.
Conclusion
In this paper an overview of how to estimate barrier option prices via Monte Carlo
simulation was provided. We discussed the presence of computational errors and
described variance reduction techniques to eliminate these as much as possible.
Up and In Put
Number of Simulations
500 Steps 100 1000 10000 100000
True Value $0.5559
Estimated $0.3377 $0.4702 $0.4900 $0.4964
σ $1.5891 $1.9919 $1.9785 $2.0367
95% Confidence Interval
$0.0224 $0.6530 $0.3466 $0.5938 $0.4513 $0.5288 $0.4837 $0.5090
Error $0.2182 $0.0857 $0.0659 $0.0596
S = $50, K = $50, r = 10%, σ = 30%, T = 1 Barrier = $60
Up and In Put
Number of Simulations
250 Steps 100 1000 10000 100000
True Value $0.5559
Estimated $0.3692 $0.5250 $0.4785 $0.4848
σ $1.2150 $1.4640 $1.3787 $1.3828
95% Confidence Interval
$0.1281 $0.6103 $0.4342 $0.6159 $0.4514 $0.5055 $0.4762 $0.4933
Error $0.1867 $0.0309 $0.0774 $0.0712
S = $50, K = $50, r = 10%, σ = 30%, T = 1 Barrier = $60
Up and In Put
Number of Simulations
500 Steps 100 1000 10000 100000
True Value $0.5559
Estimated $0.6797 $0.4610 $0.4958 $0.5000
σ $2.2537 $1.8825 $1.9877 $1.9908
95% Confidence Interval
$0.2326 $1.1269 $0.3442 $0.5778 $0.4569 $0.5348 $0.4877 $0.5124
Error $0.1238 $0.0949 $0.0601 $0.0559
S = $50, K = $50, r = 10%, σ = 30%, T = 1 Barrier = $60
a = (Sb/S)^(-1+(2*(r-q)/sigma^2));
b = (Sb/S)^(1+(2*(r-q)/sigma^2));
d1 = (log(S/E)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d2 = (log(S/E)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d3 = (log(S/Sb)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d4 = (log(S/Sb)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d5 = (log(S/Sb)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d6 = (log(S/Sb)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d7 = (log(S*E/Sb^2)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d8 = (log(S*E/Sb^2)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
if E > Sb
price = S*exp(-q*(T))*(b*(1-normcdf(d8)))...
-E*exp(-r*(T))*(a*(1-normcdf(d7)));
else
price = S*exp(-q*(T))*(normcdf(d1)-normcdf(d3)+b*(1-normcdf(d6)))...
-E*exp(-r*(T))*(normcdf(d2)-normcdf(d4)+a*(1-normcdf(d5)));
end
function price = A_DOCall(S,Sb,E,r,q,T,sigma)
a = (Sb/S)^(-1+(2*(r-q)/sigma^2));
b = (Sb/S)^(1+(2*(r-q)/sigma^2));
d1 = (log(S/E)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d2 = (log(S/E)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d3 = (log(S/Sb)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d4 = (log(S/Sb)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d5 = (log(S/Sb)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d6 = (log(S/Sb)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d7 = (log(S*E/Sb^2)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d8 = (log(S*E/Sb^2)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
if E > Sb
price = S*exp(-q*(T))*(normcdf(d1)-b*(1-normcdf(d8)))...
-E*exp(-r*(T))*(normcdf(d2)-a*(1-normcdf(d7)));
else
price = S*exp(-q*(T))*(normcdf(d3)-b*(1-normcdf(d6)))...
-E*exp(-r*(T))*(normcdf(d4)-a*(1-normcdf(d5)));
end
function price = A_DIPut(S,Sb,E,r,q,T,sigma)
a = (Sb/S)^(-1+(2*(r-q)/sigma^2));
b = (Sb/S)^(1+(2*(r-q)/sigma^2));
% d1 = (log(S/E)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
% d2 = (log(S/E)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d3 = (log(S/Sb)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d4 = (log(S/Sb)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d5 = (log(S/Sb)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d6 = (log(S/Sb)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d7 = (log(S*E/Sb^2)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d8 = (log(S*E/Sb^2)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
a = (Sb/S)^(-1+(2*(r-q)/sigma^2));
b = (Sb/S)^(1+(2*(r-q)/sigma^2));
d1 = (log(S/E)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d2 = (log(S/E)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d3 = (log(S/Sb)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d4 = (log(S/Sb)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d5 = (log(S/Sb)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d6 = (log(S/Sb)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d7 = (log(S*E/Sb^2)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d8 = (log(S*E/Sb^2)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
price = -S*exp(-q*(T))*(normcdf(d3)-normcdf(d1)-...
b*(normcdf(d8)-normcdf(d6)))...
+E*exp(-r*(T))*(normcdf(d4)-normcdf(d2)-a*(normcdf(d7)-normcdf(d5)));
function price = A_UICall(S,Sb,E,r,q,T,sigma)
a = (Sb/S)^(-1+(2*(r-q)/sigma^2));
b = (Sb/S)^(1+(2*(r-q)/sigma^2));
% d1 = (log(S/E)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
% d2 = (log(S/E)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d3 = (log(S/Sb)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d4 = (log(S/Sb)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d5 = (log(S/Sb)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d6 = (log(S/Sb)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d7 = (log(S*E/Sb^2)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d8 = (log(S*E/Sb^2)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
% up and in call
price = S*exp(-q*(T))*(normcdf(d3)+b*(normcdf(d6)-normcdf(d8)))...
-E*exp(-r*(T))*(normcdf(d4)+a*(normcdf(d5)-normcdf(d7)));
function price = A_UOCall(S,Sb,E,r,q,T,sigma)
a = (Sb/S)^(-1+(2*(r-q)/sigma^2));
b = (Sb/S)^(1+(2*(r-q)/sigma^2));
d1 = (log(S/E)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d2 = (log(S/E)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d3 = (log(S/Sb)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d4 = (log(S/Sb)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d5 = (log(S/Sb)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d6 = (log(S/Sb)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d7 = (log(S*E/Sb^2)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d8 = (log(S*E/Sb^2)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
price = S*exp(-q*(T))*(normcdf(d1)-normcdf(d3)-...
b*(normcdf(d6)-normcdf(d8)))...
-E*exp(-r*(T))*(normcdf(d2)-normcdf(d4)-a*(normcdf(d5)-normcdf(d7)));
function price = A_UIPut(S,Sb,E,r,q,T,sigma)
a = (Sb/S)^(-1+(2*(r-q)/sigma^2));
b = (Sb/S)^(1+(2*(r-q)/sigma^2));
d1 = (log(S/E)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d2 = (log(S/E)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d3 = (log(S/Sb)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d4 = (log(S/Sb)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d5 = (log(S/Sb)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d6 = (log(S/Sb)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d7 = (log(S*E/Sb^2)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d8 = (log(S*E/Sb^2)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
% up and in put
if E > Sb
price = -S*exp(-q*(T))*(normcdf(d3)-normcdf(d1)+b*(normcdf(d6)))...
+E*exp(-r*(T))*(normcdf(d4)-normcdf(d2)+a*(normcdf(d5)));
else
price = -S*exp(-q*(T))*(b*(normcdf(d8)))...
+E*exp(-r*(T))*(a*(normcdf(d7)));
end
function price = A_UOPut(S,Sb,E,r,q,T,sigma)
a = (Sb/S)^(-1+(2*(r-q)/sigma^2));
b = (Sb/S)^(1+(2*(r-q)/sigma^2));
d1 = (log(S/E)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d2 = (log(S/E)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d3 = (log(S/Sb)+(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d4 = (log(S/Sb)+(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d5 = (log(S/Sb)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d6 = (log(S/Sb)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
d7 = (log(S*E/Sb^2)-(r-q-0.5*sigma^2)*(T))/(sigma*sqrt(T));
d8 = (log(S*E/Sb^2)-(r-q+0.5*sigma^2)*(T))/(sigma*sqrt(T));
if E > Sb
price = -S*exp(-q*(T))*(1-normcdf(d3)-b*(normcdf(d6)))...
+E*exp(-r*(T))*(1-normcdf(d4)-a*(normcdf(d5)));
else
price = -S*exp(-q*(T))*(1-normcdf(d1)-b*(normcdf(d8)))...
+E*exp(-r*(T))*(1-normcdf(d2)-a*(normcdf(d7)));
end
function [price, std, CI] = knockin_call(spot,Sb,K,r,T,sigma,NSteps,NRepl)
% up and in, down and in call
% if the barrier is less than the spot price, then down and in call
% if the barrier is above the spot price, then up and in call
payoff = zeros(NRepl,1); % col vector of payoffs
for i=1:NRepl
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 1 % path hit the barrier
payoff(i) = max(0,path(NSteps+1)-K);
end
end
% return mean and confidence interval of present value payoff vector
[price, std, CI] = normfit(exp(-r*T)*payoff);
function [price, std, CI] = knockout_call(spot,Sb,K,r,T,sigma,NSteps,NRepl)
% up and out, down and out call
% if the barrier is less than the spot price, then down and out call
% if the barrier is above the spot price, then up and out call
payoff = zeros(NRepl,1); % col vector of payoffs
% loop through the paths to determine payoffs
for i=1:NRepl
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 0 % path has always been above/below the barrier
payoff(i) = max(0,path(NSteps+1)-K); % use the last price
end
end
% return mean and confidence interval of present value payoff vector
[price, std, CI] = normfit(exp(-r*T)*payoff);
function [price, std, CI] = knockin_put(spot,Sb,K,r,T,sigma,NSteps,NRepl)
% up and in, down and in put
% if the barrier is less than the spot price, then down and in put
% if the barrier is above the spot price, then up and in put
payoff = zeros(NRepl,1); % col vector of payoffs
for i=1:NRepl
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 1 % path hit the barrier
payoff(i) = max(0,K-path(NSteps+1));
end
end
% return mean and confidence interval of present value payoff vector
[price, std, CI] = normfit(exp(-r*T)*payoff);
function [price, std, CI] = knockout_put(spot,Sb,K,r,T,sigma,NSteps,NRepl)
% up and out, down and out put
% if the barrier is less than the spot price, then down and out put
% if the barrier is above the spot price, then up and out put
payoff = zeros(NRepl,1); % col vector of payoffs
% loop through the paths to determine payoffs
for i=1:NRepl
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 0 % path has always been above/below the barrier
payoff(i) = max(0,K-path(NSteps+1)); % use the last price
end
end
% return mean and confidence interval of present value payoff vector
[price, std, CI] = normfit(exp(-r*T)*payoff);
function [price,std,CI] = AVknockin_call(spot,Sb,K,r,T,sigma,NSteps,NRepl)
% up and in, down and in call
% if the barrier is less than the spot price, then down and in call
% if the barrier is above the spot price, then up and in call
payoffA = zeros(NRepl,1); % col vector of payoffs
payoffB = zeros(NRepl,1); % col vector of payoffs
% loop through the paths to determine payoffs
for i=1:NRepl
[pathA, pathB] = AssetPathsAV(spot,r,sigma,T,NSteps,1);
knockedA = barrierCrossing(spot,Sb,pathA); % determine if up or down
knockedB = barrierCrossing(spot,Sb,pathB);
if knockedA == 1
payoffA(i) = max(0,pathA(NSteps+1)-K);
end
if knockedB == 1
payoffB(i) = max(0,pathB(NSteps+1)-K);
end
end
payoff = (payoffA + payoffB)./2;
[price, std, CI] = normfit(exp(-r*T)*payoff);
function [price,std,CI] = AVknockout_call(spot,Sb,K,r,T,sigma,NSteps,NRepl)
% up and out, down and out call
% if the barrier is less than the spot price, then down and out call
% if the barrier is above the spot price, then up and out call
payoffA = zeros(NRepl,1); % col vector of payoffs
payoffB = zeros(NRepl,1); % col vector of payoffs
% loop through the paths to determine payoffs
for i=1:NRepl
[pathA, pathB] = AssetPathsAV(spot,r,sigma,T,NSteps,1);
knockedA = barrierCrossing(spot,Sb,pathA); % determine if up or down
knockedB = barrierCrossing(spot,Sb,pathB);
if knockedA == 0 % path has not crossed barrier
payoffA(i) = max(0,pathA(NSteps+1)-K);
end
if knockedB == 0
payoffB(i) = max(0,pathB(NSteps+1)-K);
end
end
payoff = (payoffA + payoffB)./2;
[price, std, CI] = normfit(exp(-r*T)*payoff);
function [price,std,CI] = AVknockin_put(spot,Sb,K,r,T,sigma,NSteps,NRepl)
% up and in, down and in put
% if the barrier is less than the spot price, then down and in put
% if the barrier is above the spot price, then up and in put
payoffA = zeros(NRepl,1); % col vector of payoffs
payoffB = zeros(NRepl,1); % col vector of payoffs
% loop through the paths to determine payoffs
for i=1:NRepl
[pathA, pathB] = AssetPathsAV(spot,r,sigma,T,NSteps,1);
knockedA = barrierCrossing(spot,Sb,pathA); % determine if up or down
knockedB = barrierCrossing(spot,Sb,pathB);
if knockedA == 1
payoffA(i) = max(0,K-pathA(NSteps+1));
end
if knockedB == 1
payoffB(i) = max(0,K-pathB(NSteps+1));
end
end
payoff = (payoffA + payoffB)./2;
[price, std, CI] = normfit(exp(-r*T)*payoff);
function [price,std,CI] = AVknockout_put(spot,Sb,K,r,T,sigma,NSteps,NRepl)
% up and out, down and out put
% if the barrier is less than the spot price, then down and out put
% if the barrier is above the spot price, then up and out put
payoffA = zeros(NRepl,1); % col vector of payoffs
payoffB = zeros(NRepl,1); % col vector of payoffs
% loop through the paths to determine payoffs
for i=1:NRepl
[pathA, pathB] = AssetPathsAV(spot,r,sigma,T,NSteps,1);
knockedA = barrierCrossing(spot,Sb,pathA); % determine if up or down
knockedB = barrierCrossing(spot,Sb,pathB);
if knockedA == 0 % path has not crossed barrier
payoffA(i) = max(0,K-pathA(NSteps+1));
end
if knockedB == 0
payoffB(i) = max(0,K-pathB(NSteps+1));
end
end
payoff = (payoffA + payoffB)./2;
[price, std, CI] = normfit(exp(-r*T)*payoff);
function [price, std, CI] = CVknockin_call(spot,Sb,K,r,T,sigma,NSteps,NRepl,NPilot)
% up and in, down and in call
% if the barrier is less than the spot price, then down and in call
% if the barrier is above the spot price, then up and in call
payoff = zeros(NPilot,1);
vanillaPayoff = zeros(NPilot,1);
muVanilla = blsprice(spot,K,r,T,sigma);
for i=1:NPilot
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
vanillaPayoff(i) = max(0,path(NSteps+1)-K);
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 1 % path hit the barrier
payoff(i) = max(0,path(NSteps+1)-K);
end
end
vanillaPayoff = exp(-r*T)*vanillaPayoff;
payoff = exp(-r*T)*payoff;
covMat = cov(vanillaPayoff, payoff);
varVanilla = var(vanillaPayoff);
c = -covMat(1,2)/varVanilla;
newPayoff = zeros(NRepl,1);
newVanillaPayoff = zeros(NRepl,1);
for i=1:NRepl
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
newVanillaPayoff(i) = max(0,path(NSteps+1)-K);
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 1 % path hit the barrier
newPayoff(i) = max(0,path(NSteps+1)-K);
end
end
newVanillaPayoff = exp(-r*T)*newVanillaPayoff;
newPayoff = exp(-r*T)*newPayoff;
CVpayoff = newPayoff + c*(newVanillaPayoff - muVanilla);
[price, std, CI] = normfit(CVpayoff);
function [price, std, CI] = CVknockout_call(spot,Sb,K,r,T,sigma,NSteps,NRepl,NPilot)
payoff = zeros(NPilot,1);
vanillaPayoff = zeros(NPilot,1);
muVanilla = blsprice(spot,K,r,T,sigma);
for i=1:NPilot
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
vanillaPayoff(i) = max(0,path(NSteps+1)-K);
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 0
payoff(i) = max(0,path(NSteps+1)-K);
end
end
vanillaPayoff = exp(-r*T)*vanillaPayoff;
payoff = exp(-r*T)*payoff;
newPayoff = zeros(NRepl,1);
newVanillaPayoff = zeros(NRepl,1);
for i=1:NRepl
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
newVanillaPayoff(i) = max(0,path(NSteps+1)-K);
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 0
newPayoff(i) = max(0,path(NSteps+1)-K);
end
end
newVanillaPayoff = exp(-r*T)*newVanillaPayoff;
newPayoff = exp(-r*T)*newPayoff;
CVpayoff = newPayoff + c*(newVanillaPayoff - muVanilla);
[price, std, CI] = normfit(CVpayoff);
function [price, std, CI] = CVknockin_put(spot,Sb,K,r,T,sigma,NSteps,NRepl,NPilot)
payoff = zeros(NPilot,1);
vanillaPayoff = zeros(NPilot,1);
[aux, muVanilla] = blsprice(spot,K,r,T,sigma);
for i=1:NPilot
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
vanillaPayoff(i) = max(0,K-path(NSteps+1));
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 1 % path hit the barrier
payoff(i) = max(0,K-path(NSteps+1));
end
end
vanillaPayoff = exp(-r*T)*vanillaPayoff;
payoff = exp(-r*T)*payoff;
newPayoff = zeros(NRepl,1);
newVanillaPayoff = zeros(NRepl,1);
for i=1:NRepl
path = AssetPaths(spot,r,sigma,T,NSteps,1); % generate one path
newVanillaPayoff(i) = max(0,K-path(NSteps+1));
knocked = barrierCrossing(spot,Sb,path); % determine if up or down
if knocked == 1 % path hit the barrier
newPayoff(i) = max(0,K-path(NSteps+1));
end
end
newVanillaPayoff = exp(-r*T)*newVanillaPayoff;
newPayoff = exp(-r*T)*newPayoff;
CVpayoff = newPayoff + c*(newVanillaPayoff - muVanilla);
[price, std, CI] = normfit(CVpayoff);
function [price, std, CI] = CVknockout_put(spot,Sb,K,r,T,sigma,NSteps,NRepl,NPilot)
payoff = zeros(NPilot,1);
vanillaPayoff = zeros(NPilot,1);
[aux, muVanilla] = blsprice(spot,K,r,T,sigma);
for i=1:NPilot
path = AssetPaths(spot,r,sigma,T,NSteps,1);
vanillaPayoff(i) = max(0,K-path(NSteps+1));
knocked = barrierCrossing(spot,Sb,path);
if knocked == 0
payoff(i) = max(0,K-path(NSteps+1));
end
end
vanillaPayoff = exp(-r*T)*vanillaPayoff;
payoff = exp(-r*T)*payoff;
newPayoff = zeros(NRepl,1);
newVanillaPayoff = zeros(NRepl,1);
for i=1:NRepl
path = AssetPaths(spot,r,sigma,T,NSteps,1);
newVanillaPayoff(i) = max(0,K-path(NSteps+1));
knocked = barrierCrossing(spot,Sb,path);
if knocked == 0
newPayoff(i) = max(0,K-path(NSteps+1));
end
end
newVanillaPayoff = exp(-r*T)*newVanillaPayoff;
newPayoff = exp(-r*T)*newPayoff;
CVpayoff = newPayoff + c*(newVanillaPayoff - muVanilla);
[price, std, CI] = normfit(CVpayoff);
function sim_paths = AssetPaths(spot,r,sigma,T,num_steps,num_sims)
sim_paths = zeros(num_sims,num_steps+1); % Each row is a simulated path
sim_paths(:,1) = spot; % fill first col w/ spot prices
dt = T/num_steps; % time step
for i=1:num_sims
for j=2:num_steps+1
wt = randn; % generate a random number from a normal distribution
sim_paths(i,j) = sim_paths(i,j-1)*exp((r-0.5*sigma^2)...
*dt+sigma*sqrt(dt)*wt);
end
end
function [sim_pathsA, sim_pathsB] = AssetPathsAV(spot,r,sigma,T,num_steps,num_sims)
% [c,i,j,k,c,m,l,n,o]
% i = barriers
% j = strike
% k = interest rate (r)
% l = volatility
% m = time interval (T)
% n = num_steps
% o = num_sims
price_matrix = zeros(1,sims_length);
ASol = zeros(1,sims_length);
error_matrix = zeros(1,sims_length);
sigmahat = zeros(1,sims_length);
CI = zeros(2,sims_length);
% for i=1:sb_length
% for j=1:strike_length
% for k=1:r_length
% for l=1:sigma_length
% for m = 1:time_length
switch barrier_type
case 1
ASol(:) = A_DICall(spot,sb,strike,r,0,T,sigma);
range = 'C13:F19';
case 2
ASol(:) = A_DOCall(spot,sb,strike,r,0,T,sigma);
range = 'C23:F29';
case 3
ASol(:) = A_DIPut(spot,sb,strike,r,0,T,sigma);
range = 'C33:F39';
case 4
ASol(:) = A_DOPut(spot,sb,strike,r,0,T,sigma);
range = 'C43:F49';
case 5
ASol(:) = A_UICall(spot,sb,strike,r,0,T,sigma);
range = 'C53:F59';
case 6
ASol(:) = A_UOCall(spot,sb,strike,r,0,T,sigma);
range = 'C63:F69';
case 7
ASol(:) = A_UIPut(spot,sb,strike,r,0,T,sigma);
range = 'C73:F79';
case 8
ASol(:) = A_UOPut(spot,sb,strike,r,0,T,sigma);
range = 'C83:F89';
end
% for n = 1:steps_length % issue here?
for i = 1:sims_length
switch barrier_type
case {1,5}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
AVknockin_call(spot,sb,strike,r,T,sigma,num_steps,num_sims(i));
case {2,6}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
AVknockout_call(spot,sb,strike,r,T,sigma,num_steps,num_sims(i));
case {3,7}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
AVknockin_put(spot,sb,strike,r,T,sigma,num_steps,num_sims(i));
case {4,8}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
AVknockout_put(spot,sb,strike,r,T,sigma,num_steps,num_sims(i));
end
error_matrix(i) = abs(price_matrix(i)-ASol(i));
end
% end
% end
% end
% end
% end
% end
M = [num_sims;ASol;price_matrix;sigmahat;CI;error_matrix];
xlswrite(filename, M, range)
disp('Completed successfully!');
% xlswrite(filename, M) writes matrix M to the Excel file filename.
% The filename input is a string enclosed in single quotes.
% The input matrix M is an m-by-n numeric, character, or cell array,
% where m < 65536 and n < 256. The matrix data is written to the first
% worksheet in the file, starting at cell A1.
% Note: To specify sheet or range, but not both, call xlswrite with just
% three inputs. If the third input is a string that includes a colon
% character (e.g., 'D2:H4'), it specifies range.
function knocked = barrierCrossing(spot,sb,path)
if sb < spot % barrier is less than spot price, down and in/out
knocked = any(path <= sb);
else % barrier is greater than spot, up and in/out
knocked = any(path >= sb);
end
barrier_type = 5;
filename = 'CVPricing.xls';
spot = 50;
sb = 60;
strike = 50;
r = 0.1;
T = 1;
sigma = 0.3;
num_steps = 500;
num_sims = [100 1000 10000 100000];
% [c,i,j,k,c,m,l,n,o]
% i = barriers
% j = strike
% k = interest rate (r)
% l = volatility
% m = time interval (T)
% n = num_steps
% o = num_sims
price_matrix = zeros(1,sims_length);
ASol = zeros(1,sims_length);
error_matrix = zeros(1,sims_length);
sigmahat = zeros(1,sims_length);
CI = zeros(2,sims_length);
% for i=1:sb_length
% for j=1:strike_length
% for k=1:r_length
% for l=1:sigma_length
% for m = 1:time_length
switch barrier_type
case 1
ASol(:) = A_DICall(spot,sb,strike,r,0,T,sigma);
range = 'C13:F19';
case 2
ASol(:) = A_DOCall(spot,sb,strike,r,0,T,sigma);
range = 'C23:F29';
case 3
ASol(:) = A_DIPut(spot,sb,strike,r,0,T,sigma);
range = 'C33:F39';
case 4
ASol(:) = A_DOPut(spot,sb,strike,r,0,T,sigma);
range = 'C43:F49';
case 5
ASol(:) = A_UICall(spot,sb,strike,r,0,T,sigma);
range = 'C53:F59';
case 6
ASol(:) = A_UOCall(spot,sb,strike,r,0,T,sigma);
range = 'C63:F69';
case 7
ASol(:) = A_UIPut(spot,sb,strike,r,0,T,sigma);
range = 'C73:F79';
case 8
ASol(:) = A_UOPut(spot,sb,strike,r,0,T,sigma);
range = 'C83:F89';
end
% for n = 1:steps_length % issue here?
for i = 1:sims_length
switch barrier_type
case {1,5}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
CVknockin_call(spot,sb,strike,r,T,sigma,num_steps,num_sims(i),5000);
case {2,6}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
CVknockout_call(spot,sb,strike,r,T,sigma,num_steps,num_sims(i),5000);
case {3,7}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
CVknockin_put(spot,sb,strike,r,T,sigma,num_steps,num_sims(i),5000);
case {4,8}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
CVknockout_put(spot,sb,strike,r,T,sigma,num_steps,num_sims(i),5000);
end
error_matrix(i) = abs(price_matrix(i)-ASol(i));
end
% end
% end
% end
% end
% end
% end
M = [num_sims;ASol;price_matrix;sigmahat;CI;error_matrix];
xlswrite(filename, M, range)
disp('Completed successfully!');
% xlswrite(filename, M) writes matrix M to the Excel file filename.
% The filename input is a string enclosed in single quotes.
% The input matrix M is an m-by-n numeric, character, or cell array,
% where m < 65536 and n < 256. The matrix data is written to the first
% worksheet in the file, starting at cell A1.
% Note: To specify sheet or range, but not both, call xlswrite with just
% three inputs. If the third input is a string that includes a colon
% character (e.g., 'D2:H4'), it specifies range.
barrier_type = 4;
filename = 'DICall.xls';
spot = 50;
sb = 40;
strike = 50;
r = 0.1;
T = 1;
sigma = 0.3;
num_steps = 500;
num_sims = [100 1000 10000 100000];
% [c,i,j,k,c,m,l,n,o]
% i = barriers
% j = strike
% k = interest rate (r)
% l = volatility
% m = time interval (T)
% n = num_steps
% o = num_sims
price_matrix = zeros(1,sims_length);
ASol = zeros(1,sims_length);
error_matrix = zeros(1,sims_length);
sigmahat = zeros(1,sims_length);
CI = zeros(2,sims_length);
% for i=1:sb_length
% for j=1:strike_length
% for k=1:r_length
% for l=1:sigma_length
% for m = 1:time_length
switch barrier_type
case 1
ASol(:) = A_DICall(spot,sb,strike,r,0,T,sigma);
range = 'C13:F19';
case 2
ASol(:) = A_DOCall(spot,sb,strike,r,0,T,sigma);
range = 'C23:F29';
case 3
ASol(:) = A_DIPut(spot,sb,strike,r,0,T,sigma);
range = 'C33:F39';
case 4
ASol(:) = A_DOPut(spot,sb,strike,r,0,T,sigma);
range = 'C43:F49';
case 5
ASol(:) = A_UICall(spot,sb,strike,r,0,T,sigma);
range = 'C53:F59';
case 6
ASol(:) = A_UOCall(spot,sb,strike,r,0,T,sigma);
range = 'C63:F69';
case 7
ASol(:) = A_UIPut(spot,sb,strike,r,0,T,sigma);
range = 'C73:F79';
case 8
ASol(:) = A_UOPut(spot,sb,strike,r,0,T,sigma);
range = 'C83:F89';
end
% for n = 1:steps_length % issue here?
for i = 1:sims_length
switch barrier_type
case {1,5}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
knockin_call(spot,sb,strike,r,T,sigma,num_steps,num_sims(i));
case {2,6}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
knockout_call(spot,sb,strike,r,T,sigma,num_steps,num_sims(i));
case {3,7}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
knockin_put(spot,sb,strike,r,T,sigma,num_steps,num_sims(i));
case {4,8}
[price_matrix(i), sigmahat(i), CI(:,i)] = ...
knockout_put(spot,sb,strike,r,T,sigma,num_steps,num_sims(i));
end
error_matrix(i) = abs(price_matrix(i)-ASol(i));
end
% end
% end
% end
% end
% end
% end
M = [num_sims;ASol;price_matrix;sigmahat;CI;error_matrix];
xlswrite(filename, M, range)
disp('Completed successfully!');
% xlswrite(filename, M) writes matrix M to the Excel file filename.
% The filename input is a string enclosed in single quotes.
% The input matrix M is an m-by-n numeric, character, or cell array,
% where m < 65536 and n < 256. The matrix data is written to the first
% worksheet in the file, starting at cell A1.
% Note: To specify sheet or range, but not both, call xlswrite with just
% three inputs. If the third input is a string that includes a colon
% character (e.g., 'D2:H4'), it specifies range.
function [price_matrix,tElapsed,error_matrix,ASol] = ...
graphingFunction(spot,sb,strike,r,T,sigma,...
num_steps,num_sims,barrier_type,filename)
% passing in vector of possible values for ranges of variables
% sb = 0:10:100; % vector of barriers
% [c,i,j,k,c,m,l,n,o]
% i = barriers
% j = strike
% k = interest rate (r)
% l = volatility
% m = time interval (T)
% n = num_steps
% o = num_sims
for i=1:sb_length
for j=1:strike_length
for k=1:r_length
for l=1:sigma_length
for m = 1:time_length
switch barrier_type
case 1
ASol(i,j,k,l,m) = A_DICall(spot,sb(i),strike(j),r(k),0,T(m),sigma(l));
case 2
ASol(i,j,k,l,m) = A_DOCall(spot,sb(i),strike(j),r(k),0,T(m),sigma(l));
case 3
ASol(i,j,k,l,m) = A_DIPut(spot,sb(i),strike(j),r(k),0,T(m),sigma(l));
case 4
ASol(i,j,k,l,m) = A_DOPut(spot,sb(i),strike(j),r(k),0,T(m),sigma(l));
case 5
ASol(i,j,k,l,m) = A_UICall(spot,sb(i),strike(j),r(k),0,T(m),sigma(l));
case 6
ASol(i,j,k,l,m) = A_UOCall(spot,sb(i),strike(j),r(k),0,T(m),sigma(l));
case 7
ASol(i,j,k,l,m) = A_UIPut(spot,sb(i),strike(j),r(k),0,T(m),sigma(l));
case 8
ASol(i,j,k,l,m) = A_UOPut(spot,sb(i),strike(j),r(k),0,T(m),sigma(l));
end
for n = 1:steps_length % issue here?
for o = 1:sims_length
switch barrier_type
case {1,5}
tic;
[price_matrix(i,j,k,l,m,n,o), aux, aux] = ...
knockin_call(spot,sb(i),strike(j),r(k),T(m),sigma(l),...
num_steps(n),num_sims(o));
tElapsed(i,j,k,l,m,n,o) = toc;
case {2,6}
tic;
[price_matrix(i,j,k,l,m,n,o), aux, aux] = ...
knockout_call(spot,sb(i),strike(j),r(k),T(m),sigma(l),...
num_steps(n),num_sims(o));
tElapsed(i,j,k,l,m,n,o) = toc;
case {3,7}
tic;
[price_matrix(i,j,k,l,m,n,o), aux, aux] = ...
knockin_put(spot,sb(i),strike(j),r(k),T(m),sigma(l),...
num_steps(n),num_sims(o));
tElapsed(i,j,k,l,m,n,o) = toc;
case {4,8}
tic;
[price_matrix(i,j,k,l,m,n,o), aux, aux] = ...
knockout_put(spot,sb(i),strike(j),r(k),T(m),sigma(l),...
num_steps(n),num_sims(o));
tElapsed(i,j,k,l,m,n,o) = toc;
end
error_matrix(i,j,k,l,m,n,o) = abs(price_matrix(i,j,k,l,m,n,o)-...
ASol(i,j,k,l,m));
end
end
end
end
end
end
end
save(filename, 'price_matrix', 'tElapsed','error_matrix','ASol','spot',...
'sb','strike','r','T','sigma','num_steps','num_sims','barrier_type');
% prices = zeros(1,o);
% for c=1:o
% prices(i) = price_matrix(1,1,1,1,1,1,i);
% end
%======== Changing Strike ===========
%======= MC vs Analytical ==========
% prices = price_matrix(1,:,1,1,1,1,1);
% plot(strike,prices);
% hold on;
% aprices = ASol(1,:,1,1,1);
% plot(strike,aprices);
% hold off;
% for c=1:l
% time(c) = tElapsed(1,1,1,c,1,1,1);
% end
% plot(num_sims,tElapsed(c),'-r');
[i j k l m n o] = size(price_matrix);
%======== Changing Barrier and Volatility ===========
%======= MC vs Analytical ==========
% [X,Y]=meshgrid(sigma,sb);
% [m n] = size(X);
% prices = zeros(m,n);
% aprices = zeros(m,n);
% error = zeros(m,n);
% for c=1:m
% for d=1:n
% prices(c,d) = price_matrix(c,1,1,d,1,1,1);
% end
% end
% surf(X,Y,prices);
% [X,Y]=meshgrid(sigma,sb);
% [m n] = size(X);
% prices = zeros(m,n);
% aprices = zeros(m,n);
% error = zeros(m,n);
% for c=1:m
% for d=1:n
% error(c,d) = error_matrix(c,1,1,d,1,1,1);
% end
% end
% surf(X,Y,error);
[X,Y]=meshgrid(T,num_steps);
[m n] = size(X);
prices = zeros(m,n);
aprices = zeros(m,n);
error = zeros(m,n);
for c=1:m
for d=1:n
error(c,d) = error_matrix(1,1,1,1,d,c,1);
end
end
surf(X,Y,error);
xlabel('Time Interval');
ylabel('Number of Steps');
zlabel('Absolute Error');
% prices = zeros(1,l);
% aprices = zeros(1,l);
% error = zeros(1,l);
% for c=1:l
% prices(c) = price_matrix(1,1,1,c,1,1,1);
% end
% prices;
% plot(sigma,prices);
% hold on;
% for c=1:l
% aprices(c) = ASol(1,1,1,c,1);
% end
% plot(sigma,aprices,'-r');
% hold off;
% prices = zeros(1,i);
% aprices = zeros(1,i);
% error = zeros(1,i);
% for c=1:i
% prices(c) = price_matrix(c,1,1,1,1,1,1);
% end
% prices;
% plot(sb,prices);
% hold on;
% for c=1:i
% aprices(c) = ASol(c,1,1,1,1);
% end
% plot(sb,aprices,'-r');
% hold off;
% for c=1:l
% error(c) = error_matrix(1,1,1,c,1,1,1)/prices(c);
% end
% plot(sigma,error,'-r');
% plot(sb,prices);
% hold on;
% for c=1:i
% aprices(c) = ASol(c,1,1,1,1);
% end
% plot(sb,aprices,'-r');
% hold off;
% [price_matrix,tElapsed,error_matrix] = ...
% graphingFunction(spot,sb,strike,r,delta,T,sigma,...
% num_steps,num_sims,barrier_type,filename)