I am looking for any script (preferably Python) to calculate the two dimensional normal distribution function of series of three dimensional data. If one does not exist, I would appreciate any code, or pseudocode, someone could provide.
The input will be a list of triples like so
[[x1, y1, z1], [x2, y2, z2], [x3, y3, z3],..., [xn, yn, zn]]
What I need is the mean and standard deviation/variance of the two dimensional normal distribution that most closely represents the data, so as to be able to manipulate it, and, later, recreate it.
example
For the sake of simplicity I will defer to using a one dimensional normal function. If I have the following two dimensional data points
[
[-4, 0.0001],
[-3, 0.0044],
[-2, 0.054 ],
[-1, 0.242 ],
[0 , 0.3989],
[1 , 0.242 ],
[2 , 0.054 ],
[3 , 0.0044],
[4 , 0.0001]
]
I expect the script to output
mean = 0.0
standard deviation = 1.0
variance = 1.0
That way, if I want to, for example, change the standard deviation from sd = 1.0 to sd = 2.0, I can modify the curve, recreate it, sample the points -4...4, and rewrite the values to the data like so.
[
[-4, 0.027 ],
[-3, 0.0648],
[-2, 0.121 ],
[-1, 0.176 ],
[0 , 0.1995],
[1 , 0.176 ],
[2 , 0.121 ],
[3 , 0.0648],
[4 , 0.027 ]
]
Now my question is: how do I do that with a list of three dimensional points which closely represent a two dimensional normal distribution?
I would prefer to do this in Python, or to call a shell script. However, I would not be against using a program like MatLab or Maple.
According to the 1D example, we are given a sequence of values
(xi, yi), where thexivalues are vectors of dimensionn(n = 1orn = 2), and theyivalues are scalars. We wish to find meanmuand covariance matrixsigmafor a Gaussian functionfof dimensionnsuch that thef(xi)values are close to theyivalues.To solve this problem we have to define what close means. We can choose for example to minimize the sum of the squares
(yi - f(xi))^2. The resulting non-linear optimization problem inmuandsigmacan be solved with an iterative method such as Levenberg–Marquardt.If MatLab is used perhaps see their Curve Fitting Guide, specifically the Nonlinear Curve Fitting section and the
lsqnonlinandlsqcurvefitexamples. If Python is used, you can find library bindings to Levenberg–Marquardt implementations in C/C++ or Fortran.Whatever approach is used, I recommend trying out the solver on the 1D example first.
Another relevant resource is SciPy’s data fitting cookbook, which includes a section on fitting a 2D gaussian.