Toy Monte Carlo Pricing
Monte Carlo simulation is a powerful tool for estimating the value of financial instruments when analytic pricing formulas are unavailable or impractical. A classic example is the European call option, which gives the holder the right (but not the obligation) to buy an asset at a specified strike price on a fixed future date. By simulating many possible future asset prices and averaging their payoffs, you can estimate the fair price of such an option. This approach is especially useful when the underlying asset's price follows a stochastic process, such as geometric Brownian motion.
123456789101112131415161718192021222324import numpy as np # Parameters for the simulation S0 = 100 # Initial asset price K = 105 # Strike price T = 1.0 # Time to maturity (in years) r = 0.05 # Risk-free interest rate sigma = 0.2 # Volatility of the asset n_sim = 10000 # Number of Monte Carlo simulations # Simulate final asset prices at maturity using geometric Brownian motion np.random.seed(42) # For reproducibility Z = np.random.randn(n_sim) ST = S0 * np.exp((r - 0.5 * sigma**2) * T + sigma * np.sqrt(T) * Z) # Calculate the payoff for each simulation (call option) payoffs = np.maximum(ST - K, 0) # Discount payoffs back to present value discounted_payoffs = np.exp(-r * T) * payoffs # Estimate the option price call_price = np.mean(discounted_payoffs) print(f"Estimated European call option price: {call_price:.2f}")
The logic behind this Monte Carlo pricing model is straightforward. First, you generate a large number of possible future asset prices at the option's maturity using the geometric Brownian motion formula. For each simulated outcome, you calculate the payoff of the call option, which is the greater of zero or the difference between the final asset price and the strike price. These payoffs are then discounted to present value using the risk-free interest rate, reflecting the time value of money. By averaging all discounted payoffs across simulations, you obtain an estimate of the fair price for the call option. The accuracy of this estimate improves as you increase the number of simulations, leveraging the law of large numbers.
Swipe to start coding
You will implement a function that prices a European put option using a Monte Carlo simulation with geometric Brownian motion (GBM).
The option payoff at maturity is:
max(K−ST,0)Your task:
-
Accept the following arguments:
S0— initial asset priceK— strike priceT— time to maturityr— risk-free interest ratesigma— volatilityn_sim— number of simulations
-
Simulate final asset prices using GBM:
ST=S0exp((r−.12σ2)T+σTZ)where Z∼N(0,1).
-
Compute the put payoff of each simulation.
-
Discount payoffs to present value:
Price=e−rT⋅E[payoff] -
Return the estimated option price as a float.
Use only numpy.
Solução
Obrigado pelo seu feedback!
single
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Can you explain what geometric Brownian motion is?
How does changing the volatility or interest rate affect the option price?
Can you show how to estimate the price of a European put option using Monte Carlo simulation?
Awesome!
Completion rate improved to 7.14
Toy Monte Carlo Pricing
Deslize para mostrar o menu
Monte Carlo simulation is a powerful tool for estimating the value of financial instruments when analytic pricing formulas are unavailable or impractical. A classic example is the European call option, which gives the holder the right (but not the obligation) to buy an asset at a specified strike price on a fixed future date. By simulating many possible future asset prices and averaging their payoffs, you can estimate the fair price of such an option. This approach is especially useful when the underlying asset's price follows a stochastic process, such as geometric Brownian motion.
123456789101112131415161718192021222324import numpy as np # Parameters for the simulation S0 = 100 # Initial asset price K = 105 # Strike price T = 1.0 # Time to maturity (in years) r = 0.05 # Risk-free interest rate sigma = 0.2 # Volatility of the asset n_sim = 10000 # Number of Monte Carlo simulations # Simulate final asset prices at maturity using geometric Brownian motion np.random.seed(42) # For reproducibility Z = np.random.randn(n_sim) ST = S0 * np.exp((r - 0.5 * sigma**2) * T + sigma * np.sqrt(T) * Z) # Calculate the payoff for each simulation (call option) payoffs = np.maximum(ST - K, 0) # Discount payoffs back to present value discounted_payoffs = np.exp(-r * T) * payoffs # Estimate the option price call_price = np.mean(discounted_payoffs) print(f"Estimated European call option price: {call_price:.2f}")
The logic behind this Monte Carlo pricing model is straightforward. First, you generate a large number of possible future asset prices at the option's maturity using the geometric Brownian motion formula. For each simulated outcome, you calculate the payoff of the call option, which is the greater of zero or the difference between the final asset price and the strike price. These payoffs are then discounted to present value using the risk-free interest rate, reflecting the time value of money. By averaging all discounted payoffs across simulations, you obtain an estimate of the fair price for the call option. The accuracy of this estimate improves as you increase the number of simulations, leveraging the law of large numbers.
Swipe to start coding
You will implement a function that prices a European put option using a Monte Carlo simulation with geometric Brownian motion (GBM).
The option payoff at maturity is:
max(K−ST,0)Your task:
-
Accept the following arguments:
S0— initial asset priceK— strike priceT— time to maturityr— risk-free interest ratesigma— volatilityn_sim— number of simulations
-
Simulate final asset prices using GBM:
ST=S0exp((r−.12σ2)T+σTZ)where Z∼N(0,1).
-
Compute the put payoff of each simulation.
-
Discount payoffs to present value:
Price=e−rT⋅E[payoff] -
Return the estimated option price as a float.
Use only numpy.
Solução
Obrigado pelo seu feedback!
single