# Two ways of obtaining differential distribution using Mathematica do not match

Consider a set of data

dataset = {{x1,y1},{x2,y2},...}


It contains ~200000 rows. I would like to obtain the pdf for x. I can do this, for example, using two ways:

1) To construct the bins:

 binsx1 =
Partition[Sort[#], Round[Length[#]/#2]] &[
dataset[[All, 1]], BinsNumber];
xDistrTable =
Table[{(binsx1[[i]][[1]] + binsx1[[i]][[BinsHeight]])/
2, BinsHeight/(
binsx1[[i]][[BinsHeight]] - binsx1[[i]][[1]])}, {i,
1, BinsNumber, 1}];


2) Using double distribution function obtained using built-in Mathematica tools:

xyDistributionTemp =
SmoothKernelDistribution[dataset,
MaxExtraBandwidths -> 0];
xyDoubleDistribution[x_,y_] =
PDF[xyDistributionTemp, {x,y}]
xDistrTable1 = Table[{x,NIntegrate[xyDoubleDistribution[x,y],{y,ymin,ymax}]},{x,xmin,xmax,Deltax}]


Here, BinsHeight, BinsNumber, xmin, xmax, Deltax, ymin, ymax are some numbers.

The problem is that the distributions obtained by xDistrTable and xDistrTable1 do not match each other (see a picture below). My first guess is that it is not enough data to obtain the double differential distribution, while it is enough statistics to get the single distribution function. However (my second guess), the problem also may be in my way to define the double distribution function.

P.S. Unfortunately I can not provide the dataset as it is too large.

• Use the "Bounded" option in SmoothKernelDistribution. Also, if you're interested in the pdf for a single variable, why would you want take the trouble to find the joint distribution and then integrate out the other variable? Just use the single variable. – JimB May 5 at 16:23
• @JimB : thank you! Actually I need the double distribution. However, I use the single distribution as a cross-check. – John Taylor May 5 at 18:22
• @JimB : I have not found examples of using the "Bounded" option in Mathematica. Could you please recommend how to use it? – John Taylor May 5 at 20:16
• Look under Scope/Estimation with fixed domain (at least for Mathematica version 12). – JimB May 5 at 21:45

x = RandomVariate[BetaDistribution[0.5, 0.5], 200000];