Search code examples
matlabmultidimensional-arraybayesian

MATLAB: How do I plot probability distributions in 3 dimensions? (involving Bayes rule)


To give you some context, I am trying to find out the optimal parameters for a research problem using Bayes rule, and my question here has to do with generating the prior probability distributions for these parameters.

Suppose that my two parameters are A and B. I know that the prior distribution for A is going to look like a Gamma distribution, and my distribution for B is going to look like a normalized Gaussian distribution. The goal is to plot these distributions on the x and y axes, and then plot the joint probability P(A,B) = P(A)*P(B) in the z-axis. If I can do that, then I can identify what values of A and B are most probable for my problem.

So I type in MATLAB

A = linspace(-50,50,1000);
B = A;
gamma = gampdf(A,5,5);
norm = normpdf(B,0,5);
[A B] = meshgrid(A,B);
Z = gamma'*norm;
surf(A,B,Z)

When I do that, I get this:

enter image description here

Rotating this perhaps gives me something I'm looking for, but since it's all black, I can't really tell for sure.

Is there a way I can make it more easy to distinguish and have the mesh work? (I'm guessing the mesh didn't work because my Z is not a function of A and B, but rather of gamma and norm).

Any help would be much appreciated, since I'm completely open to alternative ways of doing this.


Solution

  • The grid lines are so dense (you have 1000 lines in each dimension) that it just appears black. Use set to turn off the grid lines:

    h = surf(A,B,Z);
    set(h,'linestyle','none');