Data Mining Assignment: Stock Analysis, Covariance, and Histograms

Verified

Added on  2022/12/05

|16
|3395
|115
Homework Assignment
AI Summary
This data mining assignment involves analyzing stock data from various companies (A, AAPL, FB, IBM, TSLA) over 512 days. The first part of the assignment focuses on identifying the largest relative price change for each stock, the specific stock exhibiting the change, and the corresponding dates. The second part requires calculating and sorting the covariances between all possible stock pairs using open, high, low, and close prices. Finally, the assignment includes creating histograms for the closing prices of each stock after normalizing the prices to a range of 0 to 1, using 16 bins. The assignment also includes a proof for a binomial distribution problem.
Document Page
Data Mining
1)
(a) What is the largest relative price change during 512 days given, and
(b) which stocks exhibits it, and
(c) what are the dates for minimum and maximum prices that give the largest relative change (%
change)?
Solution
Octave Code
days = linspace(1,512,512);
%Loading data from text files using csvread command
a_data = csvread('a.us.txt');
aapl_data = csvread('aapl.us.txt');
fb_data = csvread('fb.us.txt');
ibm_data = csvread('ibm.us.txt');
tsla_data = csvread('tsla.us.txt');
%Stock data for a
%Largest Relative price change during the 512 days
pc1 = a_data(:,3)-a_data(:,4);
max_change_1 = max(pc1)
day = find(pc1 == max_change_1)
A_date = a_data(day,:)
%Stock data for aapl
%Largest Relative price change during the 512 days
pc2 = aapl_data(:,3)-aapl_data(:,4);
max_change_2 = max(pc2)
day = find(pc2 == max_change_2)
Apple_date = aapl_data(day,:)
%Stock data for a
%Largest Relative price change during the 512 days
pc3 = fb_data(:,3)-fb_data(:,4);
max_change_3 = max(pc3)
day = find(pc3 == max_change_3)
FB_date = fb_data(day,:)
%Stock data for a
%Largest Relative price change during the 512 days
pc4 = ibm_data(:,3)-ibm_data(:,4);
max_change_4 = max(pc4)
day = find(pc4 == max_change_4)
IBM_date = ibm_data(day,:)
%Stock data for a
%Largest Relative price change during the 512 days
pc5 = tsla_data(:,3)-tsla_data(:,4);
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
max_change_5 = max(pc5)
day = find(pc5 == max_change_5)
Tesla_date = tsla_data(day,:)
Octave Output
>> price_change
max_change_1 = 3.8600
day = 423
A_date =
20190417.00000 78.15000 78.32000 74.46000 75.43000 4471971.00000
max_change_2 = 13.600
day = 307
Apple_date =
20181029.00000 219.19000 219.69000 206.09000 212.24000 45935520.00000
max_change_3 = 14.040
day = 454
FB_date =
20190603.00000 175.00000 175.05000 161.01000 164.15000 56059609.00000
max_change_4 = 8.5000
day = 122
IBM_date =
20180205.00000 157.89000 158.50000 150.00000 152.53000 8746599.00000
max_change_5 = 48.310
day = 249
Tesla_date =
20180807.00000 343.84000 387.46000 339.15010 379.57000 30875768.00000
(a) Largest relative price change = 14.40
(b)exhibited by Facebook (from fb.us.txt)
(c) on 3rd June 2019. (20190603)
2)
Using all the prices for each stock, (open, high, low, close), sort, from highest to the lowest, all the
possible covariances between the given stocks.
Covariance between different stocks against each other for open, high, low, close data
Octave Code
%Calculating covariance
%Loading data from text files using csvread command
a_data = csvread('a.us.txt');
Document Page
aapl_data = csvread('aapl.us.txt');
fb_data = csvread('fb.us.txt');
ibm_data = csvread('ibm.us.txt');
tsla_data = csvread('tsla.us.txt');
a_sort = a_data(:,2:5);
Apple_sort = aapl_data(:,2:5);
FB_sort = fb_data(:,2:5);
IBM_sort = ibm_data(:,2:5);
Tesla_sort = tsla_data(:,2:5);
%Sorted Covariances for a against other stocks
cov_a_apple = sort(cov(a_sort,Apple_sort),'descend')
cov_a_fb = sort(cov(a_sort,FB_sort),'descend')
cov_a_IBM = sort(cov(a_sort,IBM_sort),'descend')
cov_a_Tesla = sort(cov(a_sort,Tesla_sort),'descend')
%Sorted Covariances for Apple against other stocks
cov_apple_a = sort(cov(Apple_sort,a_sort),'descend')
cov_apple_fb = sort(cov(Apple_sort,FB_sort),'descend')
cov_apple_IBM = sort(cov(Apple_sort,IBM_sort),'descend')
cov_apple_Tesla = sort(cov(Apple_sort,Tesla_sort),'descend')
%Sorted Covariances for Apple against other stocks
cov_fb_a = sort(cov(FB_sort,a_sort),'descend')
cov_fb_apple = sort(cov(FB_sort,Apple_sort),'descend')
cov_fb_IBM = sort(cov(FB_sort,IBM_sort),'descend')
cov_fb_Tesla = sort(cov(FB_sort,Tesla_sort),'descend')
%Sorted Covariances for Apple against other stocks
cov_ibm_a = sort(cov(IBM_sort,a_sort),'descend')
cov_ibm_apple = sort(cov(IBM_sort,Apple_sort),'descend')
cov_ibm_fb = sort(cov(IBM_sort,FB_sort),'descend')
cov_ibm_Tesla = sort(cov(IBM_sort,Tesla_sort),'descend')
%Sorted Covariances for Apple against other stocks
cov_Tesla_a = sort(cov(Tesla_sort,a_sort),'descend')
cov_Tesla_apple = sort(cov(Tesla_sort,Apple_sort),'descend')
cov_Tesla_fb = sort(cov(Tesla_sort,FB_sort),'descend')
cov_Tesla_Tesla = sort(cov(Tesla_sort,IBM_sort),'descend')
Octave Outputs for Sorted Covariances (descending order)
cov_a_apple =
12.185 12.044 12.160 11.956
11.815 11.896 11.845 11.884
11.251 11.164 11.656 11.726
11.111 11.071 11.551 11.447
Document Page
cov_a_fb =
1.231685 1.143589 2.089171 1.639606
0.331243 0.208256 0.761878 0.634357
-0.172336 -0.040323 0.760169 0.253722
-0.803893 -0.748822 -0.326056 -0.639891
cov_a_IBM =
0.96720387 0.76035053 1.52433554 1.30297386
0.19019051 0.00094359 0.45301127 0.25247652
-0.53906954 -0.58442913 0.05718217 0.07150142
-1.14876027 -1.22148816 -0.83794561 -0.89408042
cov_a_Tesla =
-68.054 -71.305 -64.645 -68.352
-70.769 -73.498 -67.245 -70.139
-70.849 -74.389 -68.239 -72.237
-73.506 -76.621 -70.667 -74.126
cov_apple_a =
12.185 11.896 11.656 11.726
12.160 11.884 11.447 11.551
12.044 11.845 11.251 11.164
11.956 11.815 11.071 11.111
cov_apple_fb =
91.427 91.495 90.404 89.671
85.349 86.128 84.325 84.790
84.112 83.854 81.478 80.530
80.097 80.862 77.745 77.634
cov_apple_IBM =
0.71104 0.37301 1.38314 0.24684
-3.38532 -3.43831 -2.93174 -3.25842
-3.67899 -3.64890 -3.40028 -4.55051
-6.64235 -6.47563 -6.68946 -7.29253
cov_apple_Tesla =
-392.90 -391.41 -391.43 -391.36
-393.94 -393.07 -395.12 -395.29
-400.53 -397.02 -398.83 -395.69
-407.17 -404.37 -407.75 -405.89
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
cov_fb_a =
0.760169 -0.326056 2.089171 0.761878
0.331243 -0.639891 1.639606 0.634357
0.253722 -0.748822 1.231685 -0.040323
0.208256 -0.803893 1.143589 -0.172336
cov_fb_apple =
84.112 80.862 91.495 86.128
83.854 80.097 91.427 85.349
81.478 77.745 90.404 84.790
80.530 77.634 89.671 84.325
cov_fb_IBM =
109.46 107.42 112.05 109.94
108.16 106.14 110.30 108.79
107.64 106.01 110.17 108.08
105.73 104.04 107.67 105.92
cov_fb_Tesla =
-189.60 -200.96 -175.36 -190.07
-195.39 -205.24 -181.11 -193.26
-195.41 -208.05 -184.19 -199.80
-200.24 -210.74 -188.46 -202.19
cov_ibm_a =
0.45301127 -0.83794561 1.52433554 0.07150142
0.25247652 -0.89408042 1.30297386 0.05718217
0.19019051 -1.14876027 0.96720387 -0.53906954
0.00094359 -1.22148816 0.76035053 -0.58442913
cov_ibm_apple =
-3.38532 -6.47563 1.38314 -2.93174
-3.40028 -6.64235 0.71104 -3.25842
-3.64890 -6.68946 0.37301 -3.43831
-4.55051 -7.29253 0.24684 -3.67899
cov_ibm_fb =
110.17 107.67 112.05 110.30
108.16 105.92 109.94 108.79
108.08 105.73 109.46 107.64
106.14 104.04 107.42 106.01
Document Page
cov_ibm_Tesla =
65.749 61.518 71.689 64.834
64.264 60.448 69.535 64.284
64.243 60.187 69.055 62.778
63.454 59.609 68.950 61.634
cov_Tesla_a =
-68.239 -70.667 -64.645 -67.245
-70.849 -73.506 -68.054 -70.139
-72.237 -74.126 -68.352 -70.769
-74.389 -76.621 -71.305 -73.498
cov_Tesla_apple =
-393.07 -404.37 -391.36 -395.69
-393.94 -405.89 -391.41 -397.02
-395.12 -407.17 -391.43 -398.83
-395.29 -407.75 -392.90 -400.53
cov_Tesla_fb =
-184.19 -188.46 -175.36 -181.11
-195.39 -200.24 -189.60 -193.26
-199.80 -202.19 -190.07 -195.41
-208.05 -210.74 -200.96 -205.24
cov_Tesla_IBM =
68.950 69.055 71.689 69.535
64.264 64.243 65.749 64.284
61.634 62.778 64.834 63.454
59.609 60.448 61.518 60.187
3)
After normalizing all the prices for each stock in the range from 0 to 1 (for all dates and including
open, high, low, close), create in a figure histogram for each stock price distribution. Use Use 16 bins
for the histograms.
Closing Price histogram data
Octave Code
%Loading data from text files using csvread command
a_data = csvread('a.us.txt');
aapl_data = csvread('aapl.us.txt');
fb_data = csvread('fb.us.txt');
ibm_data = csvread('ibm.us.txt');
tsla_data = csvread('tsla.us.txt');
Document Page
%Close
a_prices = a_data(:,5);
Apple_prices = aapl_data(:,5);
FB_prices = fb_data(:,5);
IBM_prices = ibm_data(:,5);
Tesla_prices = tsla_data(:,5);
A_close = 0+((a_prices-min(a_prices).*(1-0))./(max(a_prices)-min(a_prices)))
Apple_close = 0+((Apple_prices-min(Apple_prices).*(1-0))./(max(Apple_prices)-min(Apple_prices)))
FB_close = 0+((FB_prices-min(FB_prices).*(1-0))./(max(FB_prices)-min(FB_prices)))
IBM_close = 0+((IBM_prices-min(IBM_prices).*(1-0))./(max(IBM_prices)-min(IBM_prices)))
Tesla_close = 0+((Tesla_prices-min(Tesla_prices).*(1-0))./(max(Tesla_prices)-min(Tesla_prices)))
subplot(3,2,1)
hist(A_close,16)
title('A Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,2)
hist(Apple_close,16)
title('Apple Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,3)
hist(FB_close,16)
title('Facebook Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,4)
hist(IBM_close,16)
title('IBM Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,5)
hist(Tesla_close,16)
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
title('Tesla Closing Price')%Loading data from text files using csvread command
a_data = csvread('a.us.txt');
aapl_data = csvread('aapl.us.txt');
fb_data = csvread('fb.us.txt');
ibm_data = csvread('ibm.us.txt');
tsla_data = csvread('tsla.us.txt');
%Close
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
a_prices = a_data(:,5);
Apple_prices = aapl_data(:,5);
FB_prices = fb_data(:,5);
IBM_prices = ibm_data(:,5);
Tesla_prices = tsla_data(:,5);
A_close = 0+((a_prices-min(a_prices).*(1-0))./(max(a_prices)-min(a_prices)))
Apple_close = 0+((Apple_prices-min(Apple_prices).*(1-0))./(max(Apple_prices)-min(Apple_prices)))
FB_close = 0+((FB_prices-min(FB_prices).*(1-0))./(max(FB_prices)-min(FB_prices)))
IBM_close = 0+((IBM_prices-min(IBM_prices).*(1-0))./(max(IBM_prices)-min(IBM_prices)))
Tesla_close = 0+((Tesla_prices-min(Tesla_prices).*(1-0))./(max(Tesla_prices)-min(Tesla_prices)))
subplot(3,2,1)
hist(A_close,16)
title('A Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,2)
hist(Apple_close,16)
title('Apple Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,3)
hist(FB_close,16)
title('Facebook Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,4)
hist(IBM_close,16)
title('IBM Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,5)
hist(Tesla_close,16)
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
title('Tesla Closing Price')
Output
Document Page
Opening price Data
Octave Code
%Calculating covariance
%Loading data from text files using csvread command
a_data = csvread('a.us.txt');
aapl_data = csvread('aapl.us.txt');
fb_data = csvread('fb.us.txt');
ibm_data = csvread('ibm.us.txt');
tsla_data = csvread('tsla.us.txt');
%Open price
a_prices = a_data(:,2);
Apple_prices = aapl_data(:,2);
FB_prices = fb_data(:,2);
IBM_prices = ibm_data(:,2);
Tesla_prices = tsla_data(:,2);
A_open = 0+((a_prices-min(a_prices).*(1-0))./(max(a_prices)-min(a_prices)))
Apple_open = 0+((Apple_prices-min(Apple_prices).*(1-0))./(max(Apple_prices)-min(Apple_prices)))
FB_open = 0+((FB_prices-min(FB_prices).*(1-0))./(max(FB_prices)-min(FB_prices)))
Document Page
IBM_open = 0+((IBM_prices-min(IBM_prices).*(1-0))./(max(IBM_prices)-min(IBM_prices)))
Tesla_open = 0+((Tesla_prices-min(Tesla_prices).*(1-0))./(max(Tesla_prices)-min(Tesla_prices)))
subplot(3,2,1)
hist(A_open,16)
title('A Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,2)
hist(Apple_open,16)
title('Apple Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,3)
hist(FB_open,16)
title('Facebook Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,4)
hist(IBM_open,16)
title('IBM Closing Price')
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
subplot(3,2,5)
hist(Tesla_open,16)
xlabel('Time Series')
ylabel('Normalized Prices Fluctuation')
title('Tesla Closing Price')
Output
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
High Price
Octave Code
%Loading data from text files using csvread command
a_data = csvread('a.us.txt');
aapl_data = csvread('aapl.us.txt');
fb_data = csvread('fb.us.txt');
ibm_data = csvread('ibm.us.txt');
tsla_data = csvread('tsla.us.txt');
%High
a_prices = a_data(:,3);
Apple_prices = aapl_data(:,3);
FB_prices = fb_data(:,3);
IBM_prices = ibm_data(:,3);
Tesla_prices = tsla_data(:,3);
A_high = 0+((a_prices-min(a_prices).*(1-0))./(max(a_prices)-min(a_prices)))
Apple_high = 0+((Apple_prices-min(Apple_prices).*(1-0))./(max(Apple_prices)-min(Apple_prices)))
FB_high = 0+((FB_prices-min(FB_prices).*(1-0))./(max(FB_prices)-min(FB_prices)))
IBM_high = 0+((IBM_prices-min(IBM_prices).*(1-0))./(max(IBM_prices)-min(IBM_prices)))
Tesla_high = 0+((Tesla_prices-min(Tesla_prices).*(1-0))./(max(Tesla_prices)-min(Tesla_prices)))
Document Page
subplot(3,2,1)
hist(A_high,16)
title('A High Price')
xlabel('Time Series')
ylabel('Prices')
subplot(3,2,2)
hist(Apple_high,16)
title('Apple High Price')
xlabel('Time Series')
ylabel('Prices')
subplot(3,2,3)
hist(FB_high,16)
title('Facebook High Price')
xlabel('Time Series')
ylabel('Prices')
subplot(3,2,4)
hist(IBM_high,16)
title('IBM High Price')
xlabel('Time Series')
ylabel('Prices')
subplot(3,2,5)
hist(Tesla_high,16)
xlabel('Time Series')
ylabel('Prices')
title('Tesla Closing Price')
Output
chevron_up_icon
1 out of 16
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]