# How do I compute the density of this data set that is made up of two different 3D-distributions?

A sequel to this question.

I have a dataset where:

• $\frac{4}{5}$ of the points are drawn from: $(x, y) \sim \mathcal{U}_{2}(0,30)$, $(z) \sim \mathcal{U}_{1}(14.5, 15.5)$.
• $\frac{1}{5}$ of the points are drawn from: $(x, y, z) \sim \mathcal{U}_{3}(0,30)$

Where $\mathcal{U}_{d}(x,y)$ is to be interpreted as a $d$ dimensional set of points which are in each dimension drawn from the range between $x$ and $y$.

## The implementation

I have implemented this in matlab like this:

General Init:

dim = 3;

uniP = 1/5;
wallP = 4/5;

uniformN = ceil(N * uniP);
wallN  = ceil(N * wallP);


First distribution (wall):

% parameters
lowerWall = [0,0,14.5];
upperWall = [30,30,15.5];

% values
[wallD] = blockUniformDist(lowerWall, upperWall, wallN, dim);


Second distribution (noise):

% parameters
lower = 0;
upper = 30;

% values
[uniformD] = uniformDist(lower, upper, uniformN, dim);


Combine data and compute the density:

% Data
data = [wallD; uniformD]

% Density
uniDensity = 1 / ((upper - lower) ^ dim);

wallDensity = 1;
for i=1 : dim
wallDensity = wallDensity/(upperWall(i)-lowerWall(i));
end

wallSpace = (data(:,3) < upperWall(3)) & (data(:,3) > lowerWall(3));

trueValues = wallP * wallDensity .* wallSpace  + ...
uniP  .* (ones(N, 1) * uniDensity);


The wallSpace is a boolean array that indicates for each observation in data whether or not it lies within the wall. Since the range of the wall is equal to the range of the wall is equal to the range of the uniform data in dimension one and two I only consider the third dimension.

If a point with index i isn't part of the wall its density is uniDensity, since wallSpace(i) is zero for such walls trueValues(i) equals uniDensity.

A point with index j whose z is between 14.5 and 15.5 is in the wall, its density should thus be $\frac{4}{5} \cdot$ wallDensity + $\frac{1}{5} \cdot$ uniDensity. Since wallSpace[i] is one for these points, this is the density that is placed in trueValues[j].

blockUniformDist(lowerWall, upperWall, wallN, dim) is defined as:

function [ data ] = blockUniformDist( lower, upper, N, dim )
%BLOCKUNIFORMDIST Samples N values from a uniform distribution with
% dim dimensions.
% INPUT
%   - lower:   The lowest value allowed (per dimension)
%   - upper:   The highest value allowed (per dimension)
%   - N:    Number of samples to be taken
%   - dim:  Dimension of the distribution
% OUTPUT
%   - data: A vector of samples form the distribution

% values
data = rand(N, dim);
for i= 1 : dim
data(:,i) = lower(i) + data(:,i).*(upper(i) - lower(i));
end
end


And uniformDist as:

function [ data ] = uniformDist( lower, upper, N, dim )
%UNIFORMDIST Samples N values from a normal distribution with mean mu
%and standard deviation sd in dimension dim.
% INPUT
%   - lower:   The lowest value allowed
%   - upper:   The highest value allowed
%   - N:    Number of samples to be taken
%   - dim:  Dimension of the distribution
% OUTPUT
%   - data: A vector of samples form the distribution

% values
data = lower + rand(N, dim) .* (upper - lower);
end


## The result

The result of this is that each observation has one of two densities either $8.962962963e-04$ or $7.407407407e-06$. Plotting the data set with the density dictating the colour (points with density $7.407407407e-06$ in red and points with density $8.962962963e-04$ in blue) results in:

## The actual question

Shouldn't the points in the denser area of the plot all have the same density, and thus all the same colour?

The plot looks perfectly okay to me ... but it's not clear what you think it ought to look like.

The reason why the slice through the middle looks more dense ... is because the pdf actually has greater density there.

You might find it easier if we just look at z (since x and y are the same for both parts).

What you have is two components, one which is on $(0,30)$ and one which is on $(14.5,15.5)$. The one on the narrower interval has higher density.

• I understand that the middle of the density should be denser, what I don't understand is why the points inside this wall in the middle have different densities. Maybe the meaning of the colours in the plot is not clear. I'll add some more explanation and try to improve my question. – Laura Aug 10 '14 at 8:19
• How did you compute the densities you used to color the points? – Glen_b Aug 10 '14 at 9:22
• With: trueValues = wallP * wallDensity .* wallSpace + uniP .* (ones(N, 1) * uniDensity);, see the first code block in the question. – Laura Aug 10 '14 at 14:49
• I have no clear idea what that is doing. Can you explain what you are attempting to do with that code? – Glen_b Aug 10 '14 at 14:58
• I added an explanation in the question, hope that helps. – Laura Aug 10 '14 at 15:05