© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
T. C. NokeriImplementing Machine Learning for Financehttps://doi.org/10.1007/978-1-4842-7110-0_7

7. Stock Market Simulation

Tshepo Chris Nokeri1  
(1)
Pretoria, South Africa
 

The stock exchange market typically responds to socio-economic conditions. The active participation of key market players undoubtedly creates liquidity in the market. There are other factors that influence variations in the market besides the large transactions that key market makers make. For instance, markets react to news of social events, economic events, natural disasters, pandemics, etc. In certain conditions, the effects of those events drive price movement drastically. Systematic investors (investors who base their trade execution on a system that depends on quantitative models) need to prepare for future occurrences to preserve capital by learning from near-real-life conditions. If you inspect industries that involve considerable risk, for example, aerospace and defense, you will notice that the subjects learn through simulation. Simulation is a method that involves generating conditions that mimic the actual world so that subjects know how to act and react in a condition similar to preceding ones. In finance, we deal with large funds. They habitually take risk into account when experimenting and testing models.

In this chapter, we implement a Monte Carlo simulation to simulate considerable changes in the market without exposing ourselves to risk. Simulating the market helps identify patterns in preceding occurrences and forecasts future prices with reasonable certainty. If we can reconstruct the actual world, then we can understand the preceding market behavior and predict future market behavior. In the preceding chapters, we sufficiently covered models for sequential pattern identification and forecasting. We use the panda_montecarlo framework to perform the simulation. To install it in the Python environment, use pip install pandas-montecarlo.

When an investor trades a stock, they expect their speculation to yield returns over a certain period. Unexpected events occur, and they can influence the direction of prices. To build sustained profitability over the long run, investors can develop models to recognize the underlying pattern of preceding occurrences and to forecast future occurrences.

Let’s assume you are a prospective conservative investor with an initial investment capital of $5 million US dollars. After conducting a comprehensive study, you have singled out a set of stocks to invest in. You can use Monte Carlo simulation to further understand the risks of investing in those assets.

Understanding Value at Risk

The most convenient way to estimate risk is by applying the value at risk (VAR). It reveals the degree of financial risk we are exposing an investment portfolio to. It shows the minimum capital required to compensate for losses at a specified probability level. There are two primary ways of estimating VAR; we can either use the variance-covariance method or simulate it by applying Monte Carlo.

Estimate VAR by Applying the Variance-Covariance Method

The variance-covariance method makes strong assumptions about the structure of the data. The method assumes the underlying structure of the data is linear and normal. It is also sensitive to missing data, nonlinearity, and outliers. To estimate the standard VAR, we first find the returns, and then we create the covariance matrix and find the mean value and standard deviation of the investment portfolio. Thereafter, we estimate the inverse of the normal cumulative distribution and compute the VAR. Figure 7-1 shows the VAR of a portfolio alongside the simulated distributions.
../images/509469_1_En_7_Chapter/509469_1_En_7_Fig1_HTML.png
Figure 7-1

VAR

Here we show you how to calculate the VAR by applying the variance/covariance calculation.

Assume:
  • Investment capital is $5,000,000.

  • Standard deviation from an annual trading calendar (252 days) is 9 percent.

  • Using the z-score (1.65) at 95 percent confidence interval, the value at risk is as follows:

  • $5,000,0000*1.645*.09 = $740 250

Listing 7-1 scrapes the stock prices for Amazon, Apple, Walgreens Boots Alliance, Northrop Grumman Corporation, Boeing Company, and Lockheed Martin Corporation (see Table 7-1).
from pandas_datareader import data
tickers = ['AMZN','AAPL','WBA',
           'NOC','BA','LMT']
start_date = '2010-01-01'
end_date = '2020-11-01'
df = data.get_data_yahoo(tickers, start_date, end_date)[['Adj Close']]
df.head()
Listing 7-1

Scraped Data

Table 7-1

Dataset

Attributes

Adj Close

Symbols

AMZN

AAPL

WBA

NOC

BA

LMT

Date

      

2010-01-04

133.899994

6.539882

28.798639

40.206833

43.441975

53.633926

2010-01-05

134.690002

6.551187

28.567017

40.277546

44.864773

54.192230

2010-01-06

132.250000

6.446983

28.350834

40.433144

46.225727

53.396633

2010-01-07

130.000000

6.435065

28.520689

40.850418

48.097031

51.931023

2010-01-08

133.520004

6.477847

28.559305

40.624100

47.633064

52.768509

Listing 7-2 specifies the investment weights of the portfolio.
weights = np.array([.25, .3, .15, .10, .24, .7])
Listing 7-2

Specify Investment Weights

From then on we specify the initial investment capital in the portfolio. See Listing 7-3.
initial_investment = 5000000
Listing 7-3

Specify Initial Investment

Listing 7-4 estimates the daily returns (see Table 7-2).
returns = df.pct_change()
returns.tail()
Listing 7-4

Estimate Daily Returns

Table 7-2

Daily Returns

Attributes

Adj Close

Symbols

AMZN

AAPL

WBA

NOC

BA

LMT

Date

      

2020-10-26

0.000824

0.000087

-0.021819

0.004572

-0.039018

-0.015441

2020-10-27

0.024724

0.013472

-0.032518

-0.024916

-0.034757

-0.016606

2020-10-28

-0.037595

-0.046312

-0.039167

-0.028002

-0.045736

-0.031923

2020-10-29

0.015249

0.037050

-0.030934

-0.004325

0.001013

0.004503

2020-10-30

-0.054456

-0.056018

0.015513

-0.008790

-0.026300

-0.006554

Table 7-2 highlights the first five rows of each stock’s daily return. Listing 7-5 estimates the joint variability between the stocks in the portfolio (see Table 7-3).
cov_matrix = returns.cov()
cov_matrix
Listing 7-5

Covariance Matrix

Table 7-3

Covariance Matrix

Attributes

 

Adj Close

 

Symbols

AMZN

AAPL

WBA

NOC

BA

LMT

Adj Close

AMZN

0.000401

0.000159

0.000096

0.000092

0.000135

0.000081

AAPL

0.000159

0.000318

0.000098

0.000098

0.000161

0.000093

WBA

0.000096

0.000098

0.000303

0.000097

0.000131

0.000088

NOC

0.000092

0.000098

0.000097

0.000204

0.000165

0.000150

BA

0.000135

0.000161

0.000131

0.000165

0.000486

0.000162

LMT

0.000081

0.000093

0.000088

0.000150

0.000162

0.000174

Listing 7-6 estimates the VAR. First, we specify the average daily returns, then we specify the confidence interval and cutoff value, and finally we obtain the mean and standard deviations. Subsequently, we find the inverse of the distribution.
conf_level1 = 0.05
avg_rets = returns.mean()
port_mean = avg_rets.dot(weights)
port_stdev = np.sqrt(weights.T.dot(cov_matrix).dot(weights))
mean_investment = (1+port_mean) * initial_investment
stdev_investment = initial_investment * port_stdev
cutoff1 = norm.ppf(conf_level1, mean_investment, stdev_investment)
var_1d1 = initial_investment - cutoff1
var_1d1
166330.5512926411
Listing 7-6

Estimate Value at Risk

At a 95 percent confidence interval, the investment portfolio of $5,000,000 will not exceed losses greater than 166,330.55 a day. Listing 7-7 prints the 10-day VAR.
var_arry = []
num_days = int(10)
for x in range(1, num_days+1):
    var_array.append(np.round(var_1d1 * np.sqrt(x),2))
    print(str(x) + " day VaR @ 95% confidence: " + str(np.round(var_1d1 * np.sqrt(x),2)))
1 day VaR @ 95% confidence: 166330.55
2 day VaR @ 95% confidence: 235226.92
3 day VaR @ 95% confidence: 288092.97
4 day VaR @ 95% confidence: 332661.1
5 day VaR @ 95% confidence: 371926.42
6 day VaR @ 95% confidence: 407424.98
7 day VaR @ 95% confidence: 440069.27
8 day VaR @ 95% confidence: 470453.84
9 day VaR @ 95% confidence: 498991.65
10 day VaR @ 95% confidence: 525983.39
Listing 7-7

Print 10-Day VAR

Listing 7-8 graphically represents the VAR over a 10-day period (see Figure 7-2).
plt.plot(var_array, color="navy")
plt.xlabel("No. of Days")
plt.ylabel("MaX Portfolio Loss (USD)")
plt.show()
Listing 7-8

10-Day VAR

../images/509469_1_En_7_Chapter/509469_1_En_7_Fig2_HTML.jpg
Figure 7-2

Max portfolio loss (VAR) over a 15-day period

Figure 7-2 shows that as we increase the number of days, the maximum investment portfolio increases too. In the next section, we explored Monte Carlo simulation.

Understanding Monte Carlo

Monte Carlo simulation uses resampling techniques to combat sequential problems. It reconstructs real-life conditions to identify and understand the results of the preceding occurrences and predict future occurrences. It also enables us to experiment with different investment strategies. Not only that, but it performs repetitive measurements on random variables that come from a normal distribution to determine the probability of each output, and then it assigns a confidence interval output.

Application of Monte Carlo Simulation in Finance

We use Monte Carlo simulation to evaluate a strategy’s rigorousness. It helps us determine whether the strategy is optimistic. An optimistic strategy stops yielding optimal returns when parameters of the environment are adjusted. It enables us to simulate the market and identify the risk we expose ourselves to. For this example, we focus on only one stock, the Northrop Grumman Corporation stock. See Listing 7-9 and Table 7-4.
start_date = '2010-11-01'
end_date = '2020-11-01'
ticker = 'NOC'
df = data.get_data_yahoo(ticker, start_date, end_date)
df['return'] = df['Adj Close'].pct_change().fillna(0)
df.head()
Listing 7-9

Scraped Data

Table 7-4

Dataset

Date

High

Low

Open

Close

Volume

Adj Close

return

2010-11-01

58.257679

56.974709

57.245762

57.381287

1736400.0

46.094521

0.000000

2010-11-02

58.438381

57.833035

57.833035

58.357063

1724000.0

46.878361

0.017005

2010-11-03

58.501625

57.354179

58.221539

58.076981

1598400.0

46.653362

-0.004800

2010-11-04

59.143108

58.140224

58.483555

58.971443

2213600.0

47.371891

0.015401

2010-11-05

59.260567

58.826885

58.926270

59.034691

1043900.0

47.422695

0.001072

Run Monte Carlo Simulation

Listing 7-10 applies the panda_montecarlo() method to run the Monte Carlo simulation with five simulations. Also, we set the bust/max drawdown to -10.0 percent and the goal threshold to +100.0 percent.
mc = df['return'].montecarlo(sims=10, bust=-0.1, goal=1)
Listing 7-10

Run the Monte Carlo Simulation

Plot Simulations

Listing 7-11 plots the 10 simulations that the Monte Carlo simulation ran (see Figure 7-3).
mc.plot(title="")
Listing 7-11

Simulations

../images/509469_1_En_7_Chapter/509469_1_En_7_Fig3_HTML.jpg
Figure 7-3

Monte Carlo simulations

Figure 7-3 shows the simulation results. It highlights a dominant upward trend. Listing 7-12 tabulates raw simulations (see Table 7-5).
pd.DataFrame(mc.data).head()
Listing 7-12

Raw Simulations

Table 7-5

Raw Simulations

 

Original

1

2

3

4

5

6

7

8

9

0

0.000000

-0.018168

-0.002659

-0.021098

-0.006357

0.018090

-0.010110

-0.006555

-0.004119

0.020557

1

0.017005

0.005521

-0.014646

0.000541

-0.009134

-0.000602

-0.006801

-0.003373

0.004342

-0.002257

2

-0.004800

-0.003900

-0.002494

0.027738

0.005924

-0.012834

-0.004095

-0.003315

0.000116

0.113851

3

0.015401

0.006688

0.001144

0.005586

-0.004924

0.011399

0.009817

-0.001273

0.006164

0.023355

4

0.001072

0.005617

0.010052

0.011748

0.007878

0.010080

-0.008666

-0.005126

0.036459

0.015438

Listing 7-13 returns the statistics of the simulation model (Table 7-6).
ev = pd.DataFrame(mc.stats, index=["s"])
ev
Listing 7-13

Monte Carlo Statistics

Table 7-6

Monte Carlo Statistics

 

min

max

mean

median

std

maxdd

bust

goal

s

2.094128

2.094128

2.094128

2.094128

2.145155e-15

-0.164648

0.2

0.8

Table 7-6 highlights the dispersion. It also highlights the maximum drawdown (the maximum amount of loss from a peak).

Conclusions

When investing in a stock or a set of stocks, it is important to understand, quantify, and mitigate the underlying risk in an investment portfolio. This chapter discussed VAR; thereafter, it showed ways to calculate the VAR of an investment portfolio, followed by simulating stock market changes by applying Monte Carlo simulation. The simulation technique has many applications; we can also use it for asset price structuring, etc. The next chapter further expands on the investment portfolio and risk analysis.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.186.92