top of page

Simulating Stock Price Data with Python

Writer's picture: Manogane SydwellManogane Sydwell

Updated: Sep 21, 2020

In a previous article, we discussed what the normal distribution is and provided an illustration through the discussion of the heights of individuals. Additionally, a tool for readers to simulate their own normal distributions was provided. Having grasped the fundamentals of the normal distributions and a means of using it in Python, we will now use it to simulate stock market data.


But it is not enough to just simulate prices without understanding the why behind results of the process. The first reason to simulate stock prices would be that after an individual develops a model to predict time series of any sort, testing the model's performance on synthetic data would provide a good benchmark to measure the model's performance against when the model is finally used on historical data.


Another reason would be that producing synthetic data is key to a process known as Monte Carlo Simulation, which is kind of important in the world of finance. This technique generates random variables for modelling risk or the uncertainty of a certain system. You can read more about it here. Or maybe one would simulate stock market data because one’s female companions are into that type of stuff too! The list is potentially endless!

Now that the why part is elucidated succinctly, let's take a look at the code. The theme followed in the code presented below will illustrate the benefits of diversification.


Taking a look at the code

First and foremost, it is important to mention that the stock returns of the fictitious companies mentioned in this part of the blog are normally distributed. The first security, CovidProof, is a prominent company in the microprocessing industry. Its returns (or rather the code the produces those returns) are presented below.

CovidProof_initialPrice = 50
CocidProof = NormalRandomVariable(0.85,12)
CovidProof_returns = X_two.draw(100)
CovidProof_CurrentPirce = pd.Series(np.cumsum(Z_returns), name = "Z") + Z_initial
plt.figure(figsize=(15,8))
Z.plot()
plt.xlabel('Time')
plt.ylabel('Value')

The second security, a company called Outdated Fuel Manufacturer, is a company that produces petroleum through fossil fuels. This company’s returns are also normally distributed. The code that produces the company's returns is provided below:

OutdatedFuelMan_Price = 50
OutdatedFuel_Man = NormalRandomVariable(-0.65,12) 
OutdatedFuel_Man_returns = X_two.draw(100)
OutdatedFuel_Man_CurrentPirce = pd.Series(np.cumsum(Z_returns), name = "Z") + Z_initial
plt.figure(figsize=(15,8))
Z.plot()
plt.xlabel('Time')
plt.ylabel('Value')

We presume that the returns for both these companies are for 2020. We now provide the returns of a joint portfolio consisting of both CovidProof and Outdated Fuel Manufacturer. 

#The number of shares invested in the respective companies
OutdatedFuelMan_quantity = 20
CovidProof_quantity = 50

#The weight of Outdated Fuel Manufacturer in the Portfolio
OutdatedFuelMan_weight = OutdatedFuelMan_quantity/(OutdatedFuelMan_quantity + CovidProof_quantity)

#The weight of CovidProof in the Portfolio
CovidProof_weight = 1 - OutdatedFuelMan_weight

plt.figure(figsize=(15,8))

#The initial value of the portfolio
Portfolio_initial = 0.5 * OutdatedFuelMan_initial + 0.5 * CovidProof_initial

#The returns of the portfolio after some time has elapsed
Portfolio_returns = 0.4 * OutdatedFuelMan_returns + 0.6 * CovidProof_returns

#The cumulative value of the portfolio after some time has elapsed
Portfolio = pd.Series(np.cumsum(W_returns), name = 'Portfolio') + Portfolio_initial

#Plotting the data
W.plot()
plt.xlabel('Time')
plt.ylabel("Value")

By placing a investment of 60% of the portfolio value in CovidProof and the remaining balance in Outdated Fuel Manufacturer, we are able to realize an increase in portfolio value during a global market panic like the one experienced earlier during the year. Albeit this scenario is a hypothetical one, it does help explain the importance of diversification. Also simulating this data in Python looks super cool so why not!

Conclusion

This article shows that Random Variables are not just abstract ideas with no real-world applications. By understanding random variables, one is able to apply financial models to different types of data in order to evaluate how models behave under different market regimes. This helps firms in the financial world to be better prepared for turmoil caused by happenings like the Corona Virus. Complement this article with the following video


References

The Normal Distribution in Python, iridiscentcapitalprojects.com

Time Series, Wikipedia.com

Benefits of Diversification, dixon.com

Random Variables, Quantopian.com

Monte Carlo Simulation, economictimesindia.com


13 views0 comments

Recent Posts

See All

Comentários


©2020 by creativeAfricanProjects.

bottom of page