# Python Multiprocessing and Monte Carlo Option Pricing

Contributed by a student who took the introductory Python class with John Downs.

----------------------------------

In this blog post, I demonstrate the use of Python’s multiprocessing package.  The package allows for execution of Python code in a parallel manner through multiple interpreter processes.  Popular implementations of Python do not support multithreading in an optimal way, because of the global interpreter lock preventing multiple threads from executing code at once.  Multiple processes of the python interpreter work around this limitation.  The downside, however, is that data in memory cannot be shared by more than one process.  The multiprocessing package relies on the pickle package to distribute data across different processes.

To demonstrate the speedup that can be achieved by executing code in parallel, I use an example from computational finance.  It turns out that financial options, and in particular call options (contracts that give the holder the right but not the obligation to purchase an asset in the future at a pre-specified price), can be priced by averaging simulated possible payoffs.

The code below implements a European equity CallOption class that contains two methods:  PriceBS and PriceMC.  Once an option object is created by specifying the required parameters (current stock price, stock price volatility, strike price, maturity of option, and the riskfree rate), its price can be obtained by calling one of the two methods.  PriceBS provides an exact solution to the Black-Scholes differential equation, which governs options prices under some common assumptions (stock price follows a Brownian motion diffusion process).  PriceMC provides a simulation based (Monte Carlo) approximation to the price computed by averaging the option’s payoff across simulated path of the stock price.

The PriceMC function is a good candidate for parallel execution, because it requires simulating thousands or millions of possible stock price paths.  Because there is no dependence between paths, each can be simulated separately from the others.

I note that this example is only for illustrative purposes.  In practice, given that obtaining an accurate solution requires simulating millions of paths, this is not a practical method to use for pricing options without making the underlying algorithm more efficient.

The code snippet below shows the class implementation including a constructor and the PriceBS function, which implements the Black-Scholes formula.

The code below shows the PriceMC function.  This function simulates N paths of the stock price process.  The average payoff of the option is then calculated and stored in the sum_payoff variable.  Note that by keeping a running sum, memory requirements are modest since the N paths are not stored in memory simultaneously.

Code below shows a vectorized implementation of PriceMC using numpy.  This code is much faster that the running sum implementation.  However, significant memory is required for large values of N making this approach somewhat impractical.  A hybrid approach that optimizes the speed and memory tradeoff would certainly be worth exploring.

The code below implements the multiprocessing module code and tests execution speed for a variety of scenarios for N and number of processes.  This code was run on a Digital Ocean droplet with 20 virtual processors.

Note that the map function is used to run code contained in the eval_price function in parallel as shown below.

The results of the experiment are shown below.  The speedup is noticeable but not linear in the number of processes.  Likewise, accuracy generally improves as N increases.

### RiskLuminary

View all posts by RiskLuminary >

## Related Articles

Google Below youll come across the link to some web sites that we feel you must visit.
Google The time to study or visit the subject material or internet sites we have linked to below.
cartier love imitation ring August 31, 2017
I read this piece of writing completely about the comparison of latest and earlier technologies, it’s awesome article.| cartier love imitation ring http://www.latestbracelet.com/replica-cartier-love-diamond-ring-love-on-the-side.html
how much does copy rolex lady all black watch May 29, 2017
This actually answered my problem, thanks!
how much is the cheap cartier bangle white May 7, 2017
I only wish we had Representatives and Senators like Dr. Carson. We deserve to be served by honest, thoughtful and sincere representatives of the people. Politics has degraded through so much corruption and power grabbing that it’s painful for proud Americans to listen. We must engage, but for all our passion and love for this greatest nation on Earth, we feel silenced, stifled and belittled by those people of any party elected to “lead” us. They should never be leaders, rather followers of the will of the majority of their constituency. When honest, sincere, humble men and women take office, We The People may be best served. Let’s try to find and elect statesmen, not politicians… how much is the cheap cartier bangle white http://www.thislovebangle.cn/cartier-love-bracelet-white-gold-4-diamonds-replica-b6035816-p-270.html
Sabine October 21, 2016
I read this parqgraph cmpletely about the resemblance oof most recent and preceding technologies, it's awesome article.
bulgari bijoux replique September 27, 2016
Naomi, I've heard a lot of women talk about how uncomfortable expanders are, and like you some experience pain, not discomfort.  Once the expanders are exchanged for the actual implants, most women feel much better.  You are right that your doctors aren't likely to want to do any kind of surgical procedure while you are on chemo because the chances of infection increase while you are on chemo. bulgari bijoux replique http://www.bzero.cn/fr/
bracelets cartier replique September 13, 2016
cartierbraceletlove And Fayose thanked his “god” for his victory at this election. He wasn’t talking about the King of Heaven who abhors this evil. Any pastor with spiritual discernment should be wary of politicians when they come to them to bless their fruit of poison. Today is the beginning of the end of the PDP travesty in Ekiti State. bracelets cartier replique http://www.bestcalovebracelet.cn/fr/
bracelet cartier réplique love September 7, 2016
cartierbraceletlove Beautiful! I agree that, although I like to do my own doodles, this is great for those ‘harder’ days…and for those who shy away from praying in color because they are intimidated by the blank page. I might take your idea of a big one that becomes more like a community doodle for an intergenerational event in our church this fall, as well as having some for them to color on individually or take home bracelet cartier réplique love http://www.bestcalovebracelet.cn/fr/
boucles d'oreilles van cleef and arpels cornaline Knockoff September 7, 2016
we are not questioning your IQ— we are discovering your socio-economic myopia boucles d'oreilles van cleef and arpels cornaline Knockoff http://www.vancleefalhambra.com/fr/cheap-vintage-alhambra-necklace-10-motifs-vcard40600-p226.html
tank mc cartier replique July 12, 2016
cartierbraceletlove Yum, that looks delicious! Wish my husband liked zucchini. tank mc cartier replique http://www.orologipopolari.net/fr/cartier-tank-mc-watch-c99_117/
cartier rotonde femme July 5, 2016
Thanks a bunch for sharing this with all people you really realize what you're speaking approximately! Bookmarked. Please also consult with my site =). We can have a hyperlink change arrangement between us cartier rotonde femme http://www.luxemontre.com/cartier-montres-baignoire-est-un-symbole-des-femmes/
réplique bague van cleef prix June 30, 2016
cartierbraceletlove Wow! This could be one particular of the most useful blogs We’ve ever arrive across on this subject. Actually Excellent. I am also an expert in this topic so I can understand your effort. réplique bague van cleef prix http://www.collanaqualitagioielli.cn/fr/van-cleef-ring-replica-c3_84/
botas montaña salomon May 29, 2016
naturally like your website but you need to check the spelling on several of your posts. Several of them are rife with spelling problems and I to find it very bothersome to inform the reality nevertheless I will surely come back again.
lenceria calvin klein May 16, 2016
Hello, constantly i used to check webpage posts here early in the dawn, as i enjoy to learn more and more. lenceria calvin klein http://www.stolzeplanejados.com.br/jado.php?es=id-6385
Billy April 19, 2016
I am really glad to read thnis weblog posts which includes plenty of valuable data, thanks for providing these statistics.