Using Matlab to Minimize the amplitude of this sum - matlab

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);

Related

How exactly works this simple calculus of a ML gradient descent cost function using Octave\MatLab?

I am following a machine learning course on Coursera and I am doing the following exercise using Octave (MatLab should be the same).
The exercise is related to the calculation of the cost function for a gradient descent algoritm.
In the course slide I have that this is the cost function that I have to implement using Octave:
This is the formula from the course slide:
So J is a function of some THETA variables represented by the THETA matrix (in the previous second equation).
This is the correct MatLab\Octave implementation for the J(THETA) computation:
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
J = (1/(2*m))*sum(((X*theta) - y).^2)
% =========================================================================
end
where:
X is a 2 column matrix of m rows having all the elements of the first column set to the value 1:
X =
1.0000 6.1101
1.0000 5.5277
1.0000 8.5186
...... ......
...... ......
...... ......
y is a vector of m elements (as X):
y =
17.59200
9.13020
13.66200
........
........
........
Finnally theta is a 2 columns vector having 0 asvalues like this:
theta = zeros(2, 1); % initialize fitting parameters
theta
theta =
0
0
Ok, coming back to my working solution:
J = (1/(2*m))*sum(((X*theta) - y).^2)
specifically to this matrix multiplication (multiplication between the matrix X and the vector theta): I know that it is a valid matrix multiplication because the number of column of X (2 columns) is equal to the number of rows of theta (2 rows) so it is a perfectly valid matrix multiplication.
My doubt that is driving me crazy (probably it is a trivial doubt) is related to the previous course slide context:
As you can see in the second equation used to calculated the current h_theta(x) value it is using the transposed theta vector and not the theta vector as done in the code.
Why ?!?!
I suspect that it depends only on how was created the theta vector. It was build in this way:
theta = zeros(2, 1); % initialize fitting parameters
that is generating a 2 line 1 column vector instead of a classic one line 2 column vector. So maybe I have not to transpose it. But I am absolutely not sure about this assertion.
Is my intuition correct or what am I missing?
Your intuition is correct. Effectively it does not matter whether you perform the multiplication as theta.' * X or as X.' * theta, since this either generates a horizontal vector or a vertical vector of the hypothesis representing all observations, and what you're expected to do next is subtract the y vector from the hypothesis vector at each observation, and sum the results. So as long as y has the same orientation as your hypothesis and you subtract at each equivalent point, then the scalar end-result of the summation will be the same.
Often enough, you'll see the X.' * theta version preferred over theta.' * X purely for convenience, to avoid transposing over and over again just to be consistent with the mathematical notation. But this is fine, since the underlying math doesn't really change, only the order of equivalent operations.
I agree it's confusing though, both because it makes it harder to follow the formula when the code effectively looks like it's doing something else, and also since it messes with the usual convention that a vertical vector represents 'coordinates', and a horizontal vector represents observations. In such cases, especially in languages like matlab / octave where the orientation of a vector isn't explicitly defined in the variable's type, it is doubly important to document what you expect the inputs to represent, and preferably there should have been assert statements in the code confirming the input has been passed in the correct orientation. Clearly here they felt it wasn't necessary because this code is acting under controlled conditions in a predefined exercise environment anyway, but it would have been good practice to do so from a software engineering point of view.

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.

Maximize linear objective with nonlinear constraint

I would like to solve a maximization problem involving a linear objective function and nonlinear constraint. In particular, my problem is:
max_x f(x) = c_1x_1
s.t. g_m(x) <= 0 for m = 1,...,M
g_m(x) = 0 for m = M+1,...,N
x in X
where X is a compact subset of a finite dimensional euclidean space. One way to solve this is to use the fmincon function in MatLab. To do this, we call
fmincon(#(x)linobj(x,c),x0,[],[],[],[],lx,ux,nonlinear_constraint,options_fmincon);
where linobj(x,c) is the linear function
function [val,gradient] = linobj(x,c)
% c is a vector with c = (c_1,0,0,...,0)'
val = dot(x,c)
if nargout > 1
gradient = [c(1) 0 0 ... 0].'
end
end
nonlinear_constraint is a function of x that computes the inequality and equality constraint values at x and the gradients, and ux and lx specifies the boundary of X.
I understand that there are fast methods of computing the argmax to a problem that has linear/quadratic objective function with linear/quadratic constraints (e.g., by using CVXGEN). So my question is this: Given the particular structure of this problem, is there a faster way of computing the argmax of this problem (relative to fmincon) in Matlab? In particular, can I use the fact that my objective function is linear to speed up computation?

system of ordinary differential equation in Matlab

Suppose we have this Hamiltonian:
n = 10;
H = ones(n,n);
The density matrix is:
Ro = sym('r',[n,n]);%Density matrix
The equation of motion is:
H*Ro-Ro*H
The above equation of motion is the right hand side of the equation, the left hand side is the time derivative of density matrix.
How can I solve the equation of motion in Matlab without the symbolic math toolbox? I need to change the value of n. It can be up to 100.
In your dynamics function, reshape between vectors and matrices in order to use MATLAB's standard ode functions, which (to my knowledge) require vector inputs. Note, the symbolic toolbox isn't used anywhere in this solution. R can be any size n-by-n, within the constraints of your machine's memory.
function dR = dynfun(R,H)
%// R = n^2-by-1 vector
%// H = n-by-n matrix
n = sqrt(length(R));
R = reshape(R,[n,n]); %// reshape R to n-by-n matrix
dR = H*R-R*H;
dR = dR(:); %// reshape dR to n^2-by-1 vector
end
Call the ODE solver:
[tout,Rout] = ode45(#(t,R) dynfun(R,H), [0,T], R0(:));
where T is final time, R0 is n-by-n initial condition, tout are the output time steps, and Rout is the solution trajectory. Note due to the reshaping, Rout will be k-by-n^2, where k is the number of time steps. You'll need to reshape each row of Rout if you want to have the actual matrices over time.

numerical integration for Gaussian function - indefinite integral

My approach
fun = #(y) (1/sqrt(pi))*exp(-(y-1).^2).*log(1 + exp(-4*y))
integral(fun,-Inf,Inf)
This gives NaN.
So I tried plotting it.
y= -10:0.1:10;
plot(y,exp(-(y-1).^2).*log(1 + exp(-4*y)))
Then understood that domain (siginificant part) is from -4 to +4.
So changed the limits to
integral(fun,-10,10)
However I do not want to always plot the graph and then know its limits. So is there any way to know the integral directly from -Inf to Inf.
Discussion
If your integrals are always of the form
I would use a high-order Gauss–Hermite quadrature rule.
It's similar to the Gauss-Legendre-Kronrod rule that forms the basis for quadgk but is specifically tailored for integrals over the real line with a standard Gaussian multiplier.
Rewriting your equation with the substitution x = y-1, we get
.
The integral can then be computed using the Gauss-Hermite rule of arbitrary order (within reason):
>> order = 10;
>> [nodes,weights] = GaussHermiteRule(order);
>> f = #(x) log(1 + exp(-4*(x+1)))/sqrt(pi);
>> sum(f(nodes).*weights)
ans =
0.1933
I'd note that the function below builds a full order x order matrix to compute nodes, so it shouldn't be made too large.
There is a way to avoid this by explicitly computing the weights, but I decided to be lazy.
Besides, event at order 100, the Gaussian multiplier is about 2E-98, so the integrand's contribution is extremely minimal.
And while this isn't inherently adaptive, a high-order rule should be sufficient in most cases ... I hope.
Code
function [nodes,weights] = GaussHermiteRule(n)
% ------------------------------------------------------------------------------
% Find the nodes and weights for a Gauss-Hermite Quadrature integration.
%
if (n < 1)
error('There is no Gauss-Hermite rule of order 0.');
elseif (n < 0) || (abs(n - round(n)) > eps())
error('Given order ''n'' must be a strictly positive integer.');
else
n = round(n);
end
% Get the nodes and weights from the Golub-Welsch function
n = (0:n)' ;
b = n*0 ;
a = b + 0.5 ;
c = n ;
[nodes,weights] = GolubWelsch(a,b,c,sqrt(pi));
end
function [xk,wk] = GolubWelsch(ak,bk,ck,mu0)
%GolubWelsch
% Calculate the approximate* nodes and weights (normalized to 1) of an orthogonal
% polynomial family defined by a three-term reccurence relation of the form
% x pk(x) = ak pkp1(x) + bk pk(x) + ck pkm1(x)
%
% The weight scale factor mu0 is the integral of the weight function over the
% orthogonal domain.
%
% Calculate the terms for the orthonormal version of the polynomials
alpha = sqrt(ak(1:end-1) .* ck(2:end));
% Build the symmetric tridiagonal matrix
T = full(spdiags([[alpha;0],bk,[0;alpha]],[-1,0,+1],length(alpha),length(alpha)));
% Calculate the eigenvectors and values of the matrix
[V,xk] = eig(T,'vector');
% Calculate the weights from the eigenvectors - technically, Golub-Welsch requires
% a normalization, but since MATLAB returns unit eigenvectors, it is omitted.
wk = mu0*(V(1,:).^2)';
end
I've had success with transforming such infinite-bounded integrals using a numerical variable transformation, as explained in Numerical Recipes 3e, section 4.5.3. Basically, you substitute in y=c*tan(t)+b and then numerically integrate over t in (-pi/2,pi/2), which sweeps y from -infinity to infinity. You can tune the values of c and b to optimize the process. This approach largely dodges the question of trying to determine cutoffs in the domain, but for this to work reliably using quadrature you have to know that the integrand does not have features far from y=b.
A quick and dirty solution would be to look for a position, where your function is sufficiently small enough and then taking it as limits. This assumes that for x>0 the function fun decreases montonically and fun(x) is roughly the same size as fun(-x) for all x.
%// A small number
epsilon = eps;
%// Stepsize for searching bound
stepTest = 1;
%// Starting position for searching bound
position = 0;
%// Not yet small enough
smallEnough = false;
%// Search bound
while ~smallEnough
smallEnough = (fun(position) < eps);
position = position + stepTest;
end
%// Calculate integral
integral(fun, -position, position)
If your were happy with plotting the function, deciding by eye where you can cut, then this code will suffice, I guess.