How to solve this convex optimization? - scipy

It is simple, I know but I have little understanding of convex optimization yet
Problem definition:
Objective function is II b - Aw II norm 2
a vector of unknown [w1, w2, ..., wn]
a data matrix A (m x n), each row has n components([ai1, ai2, ..., ain]), m is the number of measures. ai1, ai2, ..., ain are themselves highly correlated to each other
constraints wi >= 0 and sum of wi is 1, basically wi can be interpreted as weights
which python or matlab package could I use, maybe ?

This is a problem in Quadratic Programming.
Python - CVXOPT
Matlab - quadprog

Related

Error finding the simplest row form of a matrix

This problem was discovered when I was solving this second order differential equation by matlab
syms x(t)
diff(x,t,2)+k*diff(x,t)+b*x==0
I transform it into a system of first order equations, whose coefficient matrix is
syms b k
A=[0 1;
-b-k]
Then I solve this system by following code
syms x1(t) x2(t)
X=[x1;x2];
odes=diff(X)==A*X;
[x1sol(t),x2sol(t)]=dsolve(odes);
x1sol=simplify(x1sol(t))
x2sol=simplify(x2sol(t))
But this solution is different from the one I calculated manually, because the correct answer is based on the eigenvalues and eigenvectors of A:
λ1=(-b-(b^2-4*k)^(1/2))/2
v1=[-1;
(b+(b^2-4*k)^(1/2))/2]
and
λ2=(-b+(b^2-4*k)^(1/2))/2
v2=[-1;
(-b+(b^2-4*k)^(1/2))/2]
Therefore, Instead of solving the system directly, I calculate the the eigenvalues and eigenvectors of A directly
[V,D]=eig(A)
or indirectly
syms k b t
I=eye(2);
A=[0 1;-k -b];
e=eig(A);
B1=e(1)*I-A;
B2=e(2)*I-A;
P1=null(B1)
P2=null(B2)
By comparing the results of MATLAB and manual calculation, I find that the eigenvalues are correct calculated by both means, but the eigenvectors are wrong calculated by both means either (and they are same with dsolve(odes) above). If P1 is the solution to B1X=0 (means P1 is one of the eigenvector of A), then B1P1=0 should be true, so does P2.
Finally, I find that MATLAB seems to make an error when converting B1 a to the simplest row form
B1=
[- b/2 - (b^2 - 4*k)^(1/2)/2, -1]
[ k, b/2 - (b^2 - 4*k)^(1/2)/2]
rrefB1=rref(B1)
ans=
[1, (b - (b^2 - 4*k)^(1/2))/(2*k)]
[0, 0]
and I check rrefB1 multiplies P1, and it equals to 0.
So, the problem is that B1 multiplies P1 doesn't equals to 0, but rrefB1 multiplies P1 equals to 0.
In theory, The original matrix (B1) and the simplest row form of it (rrefB1) should have the same fundamental system of solutions.
What's wrong here?

Matlab: fmincon, Portfolio Optimization, difficulties with free variables

i´m trying to replicate a paper ("Is the marketporfolio mean-variance efficient after all?" from Levy & Roll) in Matlab. I want to minimize the following function (D) with its constraints:
Objective function
Constraints
I want to get mu and sigma,
furthermore alpha, q and r_z are free variables, where
alpha: 0 ≤ α ≤ 1 is a parameter determining
the relative weight assigned to deviations of the means relative to deviations
of the standard deviations.
q: q > 0 is the constant of proportionality
r_z : zero beta rate (risk free interest rate)
I have:
mu^sam: Vector of means of historical returns
sigma^sam: Vector of standard deviation of historical returns
Covariance matrix
weights x_m,i
Now i don´t know how to handle the free variables alpha, q and r_z
My objective function D :
function dist=distOpt(alpha, N, x0, mu_sam, sigma_sam)
sumSigma=0;
for i=1:N
sumMu=sumMu+((x0(i)-mu_sam(i))/sigma_sam(i))^2;
sumSigma=sumSigma+((x0(N+i)-sigma_sam(i))/sigma_sam(i))^2;
end
dist=sqrt(alpha*(1/N)*sumMu + (1-alpha)*(1/N)*sumSigma);
And constraints:
Mu_sam=repelem(-0.0001,y-1);
Sigma_sam=repelem(-0.0002,y-1);
x0=[Mu_sam Sigma_sam];
function [c,ceq] = confuneq(x0,rz,Cor,Weights)
q=1;
ceq=diag(x0(7:end))*Cor*diag(x0(7:end))*Weights' - q*(x0(1:6)'-rz);
c=[];
My use of fmincon:
optFunc=#(x0) distOpt(alpha,(y-1),x0, Mu_sam, Sig_sam);
options = optimoptions(#fmincon,'Algorithm','sqp','MaxFunctionEvaluations', 12000,'FunValCheck','on');
nonlcon=#(c,ceq) confuneq(x0,r_z, rho, Weights);
[x0, fval, exitFlag]= fmincon(optFunc,x0,[],[],[],[],[],[],nonlcon,options);
Can anyone help?
Now i don´t know how to handle the free variables alpha, q and r_z
I think you have two options:
1) Choose fix values for them.
2) Include them into your x to get optimal values.

Using Matlab to Minimize the amplitude of this sum

I have output matrix Y as:
Y=E*A;
where
E=Exponential phase vectors (1xM)
A=Fixed complex numbers (MxN)
say,
M=4;N=256;
a = complex(randn(4,256),randn(4,256)); % coefficient matrix of "A"
theta=has four values
Objective:
What range of theta will minimize the peak of sum expression of a kth column
of "Y" ? i.e.,
Theta subjected to and m=1,2,...,M
for e.g.,
Min: Y(theta)=Peak{a11*exp(j*theta1)+ a21*exp(j*theta2)+ ...+aM1*exp(j*thetaM)}
My Question:
Can i use MATLAB to develop a logic to formulate such a problem and solve it ?
I think this is related to Linear Programming with constraints (??).
My approach will be to use the fmincon function from the matlab optimization toolbox.
Your theta vector would be the x vector of solutions you're looking for.
Then specify your problem by defining the objective function f(x) as the max from Yk for all K between 0 and N.
objectiveFunction = #(x) maxY(...
x,... %
A);
Express your constraint 0<=theta<2pi with the individuals constraints lb and ub (lower and upper bounds).
Then call fmincon
options = optimset('Algorithm','sqp', ...
'Display', 'iter-detailed',...
'UseParallel', 'always',...
'MaxFunEvals', 3000);
[result, ~, ~] = fmincon(objectiveFunction,x0,[],[],[],[],lb,ub,[],options);

MATLAB: fmincon with three-dimensional data

I want to estimate three parameters while minimizing the least squares quadratic error with the function fmincon in MATLAB. My objective function looks like:
f = #(a,b,c) sum(sum(sum((M - a - b - c).^2)));
where M is a 3D array with dimensions 20x7x16 and the estimated parameters a, b, c are vectors with dimensions 20x1, 7x1 and 16x1 respectively. In order to estimate it I 'make' them 3D as well by repeating the vector a into the array 20x7x16 and I do the same for b and c. I need the sum of the elements in vector a and b to be 1 as linear constraints. My problems are two:
How should I specify the linear constraints when Aeq is a 2D matrix and beq a vector?
How can I set the starting points for a,b,c so that MATLAB knows that the estimates of them are vectors repeated in this 3D array?
I wanted to unfold the 3D array M into 2D matrix and adjust the the parameters a,b,c but the problem with starting points is still there since I must define them as a vector and not as a matrix.
I would very appreciate your ideas and suggestions. Probably I'm thinking to complicated and there's another way how to do it.
Thank you in advance.
DO NOT REPLICATE a b and c! use bsxfun instead
f = #(a,b,c) sum( reshape( bsxfun( #minus, bsxfun( #minus, bsxfun(#minus, M, a), b' ), permute( c, [2 3 1] ) ), [], 1 ) )
Now your parameters are vecotrs and not replicates of vectors. I believe this will solve all your other problems as well.

Calculate distance from point p to high dimensional Gaussian (M, V)

I have a high dimensional Gaussian with mean M and covariance matrix V. I would like to calculate the distance from point p to M, taking V into consideration (I guess it's the distance in standard deviations of p from M?).
Phrased differentially, I take an ellipse one sigma away from M, and would like to check whether p is inside that ellipse.
If V is a valid covariance matrix of a gaussian, it then is symmetric positive definite and therefore defines a valid scalar product. By the way inv(V) also does.
Therefore, assuming that M and p are column vectors, you could define distances as:
d1 = sqrt((M-p)'*V*(M-p));
d2 = sqrt((M-p)'*inv(V)*(M-p));
the Matlab way one would rewrite d2as (probably some unnecessary parentheses):
d2 = sqrt((M-p)'*(V\(M-p)));
The nice thing is that when V is the unit matrix, then d1==d2and it correspond to the classical euclidian distance. To find wether you have to use d1 or d2is left as an exercise (sorry, part of my job is teaching). Write the multi-dimensional gaussian formula and compare it to the 1D case, since the multidimensional case is only a particular case of the 1D (or perform some numerical experiment).
NB: in very high dimensional spaces or for very many points to test, you might find a clever / faster way from the eigenvectors and eigenvalues of V (i.e. the principal axes of the ellipsoid and their corresponding variance).
Hope this helps.
A.
Consider computing the probability of the point given the normal distribution:
M = [1 -1]; %# mean vector
V = [.9 .4; .4 .3]; %# covariance matrix
p = [0.5 -1.5]; %# 2d-point
prob = mvnpdf(p,M,V); %# probability P(p|mu,cov)
The function MVNPDF is provided by the Statistics Toolbox
Maybe I'm totally off, but isn't this the same as just asking for each dimension: Am I inside the sigma?
PSEUDOCODE:
foreach(dimension d)
(M(d) - sigma(d) < p(d) < M(d) + sigma(d)) ?
Because you want to know if p is inside every dimension of your gaussian. So actually, this is just a space problem and your Gaussian hasn't have to do anything with it (except for M and sigma which are just distances).
In MATLAB you could try something like:
all(M - sigma < p < M + sigma)
A distance to that place could be, where I don't know the function for the Euclidean distance. Maybe dist works:
dist(M, p)
Because M is just a point in space and p as well. Just 2 vectors.
And now the final one. You want to know the distance in a form of sigma's:
% create a distance vector and divide it by sigma
M - p ./ sigma
I think that will do the trick.