Matlab: Computing norm of a vector - matlab

a is a n by 1 vector. X is a n by n matrix.
I want to computer a norm of a vector X*a. I know I can do it by norm(X*a). By strangeness arises when I want to do it myself:
If I do it sqrt(a'*X'*X*a)
I got the warning of
Parethesize the multiplication of 'X' and its transpose to ensure the result is Hermetian.”
Therefore should the norm be sqrt(a'*(X'*X)*a) (as the warning suggest) or
sqrt((a'*X')*(X*a)) (which seems more correct to me).
I try to compare them together with the norm function for some simple examples but they seem to be the same. But if I apply it to my program which involve time-dependent matrix they are different (shown in plotting).

Related

inverse() vs pinv() which one good for linear system

I have a linear system A*x=b. Here x is the unknown value, so I have to solve for x. A is a sparse matrix whose diagonal and off-diagonal elements have some non-zero values. The other elements is zero or close to zero.
Using MATLAB, I have two options
x = inv(A) * b
x = A \ b
But both give me NaNs in the result. I know this will happen because A is a sparse matrix. So, I tried pinv() which is pseudo-inverse. This time I got some results.
Is it OK to use pseudo-inverse in a situation like this, where inverse has failed to produce a result? What type of result does pseudo inverse produce? Is it reliable or it is a form of error?

MATLAB function graphallshortestpaths not returning symmetric matrix

I am using the MATLAB function graphallshortestpaths to compute shortest paths between vertices of an undirected network. The undirected network is given as a weighted edge list file, which you can find here.
This is the MATLAB code that I use to compute the shortest paths:
A=load('genome_edge_list');
%Extract the edges
E=[A(:,1);A(:,2)];
%Extract the vertices
V=unique(E);
%N is the number of vertices
N=length(V);
%Take the inverse of the weights
A(:,3)=1./A(:,3);
%Create a sparse weighted adjacency matrix
B=sparse(A(:,1),A(:,2),A(:,3),N,N);
%Make B symmetric
B=sparse(full(B)+full(B)');
%Compute shortest paths
D=graphallshortestpaths(B,'directed',false);
Now, the matrix D that MATLAB gives as output is not symmetric. However, since the input to graphallshortestpaths is a symmetric matrix in sparse format, the output ought to be a symmetric matrix. So what am I doing wrong?
The only related question that I could find on mathworks is this question, however in that question the OP clearly is not giving a symmetric matrix as input, which explains why the matrix returned by MATLAB is not symmetric.
EDIT:
To see how far off D and D' are, I computed the following:
E=D';
C=D==E;
find(C==0)
this returns the following linear indices:
33133
543038
1363077
1398421
1398786
1399373
but the values of D and E at those indices are the same, e.g. D(33133)= 0.1024=E(33133). Now, if I take the difference of the two matrices, then I find that the difference at those indices is -1.0000e-05. It therefore seems to be a rounding error, as #beaker points out. However, as I write in my comment below, I don't understand how this can occur, as graphsallshortestpaths computes the distance between node i and j only once, so the values of D(i,j) and D(i,j) should be the result of the same computation.
Couple or remarks:
As #beaker mentioned in the comment, it could well be a numerical issue. I would be particularly weary of the line where you take the inverse and do A(:,3)=1./A(:,3);. Try to output some debug values and see if this inverse does what you intended.
On the line where you make B symmetric: are you sure you want to do full(b)' and not full(b).'? The first one takes the hermitian, the second the transpose!
Also on the same line where you make B symmetric: perhaps you are missing a 0.5 factor in there? So instead of B=sparse(full(B)+full(B)'); something like B=sparse((full(B)+full(B).').*0.5); (see this answer).
I also think you unintentionally wrote H instead of E on the second line, right?

Very small numerical issues with hessian symmetry and sparse command

I am using IPOPT in MATLAB to run an optimization and I am running into some issues where it says:
Hessian must be an n x n sparse, symmetric and lower triangular matrix
with row indices in increasing order, where n is the number of variables.
After looking at my Hessian Matrix, I found that the non-symmetric elements it is complaining about are very close, here is an example:
H(k,j) = 2.956404205984938
H(j,k) = 2.956404205984939
Obviously these elements are close enough and there are some numerical round-off issues or something of the like. Also, when I call MATLABs issymmetric function with H as an input, I get false. Is there a way to forget about these very small differences in symmetry?
A little more info:
I am using an optimized matlabFunction to actually calculate the entire hessian (H), then I did some postprocessing before passing it to IPOPT:
H = tril(H);
H = sparse(H);
The tril command generates a lower triangular matrix, so these numeral differences should not come into play. So, the issue might be that it is complaining that the sparse command passes back increasing column indices and not increasing row indices. Is there a way to change this so that it passes back the sparse matrix in increasing row indices?
If H is very close to symmetric but not quite, and you need to force it to be exactly symmetric, a standard way to do this would be to say H = (H+H')./2.

How to calculate the squared inverse of a matrix in Matlab

I have to calculate:
gamma=(I-K*A^-1)*OLS;
where I is the identity matrix, K and A are diagonal matrices of the same size, and OLS is the ordinary least squares estimate of the parameters.
I do this in Matlab using:
gamma=(I-A\K)*OLS;
However I then have to calculate:
gamma2=(I-K^2*A-2)*OLS;
I calculate this in Matlab using:
gamma2=(I+A\K)*(I-A\K)*OLS;
Is this correct?
Also I just want to calculate the variance of the OLS parameters:
The formula is simple enough:
Var(B)=sigma^2*(Delta)^-1;
Where sigma is a constant and Delta is a diagonal matrix containing the eigenvalues.
I tried doing this by:
Var_B=Delta\sigma^2;
But it comes back saying matrix dimensions must agree?
Please can you tell me how to calculate Var(B) in Matlab, as well as confirming whether or not my other calculations are correct.
In general, matrix multiplication does not commute, which makes A^2 - B^2 not equal to (A+B)*(A-B). However your case is special, because you have an identity matrix in the equation. So your method for finding gamma2 is valid.
'Var_B=Delta\sigma^2' is not a valid mldivide expression. See the documentation. Try Var_B=sigma^2*inv(Delta). The function inv returns a matrix inverse. Although this function can also be applied in your expression to find gamma or gamma2, the use of the operator \ is more recommended for better accuracy and faster computation.

scipy generalized eigenproblem with positive semidefinite

Hi, guys!!!
I want to compute generalized eigendecomposition of the form:
Lf = lambda Af
by using scipy.sparse.linalg.eigs function, but get this error:
/usr/local/lib/python2.7/dist-packages/scipy/linalg/decomp_lu.py:61: RuntimeWarning: Diagonal number 65 is exactly zero. Singular matrix.
RuntimeWarning)
** On entry to DLASCL parameter number 4 had an illegal value
I am passing three arguments, a diagonal matrix, a positive semi-definite (PSD) matrix and numeric value K (first K eigenvalues). Matlab's eigs function performs well using the same input parameters, but in SciPy as I have understood, in order to compute with PSD I need to specify sigma parameter as well.
So, my question is: is there a way to avoid setting sigma parameter, as it is in MatLab, or if not, how to pick up sigma value?
Looking forward to getting advices or hints...
Thank you in advance!
The error appears to mean that in your generalized eigenproblem
L x = lambda A x
the matrix A is not positive definite (check the eigs docstring -- in your case the matrix is probably singular). This is a requirement for ARPACK mode 2. However, you can try specifying sigma=0 to switch to ARPACK mode 3 (but note that the meaning of the which parameter is inverted in this case!).
Now, I'm not sure what Matlab does, but a possibility is that it's calculating the pseudoinverse rather than the inverse of A. To emulate this, do
from scipy.sparse.linalg import LinearOperator
from scipy.linalg import lstsq
Ainv = LinearOperator(matvec=lambda x: lstsq(A, x)[0], shape=A.shape)
w, v = eigs(L, M=A, Minv=Ainv)
Check the results --- I don't know what will happen in this case.
Alternatively, you may try to specify a nonzero sigma. What you should select depends on the matrices involved. It affects the eigenvalues that are picked --- for instance with which='LM' are those for which lambda' = 1/(lambda - sigma) is large. Otherwise, it can probably be chosen arbitrarily, of course it's probably better for the Krylov progress if the transformed eigenvalues lambda' which you are interested in become well separated from the other eigenvalues.