# Function that solves for the efficient portfolio and minimisation problem

1 view (last 30 days)
Tai Lopez on 16 Nov 2018
I have a task to create a function that solves for the efficient portfolio. I've crated a function but I'm getting an error in line 3 ( N = numel...) and I'm not sure about line 11 ( [ x, fval ] = fmincon(@effportobj , x0 , [], [], Aeq, Beq, [], [], [], [], Sigma); )
Does anyone know what I'm doing wrong?
this is the task:
Create a function that solves for the efficient portfolio, given a set of stocks, their expected value, their variance-covariance matrix, and a target expected value of the investment. The function should use either the MATLAB minimisation procedure fmincon or quadprog.
Inputs:
procedure a string containing the MATLAB procedure used to perform the minimization ('quadprog' or 'fmincon') Sigma Variance-covariance matrix (reminder: this is a positive semi-definite matrix!) mu vector of expected values for each stock mu_p_0 expected return initial_guess for fmincon, you need to give an initial guess, can be left as [] if using quadprog optimisation_options can be added, but otherwise set to default by using [] as the value for this input
Outputs:
x optimal shares for each stock fval value of the optimisation expected_return as a check, needs to be equal to mu_p_0
When using fmincon: the objective function for the optimisation will be a local function called effportobj (already in the solution template). This function takes as inputs: the optimal shares x, and the variance-covariance matrix Sigma. In the main function, when calling this objective function in the optimisation procedure fmincon, use the function handle (starting with @).
My code:
function [ fval, x] = effportfoliol( mu, initial_guess, Sigma, mu_p_0)
N = numel(mu);
L = ones(N,1);
lb = 0;
ub = 1;
Aeq = [mu';L'];
Beq = [mu_p_0;1];
x0 = initial_guess;
[ x, fval ] = fmincon(@effportobj , x0 , [], [], Aeq, Beq, [], [], [], [], Sigma);
expected_return = x'*mu;
if expected_return == mu_p_0
disp("expected return = mu_p_0");
else
disp([]);
end
end
function [ f ] = effportobj( x, Sigma )
f = (x')*Sigma*(x);
end