How to define a negative constraint only in Aeq * X <= Beq - matlab

I am using quadprog link to find a portfolio of optimal weights.
So far, I have managed to implement a long only constraint (i.e. weights cannot be smaller than zero w >= 0 and w1 + w2 + ... wN = 1) as follows:
FirstDegree = zeros(NumAssets,1);
SecondDegree = Covariance;
Aeq = ones(1,NumAssets);
beq = 1;
A = -eye(NumAssets);
b = zeros(NumAssets,1);
x0 = 1/NumAssets*ones(NumAssets,1);
MinVol_Weights = quadprog(SecondDegree,FirstDegree,A,b,Aeq,beq,[],[],x0, options);
I am now tring to setup a short-only constraints, i.e. all the weights need to add up to -1 and they should be all strict smaller or equal to zero. How can this be rewritten?

Note that you can rewrite any “greater than” inequality a ≥ b into a “less than” inequality -a ≤ -b by flipping signs. In your example, choose
Aeq = ones(1,NumAssets);
beq = -1;
A = eye(NumAssets);
b = zeros(NumAssets,1);
That means Aeq*w == w(1) + w(2) + … + w(NumAssets) == -1, and A*w <= 0 which is the same as saying w(i) <= 0 for all i.

Related

Finding the right solver

I need to solve the following non-linear problem with constraints but I am not sure to use the appropriate function and solver as changing from the algorithm: 'interior-point' to 'sqp' give different answers. Could you help first to make sure fmincon is the most appropriate function here and also if the options should be set up in a different way to ensure that the optimization does not stop too early. I have tried to set up: 'MaxFunctionEvaluations' to 1000000 and 'MaxIterations'to 10000 and this helps but the time required increases as well dramatically.
w is a vector whose values have to be optimized.
scores is a vector of the same size as w.
the size if w and scores is usually between 10 and 40.
Here is the optimization setup:
objective function: sum(log(abs(w)));
linear inequalities1: w_i > 0 if scores_i > 0
linear inequalities2: w_i < 0 if scores_i < 0
non-linear equalities1: sum(abs(w)) = 1
non-linear inequalities1: sqrt(w * sigma * w') <= 0.1
Here is my code:
N = numel(scores);
%%%LINEAR EQUALITIES CONSTRAINTS
Aeq = [];
beq = [];
%%%LINEAR INEQUALITIES CONSTRAINTS
temp = ones(0,N);
temp(scores >= 0) = -1;
temp(scores < 0) = 1;
A = diag(temp);
b = zeros(N,1);
%%%BOUNDs CONSTRAINTS
lb = [];
ub = [];
w0 = ones(1,N)/N;
nonlcon = #(w)nonlconstr(w,sigma,tgtVol);
options = optimoptions('fmincon','Algorithm','interior-point','MaxFunctionEvaluations',1000000,'MaxIterations',10000);
%options = optimoptions('fmincon','Algorithm','interior-point','OptimalityTolerance',1e-7);
[w,fval,exitflag,output] = fmincon(#(w)objectfun(w),w0,A,b,Aeq,beq,lb,ub,nonlcon,options);
function [c,ceq] = nonlconstr(w,sigma,tgtVol)
c = sqrt(w*sigma*w') - tgtVol;
ceq = sum(abs(w)) - 1;
end
function f = objectfun(w)
f = sum(-log(abs(w)));
end

Genetic Algorithm Constraints

How can I pass the following constraints to the Matlab ga optimization function? Note that x is a vector 1xnvars
Constraint 1
0.2 <= sum(x,2)/(W*H) <= 0.4
where `W` and `H` are two constant.
Constraint 2
x(1) >= x(2) >= ... >= x(size(x,1))
Refer here for the documentation.
A = cat(1,ones(1,nvars), ones(1,nvars)*-1)/W/H;
b = [0.4;-0.2];
function [ceq ce] = noncolon1(x)
ce = [];
ceq = x(1,2:end) - x(1,1:end-1);
end
[x , fval] = ga(#fitnessfunc,nvars,A,b,[],[],[],[],#noncolon1);

Optimazition MFs of Fuzzy inference by genetic algorithm

I am using GA to optimize the parameters of the membership functions in my fuzzy system.
I create a function for fitness:
function y = gafuzzy(x)
global FISsys
global allData
global realResult
FISsys = readfis('aCAess.fis');
allData = importdata('ab.mat');
realResult = importdata('ad.mat');
FISsys.input(1,1).mf(1,1).params = [x(1) x(2) x(3)];
FISsys.input(1,1).mf(1,2).params = [x(4) x(5) x(6)];
FISsys.input(1,2).mf(1,1).params = [x(7) x(8) x(9)];
FISsys.input(1,2).mf(1,2).params = [x(10) x(11) x(12)];
FISsys.output.mf(1,1).params = [x(13) x(14) x(15)];
FISsys.output.mf(1,2).params = [x(16) x(17) x(18)];
c = evalfis(allData,FISsys);
e=sum(abs(c-realResult));
y = e;
end
And A[15*18] matrix for linear inequalities is :
A = [1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,1,-1,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,1,-1,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1;
0,1,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,1,0,0,-1,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,-1,0]
and b[15*1] vector is:
b = [0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]
but when I run GA, I get this error:
Illegal parameters in fisTriangleMf() --> a > b
why?
Generally, in the triangle MF the first number, here a (shows the left vertex) should be smaller than the second number, here b (the top vertex). So you can have a triangle MF like [-1 0 1] but it cannot be like [0 -1 1].
in your code, I assume sometimes you don't satisfy the inequality in one of those places:
[x(1) < x(2) < x(3)];
[x(4) < x(5) < x(6)];
[x(7) < x(8) < x(9)];
[x(10) < x(11) < x(12)];
....
if the program is randomizing these values, you can bound them in your code easily by checking and replacing, for instance:
if x(1) >= x(2)
tmp = x(1);
x(1) = x(2);
x(2) = tmp;
end

Solving a difference equation with initial condition

Consider a difference equation with its initial conditions.
5y(n) + y(n-1) - 3y(n-2) = (1/5^n) u(n), n>=0
y(n-1) = 2, y(n-2) = 0
How can I determine y(n) in Matlab?
Use an approach similar to this (using filter), but specifying initial conditions as done here (using filtic).
I'm assuming your initial conditions are: y(-1)=2, y(-2)=0.
num = 1; %// numerator of transfer function (from difference equation)
den = [5 1 -3]; %// denominator of transfer function (from difference equation)
n = 0:100; %// choose as desired
x = (1/5).^n; %// n is >= 0, so u(n) is 1
y = filter(num, den, x, filtic(num, den, [2 0], [0 0]));
%// [2 0] reflects initial conditions on y, and [0 0] those on x.
Here's a plot of the result, obtained with stem(n,y).
The second line of your code does not give initial conditions, because it refers to the index variable n. Since Matlab only allows positive integer indices, I'll assume that you mean y(1) = 0 and y(2) = 2.
You can get an iteration rule out of your first equation by simple algebra:
y(n) = ( (1/5^n) u(n) - y(n-1) + 3y(n-2) ) / 5
Code to apply this rule in Matlab:
n_max = 100;
y = nan(n_max, 1);
y(1) = 0;
y(2) = 2;
for n = 3 : n_max
y(n) = ( (1/5^n) * u(n) - y(n-1) + 3 * y(n-2) ) / 5;
end
This code assumes that the array u is already defined. n_max specifies how many elements of y to compute.

Abnormal behavior algorithm implemented in matlab depending of the input

I'm doing a homework assignment for scientific computing, specifically the iterative methods Gauss-Seidel and SOR in matlab, the problem is that for a matrix gives me unexpected results (the solution does not converge) and for another matrix converges.
Heres the code of sor, where:
A: Matrix of the system A * x = b
Xini: array of initial iteration
b: array independent of the system A * x = b
maxiter: Maximum Iterations
tol: Tolerance;
In particular, the SOR method, will receive a sixth parameter called w which corresponds to the relaxation parameter.
Here´s the code for sor method:
function [x2,iter] = sor(A,xIni, b, maxIter, tol,w)
x1 = xIni;
x2 = x1;
iter = 0;
i = 0;
j = 0;
n = size(A, 1);
for iter = 1:maxIter,
for i = 1:n
a = w / A(i,i);
x = 0;
for j = 1:i-1
x = x + (A(i,j) * x2(j));
end
for j = i+1:n
x = x + (A(i,j) * x1(j));
end
x2(i) = (a * (b(i) - x)) + ((1 - w) * x1(i));
end
x1 = x2;
if (norm(b - A * x2) < tol);
break;
end
end
Here´s the code for Gauss-seidel method:
function [x, iter] = Gauss(A, xIni, b, maxIter, tol)
x = xIni;
xnew = x;
iter = 0;
i = 0;
j = 0;
n = size(A,1);
for iter = 1:maxIter,
for i = 1:n
a = 1 / A(i,i);
x1 = 0;
x2 = 0;
for j = 1:i-1
x1 = x1 + (A(i,j) * xnew(j));
end
for j = i+1:n
x2 = x2 + (A(i,j) * x(j));
end
xnew(i) = a * (b(i) - x1 - x2);
end
x= xnew;
if ((norm(A*xnew-b)) <= tol);
break;
end
end
For this input:
A = [1 2 -2; 1 1 1; 2 2 1];
b = [1; 2; 5];
when call the function Gauss-Seidel or sor :
[x, iter] = gauss(A, [0; 0; 0], b, 1000, eps)
[x, iter] = sor(A, [0; 0; 0], b, 1000, eps, 1.5)
the output for gauss is:
x =
1.0e+304 *
1.6024
-1.6030
0.0011
iter =
1000
and for sor is:
x =
NaN
NaN
NaN
iter =
1000
however for the following system is able to find the solution:
A = [ 4 -1 0 -1 0 0;
-1 4 -1 0 -1 0;
0 -1 4 0 0 -1;
-1 0 0 4 -1 0;
0 -1 0 -1 4 -1;
0 0 -1 0 -1 4 ]
b = [1 0 0 0 0 0]'
Solution:
[x, iter] = sor(A, [0; 0; 0], b, 1000, eps, 1.5)
x =
0.2948
0.0932
0.0282
0.0861
0.0497
0.0195
iter =
52
The behavior of the methods depends on the conditioning of both matrices? because I noticed that the second matrix is better conditioned than the first. Any suggestions?
From the wiki article on Gauss-Seidel:
convergence is only guaranteed if the matrix is either diagonally dominant, or symmetric and positive definite
Since SOR is similar to Gauss-Seidel, I expect the same conditions to hold for SOR, but you might want to look that one up.
Your first matrix is definitely not diagonally dominant or symmetric. Your second matrix however, is symmetric and positive definite (because all(A==A.') and all(eig(A)>0)).
If you use Matlab's default method (A\b) as the "real" solution, and you plot the norm of the difference between each iteration and the "real" solution, then you get the two graphs below. It is obvious the first matrix is not ever going to converge, while the second matrix already produces acceptable results after a few iterations.
Always get to know the limitations of your algorithms before applying them in the wild :)