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?