-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProject-Stock Market Analysis and Prediction.py
937 lines (512 loc) · 26.4 KB
/
Project-Stock Market Analysis and Prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# coding: utf-8
# # Project: Stock Market Analysis and Prediction
#
# ## Stock Market Data From Google Finance
#
# ### Introduction
#
# Stock Market Analysis and Prediction is the project on technical analysis, visualization and prediction using data provided by Google Finance. By looking at data from the stock market, particularly some giant technology stocks and others. Used pandas to get stock information, visualize different aspects of it, and finally looked at a few ways of analyzing the risk of a stock, based on its previous performance history. Predicted future stock prices through a Monte Carlo method!
#
# ### Questions
#
# In this analysis, I would like to explore the following questions.
#
# 1. What was the change in price of the stock over time?
# 2. What was the daily return of the stock on average?
# 3. What was the moving average of the various stocks?
# 4. What was the correlation between different stocks' closing prices?
# 4. What was the correlation between different stocks' daily returns?
# 5. How much value do we put at risk by investing in a particular stock?
# 6. How can we attempt to predict future stock behavior?
# In[1]:
# For Data Processing
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
# Data Visualization
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
get_ipython().magic('matplotlib inline')
# In[2]:
# For reading stock data from yahoo
from pandas_datareader import DataReader
# For time stamps
from datetime import datetime
# For division
from __future__ import division
# #### Section 1 - Basic Analysis of Stock Information
#
#
# In this section I'll go over how to handle requesting stock information with pandas, and how to analyze basic attributes of a stock.
#
# In[3]:
# List of Tech_stocks for analytics
tech_list = ['AAPL','GOOGL','MSFT','AMZN']
# set up Start and End time for data grab
end = datetime.now()
start = datetime(end.year-1,end.month,end.day)
#For-loop for grabing google finance data and setting as a dataframe
# Set DataFrame as the Stock Ticker
for stock in tech_list:
globals()[stock] = DataReader(stock,'google',start,end)
# Quick note: Using globals() is a sloppy way of setting the DataFrame names, but its simple
#
# Let's go ahead and play aorund with the AAPL(Apple) Stock DataFrame to get a feel for the data.
# In[4]:
AAPL.head()
# In[5]:
# Summery stats for Apple Stock
AAPL.describe()
# In[14]:
# General Info
AAPL.info()
# Now that we've seen the DataFrame, let's go ahead and plot out the volume and closing price of the AAPL(Apple) stocks.
# In[15]:
# Let's see a historical view of the closing price
AAPL['Close'].plot(legend=True, figsize=(10,4))
# In[18]:
# Now let's plot the total volume of stock being traded each day over the past year
AAPL['Volume'].plot(legend=True, figsize=(10,4))
# We can see that on Feb'2017 was the higher for AAPL stock being traded.
# Now that we've seen the visualizations for the closing price and the volume traded each day for AAPL stock.
# Let's go ahead and caculate the moving average for the AAPL stock.
#
# For more info on the Moving Average(SMA & EMA) check out the following links:
#
# 1.) http://www.investopedia.com/terms/m/movingaverage.asp
#
# 2.) http://www.investopedia.com/articles/active-trading/052014/how-use-moving-average-buy-stocks.asp
# In[19]:
# Pandas has a built-in rolling mean calculator
# Let's go ahead and plot out several moving averages
MA_day = [10,20,50,100]
for ma in MA_day:
column_name = 'MA for %s days' %(str(ma))
AAPL[column_name] = pd.rolling_mean(AAPL['Close'],ma)
# Now, lets plot all the additional Moving Averages for AAPL stock
# In[20]:
AAPL[['Close','MA for 10 days','MA for 20 days','MA for 50 days','MA for 100 days']].plot(subplots=False,figsize=(10,4))
# #### Section 2 - Daily Return Analysis
#
# Now, that we've done some baseline analysis, let's go ahead and dive a little deeper. We're now going to analyze the risk of the stock.
#
#
# In order to do so, we need to take a closer look at the daily changes of the stock, and not just its absolute value. Let's go ahead and use pandas to retrieve the daily returns for the APPL stock.
#
# In[13]:
# We'll use pct_change to find the percent change for each day
AAPL['Daily Return'] = AAPL['Close'].pct_change()
# Lets plot the daily return percentage
AAPL['Daily Return'].plot(figsize=(12,4), legend=True, linestyle='--', marker='o')
# Great, now let's get an overall look at the average daily return using a histogram. By using seaborn to create both a histogram and kde plot on the same figure.
# In[17]:
# only with histogram
AAPL['Daily Return'].hist(bins=100)
# In[15]:
# Note the use of dropna() here, otherwise the NaN values can't be read by seaborn
sns.distplot(AAPL['Daily Return'].dropna(), bins=100, color='magenta')
# Now what if we wanted to analyze the returns of all the stocks in our list? For that, we need to build a DataFrame with all the ['Close'] columns for each of the stocks dataframes.
# In[8]:
# Grab all the closing prices for the tech stock list into one DataFrame
closingprice_df = DataReader(tech_list, 'google', start, end)['Close']
# In[19]:
closingprice_df.head(10)
# Now that we have all the closing prices, let's go ahead and get the daily return for all the stocks, like we did for the APPL stock.
# In[9]:
# make a new tech returns DataFrame
tech_returns = closingprice_df.pct_change()
# In[8]:
tech_returns.head()
# Now we can compare the daily percentage return of two stocks to check how correlated. First let's see a stock compared to itself.
#
# ##### GOOGL is a Alphabet Inc Class A Stock.
# In[22]:
# Comparing Google to itself should show a perfectly linear relationship
sns.jointplot('GOOGL','GOOGL',tech_returns,kind='scatter',color='orange')
# So now we can see that if two stocks are perfectly (and positivley) correlated with each other a linear relationship bewteen its daily return values should occur.
#
# So let's go ahead and compare Google and Amazon the same way.
# In[29]:
# We'll use joinplot to compare the daily returns of Google and Amazon.
sns.jointplot('GOOGL','AMZN',tech_returns, kind='scatter',size=8, color='skyblue')
# In[28]:
# with Hex plot
sns.jointplot('GOOGL','AMZN',tech_returns, kind='hex',size=8, color='skyblue')
# In[32]:
# Lets check out for Apple and Microsoft with reg jointplot
sns.jointplot('AAPL','MSFT',tech_returns, kind='reg', size=8, color='skyblue')
# Intersting, the pearsonr value (officially known as the Pearson product-moment correlation coefficient) can give you a sense of how correlated the daily percentage returns are. You can find more information about it at this link:
#
# Url - http://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient
#
# But for a quick intuitive sense, check out the picture below.
# In[11]:
from IPython.display import SVG
SVG(url='http://upload.wikimedia.org/wikipedia/commons/d/d4/Correlation_examples2.svg')
# Seaborn and Pandas make it very easy to repeat this comparison analysis for every possible combination of stocks in our technology stock ticker list. We can use sns.pairplot() to automatically create this plot
# In[40]:
# We can simply call pairplot on our DataFrame for an automatic visual analysis of all the comparisons
sns.pairplot(tech_returns.dropna(),size=3)
# Above we can see all the relationships on daily returns between all the stocks. A quick glance shows an interesting correlation between Google and Amazon daily returns. It might be interesting to investigate that individual comaprison. While the simplicity of just calling sns.pairplot() is fantastic we can also use sns.PairGrid() for full control of the figure, including what kind of plots go in the diagonal, the upper triangle, and the lower triangle.
#
# Below is an example of utilizing the full power of seaborn to achieve this result.
# In[41]:
# Set up the figure by naming it returns_fig, call PairGrid on the DataFrame
returns_fig = sns.PairGrid(tech_returns.dropna())
# Using map_upper we can specify what the upper triangle will look like.
returns_fig.map_upper(plt.scatter,color='purple')
# We can also define the lower triangle in the figure, including the plot type (kde) & the color map (BluePurple)
returns_fig.map_lower(sns.kdeplot,cmap='cool_d')
# Finally we'll define the diagonal as a series of histogram plots of the daily return
returns_fig.map_diag(plt.hist,bins=30)
# We can also analyze the correlation of the closing prices using this exact same technique. Here it is shown, the code repeated from above with the exception of the DataFrame called.
# In[44]:
# Set up the figure by naming it returns_fig, call PairGrid on the DataFrame
returns_fig = sns.PairGrid(closingprice_df.dropna())
# Using map_upper we can specify what the upper triangle will look like.
returns_fig.map_upper(plt.scatter,color='purple')
# We can also define the lower triangle in the figure, including the plot type (kde) & the color map (BluePurple)
returns_fig.map_lower(sns.kdeplot,cmap='cool_d')
# Finally we'll define the diagonal as a series of histogram plots of the daily return
returns_fig.map_diag(plt.hist,bins=30)
# Finally, we can also do a correlation plot, to get actual numerical values for the correlation between the stocks' daily return values. By comparing the closing prices, we see an interesting relationship between Google and Amazon stocks.
# In[76]:
# Let's go ahead and use seaborn for a quick heatmap to get correlation for the daily return of the stocks.
sns.heatmap(tech_returns.corr(),annot=True,fmt=".3g",cmap='YlGnBu')
# In[79]:
# Lets check out the correlation between closing prices of stocks
sns.heatmap(closingprice_df.corr(),annot=True,fmt=".3g",cmap='YlGnBu')
# ##### Fantastic! Just like we suspected in our PairPlot we see here numerically and visually that Amazon and Google had the strongest correlation of daily stock return. It's also interesting to see that all the technology comapnies are positively correlated.
# Great! Now that we've done some daily return analysis, let's go ahead and start looking deeper into actual risk analysis.
# ### Risk Analysis
# There are many ways we can quantify risk, one of the most basic ways using the information we've gathered on daily percentage returns is by comparing the expected return with the standard deviation of the daily returns(Risk).
# In[10]:
# Let's start by defining a new DataFrame as a clenaed version of the oriignal tech_returns DataFrame
rets = tech_returns.dropna()
# In[10]:
rets.head()
# In[94]:
# Defining the area for the circles of scatter plot to avoid tiny little points
area = np.pi*20
plt.scatter(rets.mean(),rets.std(),s=area)
# Set the x and y limits of the plot (optional, remove this if you don't see anything in your plot)
plt.xlim([-0.0025,0.0025])
plt.ylim([0.001,0.025])
#Set the plot axis titles
plt.xlabel('Expected returns')
plt.ylabel('Risk')
# Label the scatter plots, for more info on how this is done, chekc out the link below
# http://matplotlib.org/users/annotations_guide.html
for label, x, y in zip(rets.columns, rets.mean(), rets.std()):
plt.annotate(
label,
xy = (x, y), xytext = (50, 50),
textcoords = 'offset points', ha = 'right', va = 'bottom',
arrowprops = dict(arrowstyle = 'fancy', connectionstyle = 'arc3,rad=-0.3'))
# By looking at the scatter plot we can say these stocks have lower risk and positive expected returns.
# ### Value at Risk
# Let's go ahead and define a value at risk parameter for our stocks. We can treat value at risk as the amount of money we could expect to lose (aka putting at risk) for a given confidence interval. There's several methods we can use for estimating a value at risk. Let's go ahead and see some of them in action.
#
# #### Value at risk using the "bootstrap" method
# For this method we will calculate the empirical quantiles from a histogram of daily returns. For more information on quantiles, check out this link: http://en.wikipedia.org/wiki/Quantile
#
# Let's go ahead and repeat the daily returns histogram for Apple stock.
# In[14]:
# Note the use of dropna() here, otherwise the NaN values can't be read by seaborn
sns.distplot(AAPL['Daily Return'].dropna(),bins=100,color='purple')
# Now we can use quantile to get the risk value for the stock.
# In[17]:
# The 0.05 empirical quantile of daily returns
# For APPL stocks
rets["AAPL"].quantile(0.05)
# The 0.05 empirical quantile of daily returns is at -0.016. That means that with 95% confidence, our worst daily loss will not exceed 1.6%. If we have a 1 million dollar investment, our one-day 5% VaR is 0.016 * 1,000,000 = $16,000.
# In[18]:
# For AMZN stocks
rets["AMZN"].quantile(0.05)
# In[19]:
# For GOOGL stocks
rets["GOOGL"].quantile(0.05)
# In[20]:
# For MSFT stocks
rets["MSFT"].quantile(0.05)
# #### Value at Risk using the Monte Carlo method
# Using the Monte Carlo to run many trials with random market conditions, then we'll calculate portfolio losses for each trial. After this, we'll use the aggregation of all these simulations to establish how risky the stock is.
#
# Let's start with a brief explanation of what we're going to do:
#
# We will use the geometric Brownian motion (GBM), which is technically known as a Markov process. This means that the stock price follows a random walk and is consistent with (at the very least) the weak form of the efficient market hypothesis (EMH): past price information is already incorporated and the next price movement is "conditionally independent" of past price movements.
#
# This means that the past information on the price of a stock is independent of where the stock price will be in the future, basically meaning, you can't perfectly predict the future solely based on the previous price of a stock.
#
# Now we see that the change in the stock price is the current stock price multiplied by two terms. The first term is known as "drift", which is the average daily return multiplied by the change of time. The second term is known as "shock", for each time period the stock will "drift" and then experience a "shock" which will randomly push the stock price up or down. By simulating this series of steps of drift and shock thousands of times, we can begin to do a simulation of where we might expect the stock price to be.
#
# For more info on the Monte Carlo method for stocks and simulating stock prices with GBM model ie. geometric Brownian motion (GBM).
#
# check out the following link: http://www.investopedia.com/articles/07/montecarlo.asp
# To demonstrate a basic Monte Carlo method, we will start with just a few simulations. First we'll define the variables we'll be using in the Google stock DataFrame GOOGL
#
# In[21]:
rets.head()
# In[23]:
# Set up our time horizon
days = 365
# Now our delta
dt = 1/days
# Now let's grab our mu (drift) from the expected return data we got for GOOGL
mu = rets.mean()['GOOGL']
# Now let's grab the volatility of the stock from the std() of the average return for GOOGL
sigma = rets.std()['GOOGL']
# Next, we will create a function that takes in the starting price and number of days, and uses the sigma and mu we already calculated form our daily returns.
# In[24]:
def stock_monte_carlo(start_price,days,mu,sigma):
''' This function takes in starting stock price, days of simulation,mu,sigma, and returns simulated price array'''
# Define a price array
price = np.zeros(days)
price[0] = start_price
# Schok and Drift
shock = np.zeros(days)
drift = np.zeros(days)
# Run price array for number of days
for x in range(1,days):
# Calculate Schock
shock[x] = np.random.normal(loc=mu * dt, scale=sigma * np.sqrt(dt))
# Calculate Drift
drift[x] = mu * dt
# Calculate Price
price[x] = price[x-1] + (price[x-1] * (drift[x] + shock[x]))
return price
# Awesome! Now lets put above function to work.
# In[27]:
# For Google Stock - GOOGL
GOOGL.head()
# In[28]:
start_price = 830.09
for run in range(100):
plt.plot(stock_monte_carlo(start_price, days, mu, sigma))
plt.xlabel("Days")
plt.ylabel("Price")
plt.title('Monte Carlo Analysis for Google')
# In[29]:
# For Amazon Stock - AMZN
AMZN.head()
# In[31]:
start_price = 824.95
for run in range(100):
plt.plot(stock_monte_carlo(start_price, days, mu, sigma))
plt.xlabel("Days")
plt.ylabel("Price")
plt.title('Monte Carlo Analysis for Amazon')
# In[33]:
# For Apple Stock - AAPL
AAPL.head()
# In[34]:
start_price = 117.10
for run in range(100):
plt.plot(stock_monte_carlo(start_price, days, mu, sigma))
plt.xlabel("Days")
plt.ylabel("Price")
plt.title('Monte Carlo Analysis for Apple')
# In[35]:
# For Microsoft Stock - MSFT
MSFT.head()
# In[36]:
start_price = 59.94
for run in range(100):
plt.plot(stock_monte_carlo(start_price, days, mu, sigma))
plt.xlabel("Days")
plt.ylabel("Price")
plt.title('Monte Carlo Analysis for Microsoft')
# Let's go ahead and get a histogram of the end results for a much larger run. (note: This could take a little while to run , depending on the number of runs chosen)
# In[53]:
# Lets start with Google stock price
start_price = 830.09
# Set a large numebr of runs
runs = 10000
# Create an empty matrix to hold the end price data
simulations = np.zeros(runs)
for run in range(runs):
# Set the simulation data point as the last stock price for that run
simulations[run] = stock_monte_carlo(start_price,days,mu,sigma)[days-1]
# Now that we have our array of simulations, we can go ahead and plot a histogram ,as well as use qunatile to define our risk for this stock.
#
# For more info on quantiles, check out this link: http://en.wikipedia.org/wiki/Quantile
# In[54]:
# Now we'll define q as the 1% empirical quantile, this basically means that 99% of the values should fall between here
q = np.percentile(simulations,1)
# Now let's plot the distribution of the end prices
plt.hist(simulations, bins=200)
# Using plt.figtext to fill in some additional information onto the plot
# starting price
plt.figtext(0.6,0.8, s='Start Price: $%.2f' % start_price)
# mean ending price
plt.figtext(0.6,0.7, s='Mean Final Price: $%.2f' % simulations.mean())
# Variance of the price (within 99% confidence interval)
plt.figtext(0.6,0.6, s='VaR(0.99): $%.2f' % (start_price - q))
# To display 1% quantile
plt.figtext(0.15, 0.6, s="q(0.99): $%.2f" % q)
# Plot a line at the 1% quantile result
plt.axvline(x=q, linewidth=4, color='r')
# For plot title
plt.title(s="Final price distribution for Google Stock(GOOGL) after %s days" % days, weight='bold', color='Y')
# Awesome! Now we have looked at the 1% empirical quantile of the final price distribution to estimate the Value at Risk for the Google Stock(GOOGL), which looks to be $17.98 for every investment of
# 830.09 (The price of one initial Google Stock).
#
# This basically means for every initial GOOGL stock you purchase you're putting about $17.98 at risk 99% of the time from our Monte Carlo Simulation.
#
# ##### Now lets plot remaining Stocks to estimate the VaR with our Monte Carlo Simulation.
# In[55]:
# For Amazon Stock Price
start_price = 824.95
# Set a large numebr of runs
runs = 10000
# Create an empty matrix to hold the end price data
simulations = np.zeros(runs)
for run in range(runs):
# Set the simulation data point as the last stock price for that run
simulations[run] = stock_monte_carlo(start_price,days,mu,sigma)[days-1]
# In[56]:
# Now we'll define q as the 1% empirical quantile, this basically means that 99% of the values should fall between here
q = np.percentile(simulations,1)
# Now let's plot the distribution of the end prices
plt.hist(simulations, bins=200)
# Using plt.figtext to fill in some additional information onto the plot
# starting price
plt.figtext(0.6,0.8, s='Start Price: $%.2f' % start_price)
# mean ending price
plt.figtext(0.6,0.7, s='Mean Final Price: $%.2f' % simulations.mean())
# Variance of the price (within 99% confidence interval)
plt.figtext(0.6,0.6, s='VaR(0.99): $%.2f' % (start_price - q))
# To display 1% quantile
plt.figtext(0.15, 0.6, s="q(0.99): $%.2f" % q)
# Plot a line at the 1% quantile result
plt.axvline(x=q, linewidth=4, color='r')
# For plot title
plt.title(s="Final price distribution for Amazon Stock(AMZN) after %s days" % days, weight='bold', color='G')
# This basically means for every initial AMZN stock you purchase you're putting about $18.13 at risk 99% of the time from our Monte Carlo Simulation.
# In[57]:
# For Apple Stock Price
start_price = 117.10
# Set a large numebr of runs
runs = 10000
# Create an empty matrix to hold the end price data
simulations = np.zeros(runs)
for run in range(runs):
# Set the simulation data point as the last stock price for that run
simulations[run] = stock_monte_carlo(start_price,days,mu,sigma)[days-1]
# In[58]:
# Now we'll define q as the 1% empirical quantile, this basically means that 99% of the values should fall between here
q = np.percentile(simulations,1)
# Now let's plot the distribution of the end prices
plt.hist(simulations, bins=200)
# Using plt.figtext to fill in some additional information onto the plot
# starting price
plt.figtext(0.6,0.8, s='Start Price: $%.2f' % start_price)
# mean ending price
plt.figtext(0.6,0.7, s='Mean Final Price: $%.2f' % simulations.mean())
# Variance of the price (within 99% confidence interval)
plt.figtext(0.6,0.6, s='VaR(0.99): $%.2f' % (start_price - q))
# To display 1% quantile
plt.figtext(0.15, 0.6, s="q(0.99): $%.2f" % q)
# Plot a line at the 1% quantile result
plt.axvline(x=q, linewidth=4, color='r')
# For plot title
plt.title(s="Final price distribution for Apple Stock(AAPL) after %s days" % days, weight='bold', color='B')
# Great! This basically means for every initial AAPL stock you purchase you're putting about $2.48 at risk 99% of the time from our Monte Carlo Simulation.
# In[59]:
# For Microsoft Stock Price
start_price = 59.94
# Set a large numebr of runs
runs = 10000
# Create an empty matrix to hold the end price data
simulations = np.zeros(runs)
for run in range(runs):
# Set the simulation data point as the last stock price for that run
simulations[run] = stock_monte_carlo(start_price,days,mu,sigma)[days-1]
# In[61]:
# Now we'll define q as the 1% empirical quantile, this basically means that 99% of the values should fall between here
q = np.percentile(simulations,1)
# Now let's plot the distribution of the end prices
plt.hist(simulations, bins=200)
# Using plt.figtext to fill in some additional information onto the plot
# starting price
plt.figtext(0.6,0.8, s='Start Price: $%.2f' % start_price)
# mean ending price
plt.figtext(0.6,0.7, s='Mean Final Price: $%.2f' % simulations.mean())
# Variance of the price (within 99% confidence interval)
plt.figtext(0.6,0.6, s='VaR(0.99): $%.2f' % (start_price - q))
# To display 1% quantile
plt.figtext(0.15, 0.6, s="q(0.99): $%.2f" % q)
# Plot a line at the 1% quantile result
plt.axvline(x=q, linewidth=4, color='r')
# For plot title
plt.title(s="Final price distribution for Microsoft Stock(MSFT) after %s days" % days, weight='bold', color='M')
# Nice, This basically means for every initial MSFT stock you purchase you're putting about $1.28 at risk 99% of the time from our Monte Carlo Simulation.
# Now lets estiamte the Value at Risk(VaR) for a stock related to other domains.
#
#
# We'll estimate the VaR for:
# - Johnson & Johnson > JNJ (U.S.: NYSE) [JNJ](http://quotes.wsj.com/JNJ)
# - Wal-Mart Stores Inc. > WMT (U.S.: NYSE) [WMT](http://quotes.wsj.com/WMT)
# - Nike Inc. > NKE (U.S.: NYSE) [NKE](http://quotes.wsj.com/NKE)
#
#
# By using the above methods to get Value at Risk.
# In[5]:
# List of NYSE_stocks for analytics
NYSE_list = ['JNJ','NKE','WMT']
# set up Start and End time for data grab
end = datetime.now()
start = datetime(end.year-1,end.month,end.day)
#For-loop for grabing google finance data and setting as a dataframe
# Set DataFrame as the Stock Ticker
for stock in NYSE_list:
globals()[stock] = DataReader(stock,'google',start,end)
# Let's go ahead and play aorund with the JNJ(Johnson & Johnson) Stock DataFrame to get a feel for the data.
# In[6]:
JNJ.head()
# In[7]:
JNJ.describe()
# In[8]:
JNJ.info()
# Now that we've seen the DataFrame, let's go ahead and plot out the closing prices of NYSE stocks.
# In[18]:
# Let's see a historical view of the closing price for JNJ(Johnson & Johnson)
JNJ['Close'].plot(title='Closing Price - JNJ',legend=True, figsize=(10,4))
# In[19]:
# Let's see a historical view of the closing price for NKE(Nike Inc.)
NKE['Close'].plot(title='Closing Price - NKE',legend=True, figsize=(10,4))
# In[20]:
# Let's see a historical view of the closing price for WMT(Wal-Mart Stores Inc.)
WMT['Close'].plot(title='Closing Price - WMT',legend=True, figsize=(10,4))
# ### Value at risk using the "Bootstrap" method
#
# we will calculate the empirical quantiles from a histogram of daily returns.
# Let's go ahead and use pandas to retrieve the daily returns for the JNJ, WMT & NKE stock.
# In[8]:
# We'll use pct_change to find the percent change for each day
#For JNJ stocks
JNJ['Daily Return'] = JNJ['Close'].pct_change()
# In[10]:
# Note the use of dropna() here, otherwise the NaN values can't be read by seaborn
sns.distplot(JNJ['Daily Return'].dropna(),bins=100,color='R')
# In[12]:
(JNJ['Daily Return'].dropna()).quantile(0.05)
# The 0.05 empirical quantile of JNJ stock daily returns is at -0.010. That means that with 95% confidence, our worst daily loss will not exceed 1%. If we have a 1 million dollar investment, our one-day 5% VaR is 0.010 * 1,000,000 = $10,000.
# In[13]:
# For WMT stocks
WMT['Daily Return'] = WMT['Close'].pct_change()
# In[15]:
sns.distplot(WMT['Daily Return'].dropna(),bins=100,color='G')
# In[16]:
(WMT['Daily Return'].dropna()).quantile(0.05)
# The 0.05 empirical quantile of WMT stock daily returns is at -0.013. That means that with 95% confidence, our worst daily loss will not exceed 1.3%. If we have a 1 million dollar investment, our one-day 5% VaR is 0.013 * 1,000,000 = $13,000.
# In[17]:
# For NKE stocks
NKE['Daily Return'] = NKE['Close'].pct_change()
# In[18]:
sns.distplot(NKE['Daily Return'].dropna(),bins=100,color='B')
# In[19]:
(NKE['Daily Return'].dropna()).quantile(0.05)
# The 0.05 empirical quantile of NKE stock daily returns is at -0.018. That means that with 95% confidence, our worst daily loss will not exceed 1.8%. If we have a 1 million dollar investment, our one-day 5% VaR is 0.018 * 1,000,000 = $18,000.
# In[ ]: