Sunteți pe pagina 1din 17

ECE3093 Semester 1, 2014

Assignment 2, (worth 10%)


ANSWER TEMPLATE
Name: Hashan Wickramasuriya
Student ID: 23417250
Please complete the following table, after solving Task 1 and Task 2:

Returns on $1000 do not need to be quoted to 14 decimal places.


Optimal x vector ($ allocations to 10 indices)

Maximum R (for optimal & feasible solution)


meanTrainreturn
minTrainreturn
maxTrainreturn
meanTestreturn
minTestreturn
maxTestreturn
meanTrainvariance
meanTestvariance

LP
QP
[0, 290.82, 500, 0, 0, 0, 0, 0, [0, 36.133, 331.866, 0,
0, 0]
0, 0, 500, 0, 0,
132.001]
-0.05 (-5%) P
-0.05 (-5%) O -0.001
4.17653061014567 $4.18 P -0.0120133444227590 P
-49.9999999951506
-65.8214203733677
81.9030612100866
40.8724189006152
-5.95748299573666 -$5.96 P 9.32363502669894 P
-93.5816326443381
-73.82869568812463
57.4846938759323
48.6435043818340
739.031147816565 P
489.093204909667
1305.54842096684 P
624.856921270185

Please provide responses to Task 3 questions below:


a) Plot the monthly returns for each optimal strategy (LP and QP) during the training period and then
what happens during the testing period. What do you observe?
Linear Programming

Figure 1.1

Figure 1.2

NB scale
[-50;+100]

NB scale
[-100;+100]

Figure 1.3

The overlay plot, Figure 1.2, is a better plot, as the data is displayed on a common scale.

Expected monthly returns over the testing period and over the training period show some resemblance to each
other. One observation that can be made is that there are continuous period for which the expected return is
negative and continuous periods in which the expected return is positive. This can be said about both training
This is also seen in plots of cumulative returns.
and testing periods.P
These continuous periods of negative and positive expected return for training and testing
period move relative to each other as depicted in figure 1.2. Any pattern that can be observed can be best
This can be seen by overlaying LP and QP in the same time period.
explained by seasonality involved with the indices composing the portfolio. However, by observing figure 1.1
we can conclude that there is very little relationship between training period and testing period. This
observation is supported by looking at the mean train variance of 739 and mean testing variance of 1306.
Significant difference in these variances explains why training and testing periods are so different P

Figure 1.1 illustrates the increase is variance. It also has a downward trend.
LP
739.031147816565
1305.54842096684

meanTrainvariance
meanTestvariance

Quadratic Programming

Figure 2.1

Figure 2.2

Figure 2.3

For the quadratic programming, looking at the figure 2.1, it is observable that training period and testing
Should they resemble each other?
period show considerable amount of resemblance to each other. Peaks and troughs of the training and testing
Overlaying LP and QP in the same time period is the valid way to identify synchrony.
period are almost synchronized to some extent. This could well be due to seasonality of the indices involved
True, seasonality probably plays a part.
with the indices making up the portfolio. This observation is backed up by analytical results where mean train
Thedifferences
ratio between
variances
is more
valid.
variance of 489.09 and mean test variance of 624.86. The
betweenthe
mean
test variance
and mean
training variance of the quadratic programming is significantly less than what we obtained previously in linear
programming. Consequently by looking at these graphs and mean variances we can conclude that, modeling
seems to be
training period with testing period using quadratic programming is more useful for finding optimal portfolio
allocation for given constraints.

QP variance, train 489, test 625, increase by about one-third.


QP
489.093204909667
624.856921270185

meanTrainvariance
meanTestvariance

489
625

Summary, mean return LP training $4.18, test -$5.96, QP training -$0.01, test $9.32.
Optimal x vector ($ allocations to 10 indices)

LP
QP
[0, 290.82, 500, 0, 0, 0, 0, 0, [0, 36.133, 331.866, 0,
0, 0]
0, 0, 500, 0, 0,
132.001]

Maximum R (for optimal & feasible solution)

4.17653061014567
-49.9999999951506
81.9030612100866
-5.95748299573666
-93.5816326443381
57.4846938759323
739.031147816565
1305.54842096684

meanTrainreturn
minTrainreturn
maxTrainreturn
meanTestreturn
minTestreturn
maxTestreturn
meanTrainvariance
meanTestvariance

-0.0120133444227590
-65.8214203733677
40.8724189006152
9.32363502669894
-73.82869568812463
48.6435043818340
489.093204909667
624.856921270185

LP variance, train 739, test 1306; nearly doubles.


b) Discuss whether the models built on the training data were useful for the testing period, and
how they could be made more accurate.

we have

In the linear programming we tried to maximize expected returns for the training period. We can see that the
mean train return that we obtained is$4.177, which is greater than zero and aligned with our expected
return-maximizing objective. However, when we used those optimal allocations from training period to
evaluate our testing period, the mean testing return is -5.957, which is less than zero, meaning that we are
expecting a loss on the investment on average with those allocations.P
Therefore, problem here is that we
modeled our initial linear program solely to maximize expected return using training period data. However,
we did not consider how indices behave with each other over time and didnt take into account that testing
period may have completely different set of return on investment in index i over the period.P
Therefore, using
allocations from training period to maximize expected return on the testing period in this scenario is
unrealistic
unsuitable and naive, as we obtain a negative expected return on the testing period. This is also supported by
looking at the mean train variance of 739.03 and mean test variance of 1305.55, discrepancy between the
variance is significant, indicating that model built on training period is not useful for testing period.

The LP method tends to choose stock indices that move strongly with the stock markets's ups
and downs. The LP method chooses those indices that give a maximum return in the training
period.

In the quadratic programming we tried to minimize portfolio variance. We used covariance to measures the
degree of linear dependence between indices of the training period.P
We chose our allocations such that it
would minimize portfolio variance. Therefore, in quadratic programming we do not give priorityP
to maximize
maximising the return on investement is a constraint, but not an objective.
expected return for the training set, this can be seen by looking at the mean training return of -0.012, which

is an expected loss of return on average.PInstead, by looking at the covariance of the training set
which is the degree of linear dependence between indices, in essence we are getting a relationship between

we hope may

indices over time (behavioral analysis) to minimize variance, which then can be carried over when we are
evaluating the testing period. In other words, if the returns on an index (asset) are relatively constant or
stationary over some time and if we perform quadratic programming on training period and then perform
quadratic programming on testing period, the way in which allocations are done would pretty much be the
same for both cases.PP
In our analysis we obtained mean train variance to be 489.09 and mean test variance of
624.86. These mean variances are considerably lower than what we obtained in linear programming, which
satisfy the objective to minimize the variance of the portfolio.P
Consequently, quadratic programming model on training data is more useful for testing period compared to
linear programming where we only gave priority to maximum expected values. One way to improve quadratic
model is by having a larger pool of dataset therefore more information for the training period, i.e. training
period to be more than 24 months. This would consequently result in a much more intelligent allocation of the
portfolio and investment strategy as we are analyzing the behavior of indices over longer time range. On the
other hand it is difficult to improve the linear programming model where the objective is to maximize
expected return. We could increase number of months as before, but it would have very little
less effect on how
testing would be modeled, unless there are seasonality relationship between indices over time.

Overlays of LP and QP in the same time period show there is a background of other movements.
c) Discuss whether minimizing variance or maximizing expected returns was a more profitable
strategy, and the pros and cons of each approach.

minimising

Portfolio allocation requires a trade off between expected return and minimize variance (risk). In general
riskier assets (high variance) have higher expected return.P
This is because investors who invest in a higher risk
asset demand a reward for bearing risk called risk premium.OVariance is one P
measure of the risk of an asset
allocation. Minimizing variance means negative correlation, where correlation eects only the risk, not the
The given QP method minimises variance subject to a minimum return.
expected return. With negative correlation, the two assets tend to move in opposite direction, which decreases
the volatility of the portfolio. Therefore negative correlation is good. If we minimize variance by investing in
two indices such that their correlation is 0 (uncorrelated), then a negative return of one tends to occur with a
positive return of the other, which also explains the purpose of diversification.

What evidence of diversification is there in your data?


Generally, we cannot say minimizing variance or maximizing expected returns was a more profitable strategy.
It is more about finding the right balance, as there are pros and cons for each approach. For example, pros of
maximizing expected returns is, if we invest our entire portfolio in high-risk assets, it may be that we get
really lucky and expected return is extremely high (High profitability). However con of this approach is that,
if we continue to invest in the same high risk asset in a market for sometime, expected return will reduce and
we would eventually make a loss. This is similar to what we did when we considered linear programming to
maximize expected return on the indices for the training period. Then we used the allocations x corresponding
to maximum expected return from training period to model testing period. These allocations are of high risk or
high variance and generally not useful in modeling testing period and therefore would not be competent

The cumulative returns plots evidence that QP can deliver a steady profit, while LP may deliver
short-term gains, the evidence is that LP can deliver a net loss over any other time period.

profitable strategy. Whereas quadratic programming determines the minimum variance for set of period which
we hope will forecast
then can be used to forecast future expected return. Optimal portfolio is where it is not possible to increase the
It optimises expected return subject to the variance being minimum.
expected return without increasing the variance, or if it is possible to decrease the variance without decreasing
the expected return.
Another problem or con of using minimizing variance is, an investor using variance minimizing method need
estimates of what yi (return) is. In our analysis we used training set returns to find optimal portfolio allocation
x under constraints. However, those values in training maybe completely different to the testing set or an
The
overlays of LP and QP show these market movements.
unexpected event shocked the market changing all the return rates. Therefore, allocation strategy used in
training set may be irrelevant and can be dangerous to use to find expected return of the testing set. Another,
disadvantage of minimizing variance is that it neglects any nonlinear dependence that might be present. In real
life, investment returns are nonlinear. Your reference?
In our example, optimizing using linear programming to maximize expected return the mean test return is
-5.957 (expected loss) whereas in quadratic programming to minimize variance the mean test return

profitable
is 9.323 (expected gain). Therefore, we can see that minimizing variance here is more powerful
than
maximizing expected return approach. Therefore quadratic programming to minimize variance
would be a more useful profitable strategy.P
One of the pros of maximizing expected return via linear programming is that we allocate budget to indices to
maximize mean monthly return for the training set therefore maximize profit.
meanTestreturn

-5.95748299573666

9.32363502669894

d) How did your demands for a certain return R affect the optimal portfolio?

Plateau at R = -0.071

Figure d.1

P
P

Figure d.2

for the LQ strategy.

Figure d.1 shows a plot of expected return against demand for certain return R-values. Negative R
values means that we tolerate minimal loss each month in order to maximize returns over time.
Consequently, negative R returns would result in optimal portfolio in our case. First figure shows
that maximum expected return of 4.177 is when minimum return R is -0.05, which is the verge of our
negative threshold for R.PAs the R-value decreases to zero from -0.05 expected returns reduces
linearly and goes to zero.P
As the demand for certain return on the investment (R) increases beyond
zero the expected return is negative (expected loss). However, these R values greater than 0 are non
feasible solutions.POptimal portfolio is when the expected return is maximum this is when R is -0.05
or -5%. You have not answered the question. How does the portfolio change as R is changed?

Why does Figure d.1 have the characteristic shape it does?

Variance is mean variance of monthly-expected return. Figure d.2 shows a plot of variance against
range of R values. Figure indicates that for all the R values less than zero, variance is a relatively
constant line about 489. For R values greater than zero, the variance corresponding to those values
increase exponentially as depicted in the above figure. However, when R-values go beyond 0.01,
mean variance of the monthly return are non-feasible solutions therefore set to zero.POptimal
portfolio is when the variance is minimum this is when R is -0.05 or -5%.

R = -0.05 is NOT the largest R for which the variance is 489; for your data Rmax = -0.001.
How does the portfolio change as R is changed? Why does Figure d.2 have the characteristic
shape it does? Have a look at the portfolio plots I have returned to you with your assignment.

Please copy and paste your MATLAB code for LP.m


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% LP

Hashan Wickramasuriya

%%%%

23417250

%%%%%%%%

%%%%%%%%

clc
clear all
close all
% This script is to solve the portfolio optimisation problem using linear
% programming for a given subset of 10 stock indices
data = csvread('returns.dat',1,1); % reads in the monthly returns data file for all
19 stock indices
% please change the next line to select the ten stocks you have been
% allocated (see ECE3093ID-Indices.xls for your allocated list)
indices = [19, 18,

5,

P% select the indices in your subset

13, 14, 17, 11, 12, 15, 8];

of 10 from 19 available indices

subset=[data(:,indices(1)),data(:,indices(2)),data(:,indices(3)),data(:,indices(4
)),data(:,indices(5)),data(:,indices(6)),data(:,indices(7)),data(:,indices(8)),da
ta(:,indices(9)),data(:,indices(10))];
training=subset(1:25,:);

% take first 25 months as training data to optimise portfolio

weights
testing=subset(26:49,:);

% take last 24 months as testing data to evaluate

performance
N=10;
T=25;

% number of stock indices considered in portfolio


% months in training data

Ttest=24; % months in testing data


C=1000;

% dollar budget constraint

R=-0.05:0.001:0.05;

% return threshold, to be varied

mu=0.5;
%No inequalities exist
Aeq=[];
beq=[];

%Defines vector for lower and upper bounds on the design variables x
LB=zeros(length(indices),1);
UB(1:length(indices),1)=mu*C;
% Define the linear coefficients in the constraints A*x ? b.

% Left hand side values of the constraints


A=zeros(T+1,N);
A(1:T,:)=-training;
A(end,:)=1;

% Right hand side values of the constraints


b=zeros(T+1,1);
b(end)=C;
%Defines linear objective function vector f
f=-(1/T)*sum(training,1)';

% Level of 'display off' means that it displays no output.


options = optimset('Display','off');
% Create zero matrix to predefine variables
x=zeros(length(R),10);
FVAL=zeros(length(R),1);
EXITFLAG=zeros(length(R),1);
%Initialise count
count=1;
%Initialise empty matrices to obtain values
optimal_FVAL=[];
optimal_x=[];
%Keeps track of the corresponing R value which satisfy below if statement
track_steps=[];
%For loop to see the effects of the range of R values
for steps=1:length(R)
b(1:T)=-R(steps)*C;

%Define the constant vector in the constraints A*x ? b.

[x(steps,:),FVAL(steps,1),EXITFLAG(steps,1)]=linprog(f,A,b,Aeq,beq,LB,UB,[],optio
ns); %Solve linear programming problems
if(EXITFLAG(steps,1)==1)

%Exitflag 1 corresponds to algorithm terminated when

function converged to a solution x


track_steps(count,1)=steps; % Tracking R steps satisfy the condition
optimal_FVAL(count,1)=FVAL(steps,1); % Copy FVALs and X values which satisfy
the condition into predefined matrices
optimal_x(count,:)=x(steps,:);
gives max expected return
count=count+1;
end
end

% These values are the optimal values which

%ExpectedReturn=-1*round(FVAL/0.01)*0.01; %report to two decimal places


optimalexpectedreturn = -1*round(optimal_FVAL/0.01)*0.01;
% Sort Optimal expected return from highest to lowest and returns
% corresponding
[sorted_Max_ER

array of indices IX(ER_index)


ER_index] = sort(optimalexpectedreturn,'descend');

%Max Expected return


Max_Expected_return = sorted_Max_ER(1);
% Gives the optimal allocation configuration
x = optimal_x(ER_index(1),:)';

% calculate returns (min, max and mean) each month on training and test data using
your best x
% % (designed to maximise expected return on training data)
meanTrainreturn=mean(training*x);
minTrainreturn=min(training*x);
maxTrainreturn=max(training*x);
meanTestreturn=mean(testing*x);
minTestreturn=min(testing*x);
maxTestreturn=max(testing*x);
% calculate variance each month on training and test data using your best x
% (designed to maximise expected return on training data)
meanTrainvariance=mean(x'*cov(training)*x);
meanTestvariance=mean(x'*cov(testing)*x);

figure(1)
%Subplot for expected return for training and testing set
subplot(2,1,1);
Expected_return=training*x;
month=1:25;
plot(month,Expected_return,'*-b')
xlabel('Month')
ylabel('Expected Monthly Return ($)')
title('Training set Expected Monthly Return LP')
grid
subplot(2,1,2);
Expected_return=testing*x;
month=26:49;
plot(month,Expected_return,'*-r')

xlabel('Month')
ylabel('Expected Monthly Return ($)')
title('Testing set Expected Monthly Return LP')
grid
% Mapping Expected Monthly Return for both Training and testing
figure(2)
Expected_return=training*x;
month=1:25;
plot(month,Expected_return,'*-b')
grid
hold on
Expected_return=testing*x;
month=1:24;
plot(month,Expected_return,'*-r')
xlabel('Month number')
ylabel('Expected Monthly Return ($)')
title('Mapping Expected Monthly Return for both Training and testing

LP')

legend('Training set','Testing set')


grid on
hold off

% Combined Plot of training and testing data for expected monthly return
% Training part
figure(3)
subplot(2,1,[1 2]);
Expected_return=training*x;
month=1:25;
plot(month,Expected_return,'*-b')
hold on
% Testing part
Expected_return=testing*x;
month=26:49;
plot(month,Expected_return,'*-r')
xlabel('Month')
ylabel('Expected Monthly Return ($)')
title('Combined Plot of training and testing data for Expected Monthly Return LP')
legend('Training set','Testing set')
grid
% plot of Expected returns with the R range
figure (4)
All_expected_return=-1*round(FVAL/0.01)*0.01;
plot(R,All_expected_return)

xlabel('R value')
ylabel('Expected Return')
title('Expected Return Vs R value')
grid

Please copy and paste your MATLAB code for QP.m


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% QP

Hashan Wickramasuriya

%%%%

23417250

%%%%%%%%

%%%%%%%%

clc
clear all
close all
%

This script is to solve the portfolio optimisation problem using quadratic

programming on
% Markowitz' model (minimising variance) for a given subset of 10 stock indices
data = csvread('returns.dat',1,1); % reads in the monthly returns data file for all
19 stock indices
% please change the next line to select the ten stocks you have been
% allocated (see ECE3093ID-Indices.xls for your allocated list)
indices = [19, 18,

5,

13, 14, 17, 11, 12, 15, 8]; % select the indices in your subset

of 10 from 19 available indices

subset=[data(:,indices(1)),data(:,indices(2)),data(:,indices(3)),data(:,indices(4
)),data(:,indices(5)),data(:,indices(6)),data(:,indices(7)),data(:,indices(8)),da
ta(:,indices(9)),data(:,indices(10))];
training=subset(1:25,:);

% take first 25 months as training data to optimise portfolio

weights
testing=subset(26:49,:);

% take last 24 months as testing data to evaluate

performance
N=10;
T=25;

% number of stock indices considered in portfolio


% months in training data

Ttest=24; % months in testing data


C=1000;

% dollar budget constraint

R=-0.05:0.001:0.05;
mu=0.5;

% return threshold, to be varied

% Now write some code that defines the matrices and vectors needed to solve
% the quadratic programming problem to minimise variance. You need to
% define H, f, A, b, Aeq, beq, LB, UB in the quadprog function
%define objective function
H=2*cov(training); % H is defined to be the covarience between yi & yj

% Define the linear coefficients in the constraints A*x ? b.

A=-(1/T)*sum(training); % YBAR is the mean yi for each index ( average of each individual
column)

% Define the lower bound(LB) and upper bound (UB) elementwise in lb ? x ? ub.
LB=zeros(N,1);
UB(1:N,1)=mu*C;
% Define linear coefficients in the constraints Aeq*x = beq.
Aeq(1,1:N)=1;

beq=C;

f=zeros(N,1);
% Options structure created using optimset
options = optimset( 'Algorithm', 'interior-point-convex','Display','off');
% Create zero matrix to predefine variables
EXITFLAG=zeros(length(R),1);
optimal_Va=[];
optimal_x=[];
%Keeps track of the corresponing R value which satisfy below if statement
track_R=[]; count=1;
%For loop to see the effects of the range of R values
for steps=1:length(R)
b=-R(steps)*C; %Define the constant vector in the constraints A*x ? b.
% Define Quadratic programming
[x,FVAL,EXITFLAG(steps,1)]=quadprog(H,f,A,b,Aeq,beq,LB,UB,[],options);
%%Exitflag 1 corresponds to algorithm terminated when function converged to a
solution x
if EXITFLAG(steps,1)==1
optimal_Va(steps,1)=round(FVAL/0.01)*0.01;
optimal_x(steps,:)=x;
track_R(count)=1;
count=count+1;
else

optimal_Va(steps)=0;
optimal_x(steps,:)=0;
end
end
% Sort feasible value from lowest to highest and returns
% corresponding

array of indices IX(variance_index)

[feasible_variance

variance_index] = sort(optimal_Va,'ascend');

% Optimal variance where it is the minimum variance


Optimal_variance = feasible_variance(1);
% Optimal allocation value
x = optimal_x(variance_index(1),:)';

% calculate returns (min, max and mean) each month on training and test data using
your best x
% (designed to minimise variance on training data)
meanTrainreturn=mean(training*x);
minTrainreturn=min(training*x);
maxTrainreturn=max(training*x);
meanTestreturn=mean(testing*x);
minTestreturn=min(testing*x);
maxTestreturn=max(testing*x);
% calculate variance each month on training and test data using your best x
% (designed to minimise variance on training data)
meanTrainvariance=mean(x'*cov(training)*x);
meanTestvariance=mean(x'*cov(testing)*x);

figure(1)
%Subplot for expected return for training and testing set
subplot(2,1,1);
Expected_return=training*x;
month=1:25;
plot(month,Expected_return,'*-b')
xlabel('Month')
ylabel('Expected Monthly Return ($)')
title('Training set Expected Monthly Return QP')
grid
subplot(2,1,2);
Expected_return=testing*x;

month=26:49;
plot(month,Expected_return,'*-r')
xlabel('Month')
ylabel('Expected Monthly Return ($)')
title('Testing set Expected Monthly Return QP')
grid
% Mapping Expected Monthly Return for both Training and testing
figure(2)
Expected_return=training*x;
month=1:25;
plot(month,Expected_return,'*-b')
grid
hold on
Expected_return=testing*x;
month=1:24;
plot(month,Expected_return,'*-r')
xlabel('Month number')
ylabel('Expected Monthly Return ($)')
title('Mapping Expected Monthly Return for both Training and testing

QP')

legend('Training set','Testing set')


grid on
hold off

% Combined Plot of training and testing data for expected monthly return
% Training part
figure(3)
subplot(2,1,[1 2]);
Expected_return=training*x;
month=1:25;
plot(month,Expected_return,'*-b')
hold on
% Testing part
Expected_return=testing*x;
month=26:49;
plot(month,Expected_return,'*-r')
xlabel('Month')
ylabel('Expected Monthly Return ($)')
title('Combined Plot of training and testing data for Expected Monthly Return QP')
legend('Training set','Testing set')
grid
% plot of Expected returns with the R range

figure (4)
plot(R,optimal_Va)
xlabel('R value')
ylabel('Variance')
title('Variance Vs R value')
grid

S-ar putea să vă placă și