# How do I compute the density of this distribution

Ferdosi et al define six artificial datasets to test density estimation methods. Part of the fourth dataset is defined as:

$$Uniform(x,y) = [0,100], Gaussian(z) = [M = 50, var = 5]$$

Where $M$ is defined as the mean of the distribution and $var$ as the variance. To test my own density estimator using this dataset I'd like to do two things:

1. Sample $N$ items from this dataset.
2. Compute the 'true' density of each of these samples drawn.

## Sampling

For step (1) I tried the following in Matlab:

1. Draw $N$ samples from a uniform distribution (this gives a $N \times 1$ matrix).

uniform = makedist('uniform', 'lower', 0, 'upper', 100);
x = uniform.random(N, 1);

2. Draw $N$ samples from a uniform distribution (this gives a $N \times 1$ matrix).

y = uniform.random(N,1);

3. Draw $N$ samples from a Gaussian distribution (resulting in a $N \times 1$ matrix).

z = mvnrnd(M,var,N);

4. Combine these two matrices to a $N \times 3$ matrix.

data = [x, y, z];


This seems to give the correct results, that is to say the resulting plot is the same as the plot shown in the article.

Is this the correct way to sample this distribution? If not how should I go about it? Is there a difference between sampling three different vectors or sampling the $x$ and $y$ vector from a bivariate distribution?

## Computing the density

This wikipedia artikel defines ''the joint probability density function $fX,Y(x, y)$ for continuous random variables as:

$$f_{X,Y}(x,y) = f_{Y\mid X}(y|x)f_X(x) = f_{X\mid Y}(x\mid y)f_Y(y)\$$

where fY|X(y|x) and fX|Y(x|y) give the conditional distributions of Y given X = x and of X given Y = y respectively, and fX(x) and fY(y) give the marginal distributions for X and Y respectively.'' But I don't know how to use it, if it is the correct way, to determine the density of the defined distribution.

• I agree with your interpretation of what the definition means. If I understand correctly, you seem to be using a multivariate normal random number generator for the normal part; you just need $N$ observations from a univariate normal - normrnd should be sufficient. – Glen_b Jun 14 '14 at 8:38
• Yup, though if/when switching to normrnd, OP needs to note that normrnd takes standard deviation as input while mvnrnd takes variance. – Juho Kokkala Jun 14 '14 at 8:44

## 1 Answer

The notation for defining the distributions is a bit strange (at least to me), but it seems you have interpreted it correctly. I would write something like \begin{equation} X,Y \sim \mathrm{Uniform}(0,100),~Z\sim \mathrm{N}(50,5), X,Y,Z \textrm{ mutually independent}. \end{equation} Based on a quick glance, I did not find the independence of $(X,Y,Z)$ mentioned in the article explicitly, but I suppose this is intended as no dependence is specified either. Your sampling code looks correct, provided that the assumption about independence of $(X,Y,Z)$ is correct (sampling from the marginal distributions of the variables independently and combining the samples is equivalent to sampling from a bivariate (or trivariate) distribution where the variables are independent).

To obtain the density, first note that the formula in your question generalizes to 3 random variables as follows: \begin{equation} f_{X,Y,Z}(x,y,z) = f_{Z \mid X,Y}(z\mid x,y)\,f_{Y \mid X}(y \mid x)\,f_X(x). \end{equation} But when $X,Y,Z$ are independent, the conditions do not matter, i.e., the conditional distributions are equal to the marginal distributions: \begin{equation} = f_X(x)f_Y(y)f_Z(z). \end{equation} That is, in the case of independence, the joint density is obtained as a product of the marginal densities. So, to get the density in this case, you multiply two uniform probability density functions and one Gaussian probability density function.

Note that this gives you the probability density. To obtain the physical density. you need to multiply by the number of points (as is described in the linked article).

• Since the covariance matrices of the other datasets, e.g. dataset 1, have only values on the diagonal they are independent so I guess it is safe to assume that that is the case for all artificial datasets mentioned. – Laura Jun 15 '14 at 6:49