Python Multiprocessing and Monte Carlo Option Pricing

Posted on Jun 18, 2014

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.


About Author

Related Articles

Leave a Comment

Google January 25, 2021
Google One of our visitors just lately advised the following website.
Google November 5, 2019
Google Below you’ll come across the link to some web sites that we feel you must visit.
Google September 23, 2019
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
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
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
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
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
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
tank mc cartier replique July 12, 2016
cartierbraceletlove Yum, that looks delicious! Wish my husband liked zucchini. tank mc cartier replique
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
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
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
Billy April 19, 2016
I am really glad to read thnis weblog posts which includes plenty of valuable data, thanks for providing these statistics.

View Posts by Categories

Our Recent Popular Posts

View Posts by Tags

#python #trainwithnycdsa 2019 airbnb Alex Baransky alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus API Application artist aws beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Best Data Science Bootcamp 2020 Best Ranked Big Data Book Launch Book-Signing bootcamp Bootcamp Alumni Bootcamp Prep Bundles California Cancer Research capstone Career Career Day citibike clustering Coding Course Demo Course Report D3.js data Data Analyst data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization Deep Learning Demo Day Discount dplyr employer networking feature engineering Finance Financial Data Science Flask gbm Get Hired ggplot2 googleVis Hadoop higgs boson Hiring hiring partner events Hiring Partners Industry Experts Instructor Blog Instructor Interview Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter lasso regression Lead Data Scienctist Lead Data Scientist leaflet linear regression Logistic Regression machine learning Maps matplotlib Medical Research Meet the team meetup Networking neural network Neural networks New Courses nlp NYC NYC Data Science nyc data science academy NYC Open Data NYCDSA NYCDSA Alumni Online Online Bootcamp Online Training Open Data painter pandas Part-time Portfolio Development prediction Prework Programming PwC python Python Data Analysis python machine learning python scrapy python web scraping python webscraping Python Workshop R R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest Ranking recommendation recommendation system regression Remote remote data science bootcamp Scrapy scrapy visualization seaborn Selenium sentiment analysis Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau team TensorFlow Testimonial tf-idf Top Data Science Bootcamp twitter visualization web scraping Weekend Course What to expect word cloud word2vec XGBoost yelp