代写data编程、代做Python/Java程序
- 首页 >> Python编程 Objective:
‧ Applications of Queues
‧ Explain various fundamental abstract data types (ADTs), such as queues and its application
Introduction:
In the financial market, market participants need to price the value of a financial instrument called option.
In simple wordings, option’s price ( or premium ) is a function of the probability of a particular event (in‑the‑money) happening.
Though the premium can be calculated by a mathematic formula with some assumptions, we can calculate it by an alternate method called Monte Carlo simulation.
The following are the basic steps in Monte Carlo simulation.
1.Define a domain of possible inputs
2.Generate inputs randomly from a probability distribution over the domain
3.Perform a deterministic computation on the inputs
4.Aggregate the results
In this exercise, we will run a Monte Carlo simulation and calculate the stock option price. In the simulation, the Queue ADT is used. Queue data structure is often used in Monte Carlo simulation because of its FIFO nature.
Environment:
‧ PC installed with Visual Studio Code and Python 3.x.
Implementation
Aclass OptionSimulator isprovidedin option.py whichhascodefortherequiredstochasticfunctions used in the simulation of the stock random walk.
Instruction for Step 2:
You can call the following function to get a random number which follows the probability distribution for stock movements.
def _get_random_number(self):
1
Instruction for Step 3:
You need to fill in the function def simulate(self) for step 3.
self._num_of_walks
OptionSimulator .
First, you need to generate a number, which is defined in, of random numbers and enqueue them in the Queue self._queue of the class
During the simulation, those random numbers are dequeued from the Queue, applied one‑by‑one on the previous stock price and mimic the stock’s random walk.
Please note that the stochastic process is a log‑normal distribution, therefore, every step of the random walk is calculated by
𝑙𝑛(𝑆𝑡) = 𝑙𝑛(𝑆𝑡−1) + 𝑟𝑎𝑛𝑑𝑜𝑚 𝑛𝑢𝑚𝑏𝑒𝑟
After the simulation finishes, you will get a series of steps
{𝑙𝑛(𝑆0),𝑙𝑛(𝑆1),𝑙𝑛(𝑆2),...,𝑙𝑛(𝑆𝑇)}
and you need to get the values of
𝑆0,𝑆1,𝑆2,...,𝑆𝑇
by the exponential function. In Python, you can use the module numpy exponential function for the calculation.
numpy.exp( )
Then, you can get the option payoff on the expiry date by the function payoff_at_last()
Instruction for Step 4:
We run the simulation multiple times and get a list of payoffs.
Then we can calculate the option price by finding the present value of the expected payoffs.
Appendix
This section includes the background knowledge about the theory and the mathematical model used in the Python program. You may read them if you are interested in these topics, but they are not required to complete this assignment.
What is Monte Carlo simulation?
Monte Carlo methods are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. The underlying concept is to use randomness to solve problems that might be deterministic in principle. The name comes from the Monte Carlo Casino in Monaco, where the primary developer of the method, physicist Stanislaw Ulam, was inspired by his uncle’s gambling habits. ( per Wikipedia.org )
What is Option?
A stock option gives an investor the right, but not the obligation, to buy or sell a stock at an agreed‑upon price and date. ( Source from Investopedia.com) The payoff of a call option on the expiry date is as follows.
max(0,𝑆𝑡 − 𝐸)
where
𝑆𝑡 = Stock price on the expiry date
𝐸 = Strike/Exercise price
Random Walk
Financial literature stocks are said to follow geometric brownian motion
AgeometricBrownianmotion(GBM)(alsoknownasexponentialBrownianmotion)isacontinuous‑time stochastic process in which the logarithm of the randomly varying quantity follows a Brownian motion (also called a Wiener process) with drift. It is an important example of stochastic processes satisfying a stochastic differential equation (SDE)
𝑑𝑆 = 𝑆𝜇𝑑𝑡 + 𝑆𝜎𝑑𝑊(𝑡)
𝑡
𝑙𝑛(𝑆𝑇) = 𝑙𝑛(𝑆𝜎𝑑𝑊(𝑡)
Brownian motion
Brownian motion is the random motion of particles suspended in a medium. Yes, this idea is from the Physic, but applied in Finance.
ThepictureaboveshowsaBrownianmotionofalargeparticle,analogoustoadustparticle,thatcollides withalargesetofsmallerparticles,analogoustomoleculesofagas,whichmovewithdifferentvelocities
in different random directions.
( Source from Wikipedia.org )
–End—
‧ Applications of Queues
‧ Explain various fundamental abstract data types (ADTs), such as queues and its application
Introduction:
In the financial market, market participants need to price the value of a financial instrument called option.
In simple wordings, option’s price ( or premium ) is a function of the probability of a particular event (in‑the‑money) happening.
Though the premium can be calculated by a mathematic formula with some assumptions, we can calculate it by an alternate method called Monte Carlo simulation.
The following are the basic steps in Monte Carlo simulation.
1.Define a domain of possible inputs
2.Generate inputs randomly from a probability distribution over the domain
3.Perform a deterministic computation on the inputs
4.Aggregate the results
In this exercise, we will run a Monte Carlo simulation and calculate the stock option price. In the simulation, the Queue ADT is used. Queue data structure is often used in Monte Carlo simulation because of its FIFO nature.
Environment:
‧ PC installed with Visual Studio Code and Python 3.x.
Implementation
Aclass OptionSimulator isprovidedin option.py whichhascodefortherequiredstochasticfunctions used in the simulation of the stock random walk.
Instruction for Step 2:
You can call the following function to get a random number which follows the probability distribution for stock movements.
def _get_random_number(self):
1
Instruction for Step 3:
You need to fill in the function def simulate(self) for step 3.
self._num_of_walks
OptionSimulator .
First, you need to generate a number, which is defined in, of random numbers and enqueue them in the Queue self._queue of the class
During the simulation, those random numbers are dequeued from the Queue, applied one‑by‑one on the previous stock price and mimic the stock’s random walk.
Please note that the stochastic process is a log‑normal distribution, therefore, every step of the random walk is calculated by
𝑙𝑛(𝑆𝑡) = 𝑙𝑛(𝑆𝑡−1) + 𝑟𝑎𝑛𝑑𝑜𝑚 𝑛𝑢𝑚𝑏𝑒𝑟
After the simulation finishes, you will get a series of steps
{𝑙𝑛(𝑆0),𝑙𝑛(𝑆1),𝑙𝑛(𝑆2),...,𝑙𝑛(𝑆𝑇)}
and you need to get the values of
𝑆0,𝑆1,𝑆2,...,𝑆𝑇
by the exponential function. In Python, you can use the module numpy exponential function for the calculation.
numpy.exp( )
Then, you can get the option payoff on the expiry date by the function payoff_at_last()
Instruction for Step 4:
We run the simulation multiple times and get a list of payoffs.
Then we can calculate the option price by finding the present value of the expected payoffs.
Appendix
This section includes the background knowledge about the theory and the mathematical model used in the Python program. You may read them if you are interested in these topics, but they are not required to complete this assignment.
What is Monte Carlo simulation?
Monte Carlo methods are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. The underlying concept is to use randomness to solve problems that might be deterministic in principle. The name comes from the Monte Carlo Casino in Monaco, where the primary developer of the method, physicist Stanislaw Ulam, was inspired by his uncle’s gambling habits. ( per Wikipedia.org )
What is Option?
A stock option gives an investor the right, but not the obligation, to buy or sell a stock at an agreed‑upon price and date. ( Source from Investopedia.com) The payoff of a call option on the expiry date is as follows.
max(0,𝑆𝑡 − 𝐸)
where
𝑆𝑡 = Stock price on the expiry date
𝐸 = Strike/Exercise price
Random Walk
Financial literature stocks are said to follow geometric brownian motion
AgeometricBrownianmotion(GBM)(alsoknownasexponentialBrownianmotion)isacontinuous‑time stochastic process in which the logarithm of the randomly varying quantity follows a Brownian motion (also called a Wiener process) with drift. It is an important example of stochastic processes satisfying a stochastic differential equation (SDE)
𝑑𝑆 = 𝑆𝜇𝑑𝑡 + 𝑆𝜎𝑑𝑊(𝑡)
𝑡
𝑙𝑛(𝑆𝑇) = 𝑙𝑛(𝑆𝜎𝑑𝑊(𝑡)
Brownian motion
Brownian motion is the random motion of particles suspended in a medium. Yes, this idea is from the Physic, but applied in Finance.
ThepictureaboveshowsaBrownianmotionofalargeparticle,analogoustoadustparticle,thatcollides withalargesetofsmallerparticles,analogoustomoleculesofagas,whichmovewithdifferentvelocities
in different random directions.
( Source from Wikipedia.org )
–End—