# [R-SIG-Finance] Monte Carlo Option Pricing formula R code vs Matlab

Enrico Schumann enricoschumann at yahoo.de
Fri Feb 3 09:08:46 CET 2012

```Hi Darko,

R's 'max' function behaves differently from MATLAB's. You probably don't
want

payoff = max(delS - exercisePrice, 0);

but

payoff = pmax(delS - exercisePrice, 0)

(and you should settle on one number of time steps per year; either 255
or 252).

If you only work with European options and use the
Black--Scholes--Merton SDE for the underlier, then there is no need to
simulate the paths of the SDEs; you can do one time step and "jump from
0 to T".

And as rex has pointed out, there would indeed be a substantial speedup
if you rewrote your code in a vectorised fashion. (This is also true for
MATLAB, though the speedup is typically much smaller than in R.)

Regards,
Enrico

Am 03.02.2012 00:36, schrieb Roupell, Darko:
> Thanks Enrico,
>
> To test if the code structure is correct I googled for alternative samples of Monte Carlo Option pricing coded in Matlab.
>
> What I find the most puzzling is that even if I re-code using sample from matlab the results obtained in R are very different to those obtained by matlab, despite using the same parameters apart of Rnorm(). As I am at loss I am hoping that you or someone else in R-SIG may spot the difference that explains it.
>
> ########################################## R code ###########################################################
>
> exercisePrice   = 100;
> timeToExpiry    = 5;        #% in years
> underlyingPrice = 100;      #% underlying in cents
> expectedVol     = 0.2;      #% expected volatility
> expectedDiv     = 0;        # expected dividend
> riskFreeRate    = 0.06;   #% interest rate
> itr = 10000              #% number of iterations
> delS = 0*array(0,itr)
>
> dt = 1/252
> nudt = (riskFreeRate - expectedDiv - 0.5 * expectedVol^ 2)* dt
> sigsdt = expectedVol * sqrt(dt)
> itr = 10000
> delS = array(0,itr)
> drifts = 255 * timeToExpiry
>
> for( i in (1:itr))
> {
> dS =  underlyingPrice
>
>      for (j in (1:drifts))
>      {
>      eps = rnorm(1, mean = 0, sd = 1)
>      dS  = dS * exp(nudt +sigsdt*eps)
>      }
> delS[i] =dS
> }
>
> payoff = max(delS - exercisePrice, 0);
> cal=mean(payoff) * exp(-riskFreeRate*timeToExpiry)
>
> results:
>> cal
> [1] 396.2675
>
> ################################################## MATLAB ###########################################
>
> S0=100; K=100; r=0.06; sig=0.2; T=5; div=0;
>
> dt = 1/252;
> nudt = (r - div - 0.5 * sig ^ 2) * dt;
> sigsdt = sig * sqrt(dt)
> sim=10000;
> Si=zeros(1,sim);
>
> drifts=255*T
> for i=1:sim
> S=S0;
> for j=1:drifts;
> z=randn(1,1);
>
> S = S* exp(nudt + sigsdt * z);
> end
> Si(i)=S;
> end
>
> payoff = max(Si - K, 0);
> cal=mean(payoff) * exp(-r* T)
>
> calbs=blsprice(S0,K,r,T,sig,div)<---B&S
>
> results:
>
> cal = 32.0173
>
>
> calbs = 31.6150
>
>
> __________________________________________________
> Commonwealth Bank
> Darko Roupell
> Associate Quantitative Analyst
> Institutional Banking&  Markets
> Equities Research
> Darling Park Tower 1
> Level 23, 201 Sussex Street
> Sydney, NSW 2000
> P:  +61 2 9117 1254
> F:  +61 2 9118 1000
> M: +61 400 170 515
> E: Darko.Roupell at cba.com.au
> Our vision is to be Australia's finest financial services organisation through excelling in customer service.
>
> Email Security
> This email is sent solely for informational purposes. Hoax emails, commonly referred to as phishing, can appear to be from the Commonwealth Bank and ask you to update or confirm details such as client numbers, passwords, personal identification questions, contact details or account numbers. The Commonwealth Bank will never send you an email asking you to confirm, update or reveal your confidential banking information.
> Important Information
> Produced by Global Markets Research, a business unit of Commonwealth Bank of Australia ABN 48 123 123 124 - AFSL 234945 (Commonwealth Bank). This publication is based on information available at the time of publishing.  We believe that the information in this communication is correct and any opinions, conclusions or recommendations are reasonably held or made as at the time of its compilation, but no warranty is made as to accuracy, reliability or completeness.  To the extent permitted by law, neither Commonwealth Bank nor any of its subsidiaries accept liability to any person for loss or damage arising from the use of this communication. This communication does not purport to be a complete statement or summary.
> The information provided has been prepared without considering your objectives, financial situation or needs, and before acting on the information, you should consider its appropriateness to your circumstances. No person should act on the basis of this report without considering and if necessary taking appropriate professional advice upon their own particular circumstances.
> Commonwealth Bank of Australia, as a provider of investment, borrowing and other financial services undertakes financial transactions with many corporate entities in Australia. This may include any corporate issuer referred to in this communication. Commonwealth Bank and its subsidiaries have effected or may effect transactions for their own account in any investments or related investments referred to herein. In the case of certain securities Commonwealth Bank is or may be the only market maker.
>
> -----Original Message-----
> From: Enrico Schumann [mailto:enricoschumann at yahoo.de]
> Sent: Thursday, 2 February 2012 8:45 PM
> To: Roupell, Darko
> Cc: r-sig-finance at r-project.org
> Subject: Re: [R-SIG-Finance] Monte Carlo Option Pricing formula
>
>
> Hi, Darko,
>
> Am 02.02.2012 07:44, schrieb Roupell, Darko:
>> Hi All,
>>
>> I am trying to cross check option implied employee option price that was derived using Monte Carlo simulation. Below is code and parameter used and after accounting for dividend yield ( 1.46%) the derived option price is 206.8843 using the code snippet provided. Approx 1 cent below 207.95 that is listed in company prospect using their Monte Carlo simulation and below parameters.
>>
>> As we all know number of iteration can also slightly impact the average price, but am I rightly concerned that my methodology may not be correct?
>
> Hm, I have not really looked at your programme so I cannot comment
> whether it is correct. But we are talking about a difference of about
> half a percentage point here. Which is not much. I just ran you script
> 20 times.
>
>   >  summary(results)
>      Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>     205.6   206.0   206.3   206.3   206.8   206.9
>
> Admittedly, all results are all below the company's price, but
> nevertheless: they vary.
>
> There are details that they might have done differently. For instance,
> you do not compound (if I see correctly). What if you replaced
>
> riskFreeRate*timeToExpiry
>
> with
>
> (1+riskFreeRate)*timeToExpiry-1
>
> But even if that gives you the price: from a practical point of view,
> the difference is small, really.
>
> (Much better would be to check what would happen if the div did not turn
> out as expected, if the vol were different, etc)
>
> Regards,
> Enrico
>
>
>>
>> Any feedback will be appreciated.
>>
>>
>> exercisePrice   = 0;
>> timeToExpiry    = 3;        #% in years
>> underlyingPrice = 490;      #% underlying in cents
>> expectedVol     = 0.5;      #% expected volatility
>> expectedDiv     = 0.0146;        #% expected dividend in cents
>> riskFreeRate    = 0.0425;   #% interest rate
>> itr = 500000              #% number of iterations
>> delS = 0*array(0,itr)
>>
>> for( i in (1:itr))
>> {
>>       eps = rnorm(1)         #% random number generator
>>       dS = expectedDiv*underlyingPrice+underlyingPrice*(riskFreeRate*timeToExpiry) + (underlyingPrice*expectedVol*eps*sqrt(timeToExpiry))
>>       mv = dS - exercisePrice;
>>
>>       delS[i] = max(mv,0);
>> }
>>
>> mean(delS)
>>
>> __________________________________________________
>> Commonwealth Bank
>> Darko Roupell
>> Associate Quantitative Analyst
>> Institutional Banking&   Markets
>> Equities Research
>> Darling Park Tower 1
>> Level 23, 201 Sussex Street
>> Sydney, NSW 2000
>> P:  +61 2 9117 1254
>> F:  +61 2 9118 1000
>> M: +61 400 170 515
>> E: Darko.Roupell at cba.com.au
>
> [...]
>> _______________________________________________
>> R-SIG-Finance at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R questions should go.
>>
>

--
Enrico Schumann
Lucerne, Switzerland
http://nmof.net/

```