Created
July 17, 2020 05:06
-
-
Save JKEVIN2010/442f527050d65d7ede7e8d9e1135dfd8 to your computer and use it in GitHub Desktop.
Monte Carlo Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Monte Carlo valuation of European call options with NumPy (log version) | |
| # Monte_Carlo.py | |
| # | |
| import math | |
| from numpy import * | |
| from time import time | |
| # star import for shorter code | |
| random.seed(20000) | |
| t0 = time() | |
| # Parameters | |
| S0 = 100.; K = 105.; T = 1.0; r = 0.05; sigma = 0.2 | |
| M = 50; dt = T / M; I = 250000 | |
| # Simulating I paths with M time steps | |
| S = S0 * exp(cumsum((r - 0.5 * sigma ** 2) * dt | |
| + sigma * math.sqrt(dt) | |
| * random.standard_normal((M + 1, I)), axis=0)) | |
| # sum instead of cumsum would also do | |
| # if only the final values are of interest | |
| S[0] = S0 | |
| # Calculating the Monte Carlo estimator | |
| C0 = math.exp(-r * T) * sum(maximum(S[-1] - K, 0)) / I | |
| # Results output | |
| tnp2 = time() - t0 | |
| print('The European Option Value is: ', C0) # The European Option Value is: 8.165807966259603 | |
| print('The Execution Time is: ',tnp2) # The Execution Time is: 0.9024488925933838 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment