Message Boards Message Boards

GROUPS:

Generate points with density following Gaussian distribution

Posted 1 month ago
577 Views
|
22 Replies
|
0 Total Likes
|

Hello,

I want to generate a collection of 1000 points (x, y), at (near) origin with their density following a Gaussian distribution on both X and Y axes. The full width half maxima (FWHM) of this Gaussian distribution should be a variable, say 5 to start with.

Will appreciate any help. Thanks

POSTED BY: S G
Answer
22 Replies

I am not sure whether this does the job

xx = RandomReal[NormalDistribution[0, 1], 500];
yy = RandomReal[NormalDistribution[0, 1], 500];
pp = Transpose[{xx, yy}];
ListPlot[pp, AspectRatio -> Automatic]

but you could have a look at it.

or something like this?

xx = RandomReal[NormalDistribution[0, 1], 500];
yy = RandomReal[NormalDistribution[0, 1], 500];
pp1 = Transpose[{xx, yy}];
xx = RandomReal[NormalDistribution[0, .1], 500];
yy = RandomReal[NormalDistribution[0, .1], 500];
pp2 = Transpose[{xx, yy}];
ListPlot[{pp1, pp2}, AspectRatio -> Automatic, 
 PlotRange -> {{-1, 1}, {-1, 1}}, PlotStyle -> {Red, Blue}]
Posted 1 month ago

Thanks.

In NormalDistribution[0, 1], first variable is the position of the Gaussian peak, defined as mean in Mathematica. However, I am not able to figure out the fact that how the second variable, which is defined as "deviation" in Mathematica, is related to FWHM ? Can you please elaborate that how the number "1" chosen by you is equivalent to the FWHM of 5.

POSTED BY: S G
Answer

What is FWHM? The 2nd variable is the standard deviation of the NomalDistribution.

As I said, I am by no means sure whether my approach is correct.

Posted 1 month ago

FWHM - Is a measure of the width of the peak. abbreviation of Full width at half maxima. Trying to figure out how the "standard deviation" is related to FWHM !!

POSTED BY: S G
Answer
Posted 1 month ago

Got it. FWHM = 2.35 * standard deviation. Thanks for you help.

POSTED BY: S G
Answer
Posted 1 month ago

So that you know while using FWHM as a parameter to characterize the Normal (i.e., Gaussian) distribution is legitimate, it is not a very common way of doing so (but maybe the standard in some specialized fields). Typically one uses the mean and variance or mean and standard deviation. Mathematica uses the mean and standard deviation.

For a Normal distribution (and only for a Normal distribution) the standard deviation is given by

$$\sigma=FWHM/(2\sqrt{2 \log2})\approx FWHM/2.35482$$

Posted 1 month ago

True !! I have to relate a calculation with experimental data, where peak is characterized by FWHM. Thanks by the way.

POSTED BY: S G
Answer

Here is another piece of code "to analyse" the density of the points obtained, using the assumption that the density is something like Exp[ - a x^2 ]

xx = RandomReal[NormalDistribution[0, .3], 500];
yy = RandomReal[NormalDistribution[0, .3], 500];
pp2 = Transpose[{xx, yy}];
ListPlot[pp2, AspectRatio -> Automatic, 
 PlotRange -> {{-1, 1}, {-1, 1}}, PlotStyle -> {Red, Blue}]
total = Length [pp2]; pts = 
 Table[{d + .05, 
   N@Length[Select[pp2, d < Sqrt[#.#] < d + .1 &]]/total}, {d, 0, 
   2, .1}]
fit = NonlinearModelFit[pts, 
   2 Pi x  b  Exp[-a x^2], {{b, .21}, {a, 6.2},}, x];
ffit = Normal[fit]
Plot[ffit, {x, 0, 2}, PlotRange -> {0, .3}, Epilog -> Point /@ pts]

Run this code several times and look at the plots

Posted 1 month ago

Thanks. I have not understood this code yet, but why a peak at 0.25 when the mean value for normal distribution is zero ??

POSTED BY: S G
Answer

but why a peak at 0.25

Because if your point-denisity (number of points per unit area) is rho = Exp[ -a x^2 ] (something like a normal distribution (?), at least half ), x being the distance from the origin, then the number of points in the ring of area 2 Pi x deltax is 2 Pi x deltax rho, and this is zero at the origin and has a peak somewhere.

I counted the number of points in such areas ( in the Table, using Select) and plotted it against distance. The result could be well fitted by a rho like that given above.

Posted 1 month ago

I think what you've given the OP is the distribution of the "distance" from the origin and I don't think that's what's being asked. 0.25 is approximately where the PDF of the distance has a maximum (although I'd say it's closer to 0.3 for the example you gave):

dist = TransformedDistribution[Sqrt[x^2 + y^2], {x \[Distributed] NormalDistribution[0, 0.3], 
    y \[Distributed] NormalDistribution[0, 0.3]}];
d = RandomVariate[dist, 1000000];
skd = SmoothKernelDistribution[d];
Plot[PDF[skd, x], {x, 0, 1.2}]

Distribution of distance

FindMaximum[PDF[skd, x], {{x, 0.25}}]
(* {2.01764, {x -> 0.301453}} *)

?? what is OP? and anyway, you have your points "normally" distributed around { 0, 0 }: and what do think is "being asked for"?

Posted 1 month ago

OP is for "original poster". In other words, a reference to the person who asked the question or to the question being asked.

Your initial response did contain the what was asked for: how to generate the sample points. Your other response described the distribution of the distance of those points from (0,0) and I don't see that the OP asked for that.

@Jim Baldwin

Indeed, he didn't ask for that. But I asked myself, whether pp2 given above follows a normal distribution. Not being a specialist in statistics I had no other idea as to check whether the densitiy follows an Exp[ - a x^2 ] - law, which I think it does.

Posted 1 month ago

What about using histogram !!

![data = RandomVariate\[NormalDistribution\[0, 5\], 10000\];
Show\[
 Histogram\[data, 20, "ProbabilityDensity"\],
 Plot\[PDF\[NormalDistribution\[0, 5\], x\], {x, -15, 15}, 
  PlotStyle -> Thick\]\]][1]
POSTED BY: S G
Answer
Posted 1 month ago

Thanks, I get your point mathematically. However, if we are using a function under RandomReal which has a peak at zero, why don't we have the highest density at origin. Can we modify the NormalDistribution in such a way (or use any other function) that can give highest density of points at origin. Because, that is what I need in the end.

POSTED BY: S G
Answer
Posted 1 month ago

You do get a peak of the density of points at the origin. The density of points for a bivariate normal distribution with mean vector (0,0) does have a peak at (0,0).

Hans introduced the distribution of the "distance" from the origin which doesn't seem to have anything to do with your question. The peak around 0.3 mentioned by Hans is the peak of the probability density function of the "distance from (0,0)" which is a univariate distribution. And that, again, is an interesting distribution but does not appear to me to something that you were asking about.

So no need to modify the Normal distribution. You can get a set of data points with the origin at (0,0) (and therefore the highest density of sample points near the origin) from two independent normal random variables in several ways. Here are 2:

sd = 5/(2 Sqrt[2 Log[2]]);
data = RandomVariate[NormalDistribution[0, sd], {500, 2}];
data = RandomVariate[BinormalDistribution[{0, 0}, {sd, sd}, 0], 500];
Posted 1 month ago

Thanks a lot. But the example you provided above, was giving a peak at 0.3 for normal distribution NormalDistribution[0, 0.3]. How that is possible. And what is the difference RandomVariate makes.

POSTED BY: S G
Answer
Posted 1 month ago

It certainly does not. Why are you saying that? If you are referring to the figure

Distance distribution

that is the distribution of the distance sample points are from the origin. What

sd = 5/(2 Sqrt[2 Log[2]]);
data = RandomVariate[NormalDistribution[0, sd], {500, 2}];

gives you is a set of points centered on the origin:

Bivariate distribution of points

The function to obtain random samples from a specified distribution is RandomVariate. Hans used RandomReal. But while that appears to work, the documentation does not mention that RandomReal should work that way.

As Jim has pointed out, the density of points is Exp[ -a x^2] , and this has a peak at zero. But to claculate the number of points you must use an area element, 2 Pi x dx , and this is zero at the origin. So area times density has a maximum somewhere out.

Posted 1 month ago
rho = 0.8;  (* Correlation coefficient *)
FWHM = 5;  (* Your desired value for FWHM *)
sigma = FWHM/(2 Sqrt[2 Log[2]]);  (* Convert to equivalent standard deviation for a Normal *)
n = 1000;  (* Sample size *)
xy = RandomVariate[BinormalDistribution[{0, 0}, {sigma, sigma}, rho], n];
ListPlot[xy]

Plot of random sample from a bivariate normal

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract