mvnpdf behavior weird at infinity - matlab

The mvnpdf function in Matlab takes the default mean and std as 0 and I (Identity matrix).
When I type this:
mvnpdf([0,0])
ans = 0.1592
mvnpdf([0,0], [0,0], [1,0;0,1])
ans = 0.1592
mvnpdf([1,1])
ans = 0.0585
mvnpdf([1,1], [0,0], [1,0;0,1])
ans = 0.0585
So the function works when we specify the Mean and Correlation Matrix outselves, however, when I do this, it no longer works
mvnpdf([Inf,Inf])
ans = 0
mvnpdf([Inf,Inf], [0,0], [1,0;0,1])
ans = NaN
Why is this happening? How can I fix this?
These bivariate normal pdfs are a small part of a larger expression which I am optimizing over "rho", so I need an expression like
mvnpdf([Gamma(i+1) Tau(j+1)],[0 0],[1,rho;rho 1])
to work for some unkown $\rho$ and in additional inputs $\Gamma$ and $\Tau$ which can possible be $\infty$ or $-\infty$. Ok LaTeX doesnt seem to work.
Anyway, I would like to be able to provide a correlation Matrix with an unknown parameter Rho and additional inputs Gamma's and Tau's which may run over a large range and could possibly be negative or positive infinity.

Related

Wrong std and mean values then expected

I have a matrix X with 3 columns. For the porous of the question X=randn(5,3).
I want to normalize the columns of X S.T. each column will have a 0 mean and a 1 std. I was using the following code:
X=(X-mean(X))./std(X);
I am getting an std of 1. My mean, however, is a very small value close to 0 but not essential 0. I tried playing a bit with the numbers to find an explanation:
X=1:15;
X=reshape(X,[5 3]);
mean(X-mean(X));
Which gives me 0 value for each column.
X=1:15;
X=reshape(X,[5 3]);
mean((X-mean(X))./std(X));
Which does not. But 0/anything is still 0. what am I missing?
Why am I not getting 0 values?
Are the values I am getting good enough for a pre-clustering algorithm normalization?
Here is a version that does what I think you're trying to do... you need to replicate the matrix because X-mean(X) isn't valid (if you're using the standard implementation)-- you can't subtract a 1x3 from a 5x3.
r = 5; c = 3;
X=randn(r,c);
Xm=repmat(mean(X),r,1);
Xstd = repmat(std(X),r,1);
Xn = (X-Xm)./Xstd;
mean(Xn)
std(Xn)
For me this prints out
ans =
1.0e-16 *
-0.6661 0 0.4441
ans =
1.0000 1.0000 1.0000
Which seems like exactly what you're looking for... note the 1e-16 multiplier on the mean values... this is essentially 0, with some floating point error.

compute weights by Generalized Hebbian Algorithm in matlab

I have a task to do some calculations in matlab .. I use the Generalized Hebbian Algorithm to compute some weights , here is the functions of Hebbian Algorithm , slice 15
http://www.eit.lth.se/fileadmin/eit/courses/eitn55/Downloads/ICA_Ch6.pdf
here is my code
alfa=0.5;
e=randn(3,5000);
A=[1 0 0;-0.5 0.5 0;0.3 0.1 0.1];
x=A*e;
W=rand(3);
nn=size(x);
for n=1:nn
y=W*x(:,n);
k=tril(y*y')*W;
W(:,n+1)= alfa*(y*x(:,n)'-k);
end
In my task I know that x=A*e;
but I do not know if I am iterating in correct way or not?
is my for loop doing correct?
and are those equations below correct?
y=W*x(:,n);
k=tril(y*y')*W;
W(:,n+1)= alfa*(y*x(:,n)'-k);
W(:,n+1) should print out a 3*3 matrix (that what I understood)...
Matlab says when I run this code : Error using *
Inner matrix dimensions must agree.
thanks
If you check size of each matrix, you will find out that the order is incorrect:
size(x)
ans =
3 5000
size(W)
ans =
3 3
so you should multiply them as
for n=1:nn
y=W*x;
end
However this part does not make sense either,
k=tril(y'*y)*W;
because tril(y'*y) is a matrix size 5000x5000 and W is 3x3. So I guess you should change it to:
k=tril(y*y')*W;
Then alfa*(y*x'-k); would be a 3x3 matrix.

Entropy of pure split caculated to NaN

I have written a function to calculate entropy of a vector where each element represents number of elements of a class.
function x = Entropy(a)
t = sum(a);
t = repmat(t, [1, size(a, 2)]);
x = sum(-a./t .* log2(a./t));
end
e.g: a = [4 0], then entropy = -(0/4)*log2(0/4) - (4/4)*log2(4/4)
But for above function, the entropy is NaN when the split is pure because of log2(0), as in above example. The entropy of pure split should be zero.
How should I solve the problem with least effect on performance as data is very large? Thanks
I would suggest you create your own log2 function
function res=mylog2(a)
res=log2(a);
res(isinf(res))=0;
end
This function, while breaking the log2 behaviour, can be used in your specific example because you are multiplying the result with the inside of the log, thus making it zero. It is not "mathematically correct", but I believe that's what you are looking for.

matlab constant anonymous function returns only one value instead of an array

I've been searching the net for a couple of mornings and found nothing, hope you can help.
I have an anonymous function like this
f = #(x,y) [sin(2*pi*x).*cos(2*pi*y), cos(2*pi*x).*sin(2*pi*y)];
that needs to be evaluated on an array of points, something like
x = 0:0.1:1;
y = 0:0.1:1;
w = f(x',y');
Now, in the above example everything works fine, the result w is a 11x2 matrix with in each row the correct value f(x(i), y(i)).
The problem comes when I change my function to have constant values:
f = #(x,y) [0, 1];
Now, even with array inputs like before, I only get out a 1x2 array like w = [0,1];
while of course I want to have the same structure as before, i.e. a 11x2 matrix.
I have no idea why Matlab is doing this...
EDIT 1
Sorry, I thought it was pretty clear from what I wrote in the original question, but I see some of you asking, so here is a clarification: what I want is to have again a 11x2 matrix, since I am feeding the function with arrays with 11 elements.
This means I expect to have an output exactly like in the first example, just with changed values in it: a matrix with 11 rows and 2 columns, with only values 0 in the first column and only values 1 in the second, since for all x(i) and y(i) the answer should be the vector [0,1].
It means I expect to have:
w = [0 1
0 1
0 1
...
0 1]
seems pretty natural to me...
You are defining a function f = #(x,y) [0, 1]; which has the input parameters x,y and the output [0,1]. What else do you expect to happen?
Update:
This should match your description:
g=#(x,y)[zeros(size(x)),ones(size(y))]
g(x',y')
Defining an anonymous function f as
f = #(x,y) [0,1];
naturally returns [0,1] for any inputs x and y regardless of the length of those vectors.
This behavior puzzled me also until I realized that I expected f(a,b) to loop over a and b as if I had written
for inc = 1:length(a)
f(a(inc), b(inc))
end
However, f(a,b) does not loop over the length of its inputs, so it merely returns [0,1] regardless of the length of a and b.
The desired behavior can be obtained by defining f as
g=#(x,y)[zeros(size(x)),ones(size(y))]
as Daniel stated in his answer.

Different results using == and find in MATLAB

I have created a sparse matrix using MEX and also created a sparse matrix using MATLAB. To fill in the values of the matrix i have used same formula.
Now to check if the both the matrices are equal I used result=(A==B). result returns 1 for all indices, which implies that all the matrix elements are equal.
But if I do find(A-B) it returns some indices, which indicates that at these indices the values are non-zero. How is this possible?
Note: When i compare the value at these indices it shows equal !
I'm guessing you have values of infinity cropping up in your matrices at the same points. For example:
>> A = Inf;
>> B = Inf;
>> A == B
ans =
1 %# They are treated as equal...
>> A-B
ans =
NaN %# ...but their difference actually results in NaN...
>> find(A-B)
ans =
1 %# ...which is treated as a non-zero value.
The discrepancy here results from the fact that certain operations involving infinity result in NaN values. You can check to see if you have any infinities in A and B by using the function ISINF like so:
any(isinf(A(:)))
any(isinf(B(:)))
And if you get a value of 1 (i.e. true), then the presence of infinities is likely the source of your discrepancy.