Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Topic: Compute a bivariate probability mass function on a predefined grid without loops
Replies: 10   Last Post: Apr 22, 2012 2:43 PM

 Search Thread: Advanced Search

 Messages: [ Previous | Next ]
 Massimo Posts: 23 From: Boston Registered: 7/24/07
Compute a bivariate probability mass function on a predefined grid without loops
Posted: Apr 20, 2012 12:55 PM
 Plain Text Reply

Hi,
I have a problem very similar to the one I posted some time ago here:

http://www.mathworks.it/matlabcentral/newsreader/view_thread/311682#849815

the main difference is that now I am dealing with a 2D grid and therefore the density needs to be distributed in 4 points to obtain a discretized pmf (you can think of it as a 2D histogram).

More precisely I have a predefined equally-spaced 2D grid of values over two vectors k,m , say:
k=linspace(k_min,k_max,N);
m=linspace(m_min,m_max,N);
[K, M]=ndgrid(k,m);

I also have 2 grids of values Sk and Sm of the same size (N*N) which are functions of k and m and whose values are suche that:
max(Sk(:))<k_max;
max(Sm(:))<m_max;
min(Sk(:))>k_min;
min(Sm(:))>m_min;

What I want to obtain is a joint probability mass function f(k,m) such that the joint density associated to each point (Sk(i,j),Sm(i,j)) in the domain spanned by k and m, is distributed to the grid points of the rectangle that contains (Sk(i,j),Sm(i,j)) according to its relative distance from such values.
In other words, suppose (Sk(i,j),Sm(i,j)) is contained in the rectangle: k(l),k(l+1),m(r),m(r+1),
then
define first:
a=Sk(i,j)-k(l);
b=Sm(i,j)-m(r);
c=k(l+1)-Sk(i,j);
d=m(r+1)-Sm(i,j);
and:
A=sqrt(a^2+b^2);
B=sqrt(c^2+b^2);
C=sqrt(c^2+d^2);
D=sqrt(a^2+d^2);
hence:

f(k(l),m(r))=A/(A+B+C+D);
f(k(l+1),m(r))=B/(A+B+C+D);
f(k(l+1),m(r+1))=C/(A+B+C+D);
f(k(l),m(r+1))=D/(A+B+C+D);

I am clearly able to compute f(k,m) using for loops, but since N is pretty large (between 5000 and 10000) and since this computation is inside an fsolve I am looking for a smart way to vectorize it.
I hope to have described the problem accurately, if you have questions please don't hesitate to ask them.
Thank you,
Massimo

Date Subject Author
4/20/12 Massimo
4/20/12 Roger Stafford
4/21/12 Massimo
4/21/12 Bruno Luong
4/21/12 Roger Stafford
4/21/12 Bruno Luong
4/21/12 Roger Stafford
4/22/12 Bruno Luong
4/22/12 Bruno Luong
4/22/12 Roger Stafford
4/22/12 Roger Stafford

© Drexel University 1994-2013. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.