Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Monte Carlo to simulate option pricing in Python

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of Python how to use Monte Carlo to simulate option pricing, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Python article on how to use Monte Carlo to simulate option pricing. Let's take a look.

An option is a contract that gives the buyer the right to buy and sell assets at a specific price at a certain point in the future. These contracts, known as derivatives, are traded for a variety of reasons, but a common use is to hedge exposure when asset prices move unfavourably.

Options, the right to buy or sell, also have a price. The Black Scholes model describes a method to determine the fair price of options, but there are many other ways to determine the price.

Options, and their value

European options can only be used (or exercised) after they reach the scheduled date (called expiration date) in the future, which can be expressed by the letter T.

A call option gives the option holder the right to buy at a known price. If the maturity price of the asset (represented by ST) is higher than the strike price K, the call option will make money, otherwise it will be worthless.

CT=max (0mai St − K)

Similarly, put options are the right to sell assets. When the maturity price ST is lower than the strike price K, the bearish option will make money, otherwise it will be worthless.

PT=max (0mai K − ST)

The following is the yield chart of put and call options at maturity. Our asset price is the x-axis and the return is the y-axis.

Risk neutral valuation

In order to use Monte Carlo simulation to price options, we use risk-neutral valuation, in which the fair value of derivatives is the expected value of their future earnings.

Therefore, on any date before maturity, expressed as t, the value of the option is the expected present value T of its maturity earnings.

Ct=PV (E [max (0st St − K)])

Pt=PV (E [max (0m K − ST)])

Under the risk-neutral valuation, we assume that the underlying asset will receive an average risk-free interest rate. Therefore, in order to calculate the option return of t at any time, we need to discount the return at that interest rate. Now we have a way to calculate the present value of PV.

In the above formula, all of these variables are known except St, so St is what our simulation will provide.

To price options, we will create a simulation that provides many observations for the final price of the asset St. By averaging all returns, we get the expected value of return.

Simulated asset price

The stock price behavior model used in the Black Scholes model assumes that we have a known volatility, we have a risk-free interest rate, and the price of the asset follows the geometric Brownian motion.

Geometric Brownian motion is a random process in which the logarithm of random variables obeys normal distribution. This type of process allocates prices through lognormal distribution.

So now we have a way to calculate the asset price at the time T:

To do this, we need to know:

R is the risk-free rate we want to discount. σ is volatility, that is, the annualized standard deviation of stock returns. (Tmurt) gives us an annualized expiration date. For example, for the 30-day option, this would be 30max 365 seconds 0.082. S is the price of the underlying asset at time t. Random is our random value. Its distribution must be standard normal (mean 0.0, standard deviation 1.0)

Option pricing

In order to price options during the simulation, we generate many prices at which the asset may expire, calculate the option returns for each generated price, average them, and then discount the final value.

Before creating the full simulation, we will go through a small example that contains 10 runs. Suppose we have an asset with the following value: s = $100.00 and σ = 20%. We want to price half-yearly maturing call options at a strike price of $110.00 and our risk-free interest rate is 1%.

Random variable asset price return discount return 1.3620120.6410.58-0.778489.130.000.00-0.940887.110.000.2227102.690.000.00-0.036498.990.000.00-1.430381.280.000.00-0.830688.470.001.5155123.2813.21-1.5679710.000.00-1.671878.550.000.00

By averaging the discount income value, we get that our call option price is $2.38. The more simulations we perform, the more accurate the price will be.

Now that we can see how the simulation generates the price, let's build a small Python script that can price options and see if it matches the real situation. Let's look at a practical example.

Pricing real options

In the figure below, we have an offer for a Google call option with an exercise price of $860.00 that expires on September 21, 2013. We can also see that its final trading price is $14.50. This example gives us a target price for options when we try to price them.

What is not specified here is volatility, risk-free interest rate, and current stock price. Volatility is a rather complex topic, so for the purpose of this article, we will assume that we know that the volatility of that particular option is 20.76%. The current price of the stock can be found by looking at a variety of sources, at $857.29.

For the risk-free rate, we can use the same US LIBOR rate as the expiration time we chose; our options expire in about three weeks, and since there is no three-week interest rate, we will use a two-week interest rate, that is, 0.14%.

Next is the implementation of the Python code. First we will write down how we will generate the asset price.

Def generate_asset_price: return S * exp * T + v * sqrt (T) * gauss (0Magol 1.0)

We know all the input values, so we can set them like this:

S = 857.29 # underlying pricev = 0.2076 # vol of 20.76% r = 0.0014 # rate of 0.14% T = (datetime.date (2013 rate of 9)-datetime.date (2013 9)). Days / 365.0print generate_asset_price (Stemery vJournal T) > > 862.1783726682384

Now we need to be able to calculate the return of this generated price. Recall that we said earlier that the value of the call option is ST-K or 0 when it expires, and we express it as a function and apply it to the asset price we generate.

Def call_payoff (slott, K): return max (slott-K, 0.0) print call_payoff (862.18) > 2.1799999999 complete simulation

Now let's combine the modules and price the Google options.

Import datetimefrom random import gaussfrom math import exp, sqrtdef generate_asset_price: return S * exp ((r-0. 5 * vault 2) * T + v * sqrt (T) * gauss (0. 0. 0)) def call_payoff: return max (0. 0penny T) S = 857.29 # underlying pricev = 0.2076 # vol of 20.76% r = 0.0014 # rate of 0.14% T = (datetime.date (2013 Let9) 21)-datetime.date). Days / 365.0K = 860.simulations = 90000payoffs = [] discount_factor = math.exp (- r * T) for i in xrange (simulations): slat = generate_asset_price (Stemery vReagt T) payoffs.append (call_payoff (Scrit, K)) price = discount_factor * (sum (payoffs) / float (simulations)) print 'Price:% .4f'% price

The program runs as follows, which matches the price of this Google option we have observed in the market.

Price: 14.5069

It should be noted that the Google option we just calculated is actually an American option. We just priced it as an European option without considering the possibility that the option can be exercised in advance. Nevertheless, we still got the right price.

This is because the price of American call options for non-dividend stocks (such as Google) is the same as that of European call options. In theory, early exercise is not the best option when stocks do not pay dividends. If the option is never exercised in advance, the price of the American option can be calculated like an European option.

This is the end of the article on "how Python uses Monte Carlo to simulate option pricing". Thank you for reading! I believe you all have a certain understanding of "how Python uses Monte Carlo to simulate option pricing". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report