intelligent control problems with solutions
TRANSCRIPT
-
7/24/2019 Intelligent Control Problems with Solutions
1/16
EE 5322: Assignment #1
Due on Tuesday, Sept 8, 2015
Ajay Pratap Yadav
1001248915
September 7, 2015
1
-
7/24/2019 Intelligent Control Problems with Solutions
2/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
Question #1
a. Compute the 20 day MA. Plot on the same figure as the stock closing price.
b. Plot the stock minus the 20 day MA.
c. Compute and plot the 20 day moving sample variance.
d. On the same figure, plot the stock closing price, the 20 day MA, and the MA plus three
times the 20 day standard deviation the MA minus three times the 20 day standard deviation.
The last two lines are known as the Bollinger Bands, after John Bollinger.
Answer
Note: I have first plotted the figures followed by code.
(a)
0 50 100 150 200 250 3005
10
15
20
25
30
Days
Price
Closing Price
20Moving Avg
2 Page 2 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
3/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
(b)
0 50 100 150 200 250 3006
4
2
0
2
4
6
8
Days
Pricedifferenceb/wStockand20MA
(c)
0 50 100 150 200 250 3000
1
2
3
4
5
6
7
8
9
Days
20daymo
vingsamplevariance
3 Page 3 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
4/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
(d)
0 50 100 150 200 250 3000
5
10
15
20
25
30
35
Days
Prices
Closing Price
Mavg
Bol band
MATLAB Code
1 % NOTE: FUNCTIONS ARE WRITTEN FIRST FOLLOWED BY MAIN CODE
2 function out = MA(x,t) %Calculating Moving Avg
3 n = size(x);
4 out = zeros(n(1),1);
5 for i=1:t
6 out(i) = sum(x(1:i))/i;
7 end
8 for i=t+1:n(1)
9 out(i) = sum(x(i(t1):i))/t;
10 end
11 end
12 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 function out = MV(x,MA,t) %Calculating Moving Variance
14 n = size(x);
15 out = zeros(n(1),1);
16
17 for i = t+1:n(1)
18 a=0;
19 for j=1:t
4 Page 4 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
5/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
20 a=a+(x(ij+1)MA(i))2;
21 end
22 out(i) = a/t;
23 end
24
25 for i = 1:t
26 a=0;
27 for j=1:i
28 a=a+(x(ij+1)MA(i))2;
29 end
30 out(i) = a/i;
31 end
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 % Main Code for question #1
34 %EE5322: Home Work 1
35 %Ajay Yadav 1001248915
36 clc
37 clear
38 load ('HW1 data');
39 data = Data;
40 t = 20;%size of window
41 N = size(data);
42 x = data(:,2);
43 M avg = MA(x,t);
44 diff = data(:,2)M avg;
45 plot(data(:,1),data(:,2));
46 xlabel('Days');ylabel('Price');
47 hold on
48 plot(M avg,'k')
49 legend('Closing Price','20Moving Avg');grid on
50 figure
51 plot(data(:,1),diff)
52 xlabel('Days');ylabel('Price difference b/w Stock and 20 MA ');
53 grid on
54 M var = MV(x,M avg,t);
55 figure
56 plot(data(:,1),M var )
57 xlabel('Days');ylabel('20 day moving sample variance');
58 grid on59
60 Bol1 = M avg+3*sqrt(M var);
61 Bol2 = M avg3*sqrt(M var);
62 figure
63 plot(data(:,1),x)
64 hold on
65 plot(M avg,'g')
66 hold on
67 plot(Bol1,'r')
68 hold on
5 Page 5 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
6/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
69 plot(Bol2,'r')
70 legend('Closing Price', 'M avg', 'Bol band')
71 xlabel('Days');ylabel('Prices')
Question #2
a. Compute and plot the 20 day moving skew.
b. Compute and plot the 20 day moving kurtosis.
c. Can you use these statistics to find a leading indicator for movements in the stock?
i.e. how can we predict using statistics when the stock is about to break its trend (change its
pattern)?
Answer
(a)
0 50 100 150 200 250 3002.5
2
1.5
1
0.5
0
0.5
1
1.5
2
2.5
Days
20daymovingSKE
W
6 Page 6 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
7/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
(b)
0 50 100 150 200 250 3003
2
1
0
1
2
3
4
5
6
7
Days
20daymovingKURTOSIS
(c)
0 50 100 150 200 250 3000
5
10
15
20
25
30
35
Days
Prices
Closing Price
Mavg
Bol band
7 Page 7 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
8/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
0 50 100 150 200 250 3003
2
1
0
1
2
3
4
5
6
7
Days
20daymovingKURTOSIS
0 50 100 150 200 250 3002.5
2
1.5
1
0.5
0
0.5
1
1.5
2
2.5
Days
20
daymovingSKEW
Figure 1: 20-day Skew and 20-day Kurtosis8 Page 8 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
9/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
We can predict the change in movement of the stocks from its skew and kurtosis. To
demonstrate this, lets observe the 20-day skew and 20-day kurtosis along with the plots
of closing price. From figure 1, we can observe that sudden rise in skew and kurtosis
(shown by black arrows) coincides with points on stock prices graph where the stocks
touch the upper Bollinger Band. These are the points when its best to sell stocks.
Matlab Code
1 % NOTE: FUNCTIONS ARE WRITTEN FIRST FOLLOWED BY MAIN CODE
2 function out = MA(x,t) %Calculating Moving Avg
3 n = size(x);
4 out = zeros(n(1),1);
5 for i=1:t
6 out(i) = sum(x(1:i))/i;
7 end
8 for i=t+1:n(1)
9 out(i) = sum(x(i(t1):i))/t;
10 end
11 end
12 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 function out = MV(x,MA,t) %Calculating Moving Variance
14 n = size(x);
15 out = zeros(n(1),1);
16
17 for i = t+1:n(1)
18 a=0;
19 for j=1:t
20 a=a+(x(ij+1)MA(i))2;
21 end
22 out(i) = a/t;
23 end
24
25 for i = 1:t
26 a=0;
27 for j=1:i
28 a=a+(x(i
j+1)
MA(i))2;29 end
30 out(i) = a/i;
31 end
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 % Main Code for question #2
34 clc
35 clear
36 load ('HW1 data');
37 data = Data;
38 t = 2 0;%size of window
9 Page 9 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
10/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
39 N = size(data);
40 x = data(:,2);
41 M avg = MA(x,t);
42 %Calculate Skew
43 M var = MV(x,M avg,t); %sigma square44 skew = zeros(N(1),1);
45 kurt = zeros(N(1),1);
46 for i = t+1:N(1)
47 a = 0;b = 0;
48 for j = 1:t
49 a = a + (x(ij+1) M avg(i))3;
50 b = b + (x(ij+1) M avg(i))4;
51 end
52 skew(i) = a/(t*(M var(i))1.5); % factor 1.5 balances the square
53 kurt(i) = (b/(t*(M var(i))2))3;
54 end
55 for i = 1:t
56 a = 0;b = 0;
57 for j=1:i
58 a = a + (x(ij+1)M avg(i))3;
59 b = b + (x(ij+1)M avg(i))4;
60 end
61 skew(i) = a/(i*(M v ar(i))+0.00001)1.5; %Small constant added to avoid ...
0 in denominator
62 kurt(i) = (b/(i*(M va r(i))+0.00001)2)3;%Small constant added to ...
avoid 0 in denominator
63 end
64
65 plot(data(:,1),skew)
66 xlabel('Days');ylabel('20 day moving SKEW');grid on;
67 figure
68 plot(data(:,1),kurt)
69 xlabel('Days');ylabel('20 day moving KURTOSIS');grid on
Question #3
a. Compute and plot the overall autocorrelation.
b. Compute and plot the overall autocovariance.
Answer
Note: I have calculated the autocorrelation and autocovariance for different lags or delays
starting from 1 to 253.
10 Page 10 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
11/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
(a)
0 50 100 150 200 250 3000
50
100
150
200
250
300
lag (n)
Autocorrelation
Matlab Code
1 %Question # 3
2 %Overall Auto correlation
3 clc
4 clear
5 load ('HW1 data');
6 data = Data;
7 N = size(data);
8 x = data(:,2);
9 autocor = zeros(N(1),1);
10 for i = 1:N(1)
11 a = 0;
12 for j = 1:N(1)
13 if i+j N(1)
14 a = a + x(j)*x(j+i);
15 end
16 end
17 autocor(i) = a/N(1);
18 end
19 plot(autocor,'o')
11 Page 11 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
12/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
20 xlabel('lag (n)');ylabel('Autocorrelation');grid on;
(b)
0 50 100 150 200 250 30010
5
0
5
10
15
20
25
lags (n)
Au
tocovariance
Matlab Code
1 % H W 1 3 b
2 % Overall auto covariance
3 clc
4 clear
5 load ('HW1 data');
6 data = Data;
7 N = size(data);8 x = data(:,2);
9 x mean = mean(x);
10 autovar = zeros(N(1),1);
11 for i = 1:N(1)
12 a = 0;
13 for j = 1:N(1)
14 if i+j N(1)
15 a = a + (x(j)x mean)*(x(j+i)x mean);
16 end
17 end
12 Page 12 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
13/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
18 autovar(i) = a/N(1);
19 end
20 plot(autovar,'o')
21 xlabel('lags (n)');ylabel('Auto covariance');grid on;
Question #4
Any news about predicting movements in this stock? Is it time to buy this stock now?
Answer
For this problem, I have used a neural network to perform 1-day ahead prediction of stock
closing prices. I am assuming that the next day prices are affected by todays price, 20-day
moving average, 20-day moving variance, 20-day skew and kurtosis. Therefore, the output
of the neural networks is the next day stock price while todays price, its 20-day moving
average, 20-day moving variance, 20-day skew and 20-day kurtosis are inputs.
The neural network model used is a simple feed forward network with 5 inputs, 10 hidden
neurons and 1 output. Training is done using back propagation algorithm. The first 220
days are used for training and the prediction is made for the next 34 days. From figure 2,
we can observe that the neural network is able to forecast the movements of closing stock
prices.
Note: I believe this network will fail if the stock market crashes due to factors such as war.
Also, the recent market crash due to Chinese currency devaluation are also difficult to predictusing this particular model.
My neural network model predicts that the next day (255th day) closing price is 25.75 while
the price of 254th day is 19.03. Therefore, we can buy the stocks.
Matlab Code
1 function [y]=weightconv(Wo,Wi)
2
insize=size(Wi);outsize=size(Wo);3 z=Wo';
4 for i=1:insize(1)
5 z=[z;Wi(i,:)'];
6 end
7 y=z;
8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9 %Forecasting Stock Prices Using Stocks
10 clc
11 clear all
12 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
13 Page 13 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
14/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
0 50 100 150 200 250 3000
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
X: 220Y: 0.6486
Days
NormalizedStockPrices
Actual Prices
Predicted Prices
Predicted Values
Training Data
Figure 2: Training and forecasting data
13 load ('HW1 data');
14 data = Data;
15 t = 2 0;%size of window
16 N = size(data);
17 x = data(:,2);
18 M avg = MA(x,t);
19 M var = MV(x,M avg,t);
20 load('skew.mat')
21 load('kurt.mat') %loading the presaved 20 day skew and Kurt
22 %%%%creating a Pattern Matrix%%%%%%%%
23 pattern = zeros(N(1),6); % 5 inp and 1 outputs
24 pattern(:,1:5) = [x M avg M var skew kurt];
25 pattern(1:253,6) = x(2:254);
26 pattern = pattern(1:253, :);
27
28 %%%%%Normalizing the Data points%%%%%%%%
29 pmax = zeros(1,6); pmin = zeros(1,6);
30 for i = 1:6
31 pmax(i) = max(pattern(:,i));
32 pmin(i) = min(pattern(:,i));
33 end
34 for i = 1:253
35 pattern(i,:) = (pattern(i,:) pmin)./(pmax pmin);
36 end
37
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 N=253; %number of pattern
40 %Q=0.8*N;
41 %Q=N;
42 Q = 220; % Number of patterns used for NN training.
14 Page 14 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
15/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
43 Ni=5;Nj=10;
44 Runs=1;
45 output=pattern(:,Ni+1);cc=0;accu=zeros(Runs,1);alpha=0.9;itercount=zeros(Runs,1);
46 for epo=1:Runs
47 tic48 Wi=2*rand(Nj,Ni+1)1; Wi ori=Wi; %hidden weight
49 Wo=2*rand(1,Nj+1)1; Wo ori=Wo;
50 % load('input weight5.mat')
51 % load('output weight5.mat')
52 % Wi=Wi ori;Wo=Wo ori;
53 Wi=zeros(Nj,Ni+1);
54 Wo=zeros(1,Nj+1);
55
56 m=Nj+1+(Ni+1)*Nj;
57 W=zeros(m,1);
58 Si=[ones(N,1) pattern(:,1:Ni)]; %input signals
59 V=[1; zeros(Nj,1)];
60 Jo=zeros(1,Nj+1); %jacobian wrt out
61 Ji=zeros(Nj,Ni+1);% jacobian wrt inp
62 mu=0.45;
63 % mu=1;
64 % mu=0.7;
65 tol=.01;
66 iter=2000;W=weightconv(Wo,Wi);Wmin=2*rand(m,1)1;r=1;
67
68 for kk=1:iter
69 error=0;
70 for i=1:Q
71
72 in=Si(i,:)';out=output(i);
73 h1=Wi*in;
74 v=1./(1+exp(h1));
75 V=[1;v]; %neuron signal
76 h2=Wo*V;
77 y=1/(1+exp(h2));
78 error=(outy)2+error;
79 Jo=y*(1y)*V';
80 for j=1:Nj
81 Ji(j,:)=y*(1
y)*Wo(1+j)*V(1+j)*(1
V(1+j))*in';82 end
83 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Back Propagation+ momentum
84 J=weightconv(Jo,Ji);
85 e=(outy);
86 deno=norm(J'*e)2;
87 rate=1;
88 Wi=Wi+mu*rate*Ji*e+alpha*Wi;
89 Wo=Wo+mu*rate*Jo*e+alpha*Wo;
90 Wi=Ji*e;
91 Wo=Jo*e;
15 Page 15 of 16
-
7/24/2019 Intelligent Control Problems with Solutions
16/16
Ajay Pratap Yadav1001248915 EE 5322 (F. Lewis): Assignment #1
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 BP
93 % J=weightconv(Jo,Ji);
94 % e=(outy);
95 % deno=norm(J'*e)2;
96 % rate=1;97 % Wi=Wi+mu*rate*Ji*e;
98 % Wo=Wo+mu*rate*Jo*e;
99
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
101
102 end
103 epo
104 kk
105 Err(epo,kk)=sqrt((error)/Q);
106 Rate(epo,kk)=rate;
107 if Err(epo,kk)