\(\renewcommand\AA{\unicode{x212B}}\)

BivariateGaussian

Description

This function provides a Bivariate Gaussian distribution by acting as a wrapper to matplotlib.mlab.bivariate_normal. It differs from BivariateNormal in that it does not require any detector-space-specific input and allows for a correlation term.

\[V= \frac{A}{2 \pi \sigma_x \sigma_y \sqrt{1-\rho^2}} \times \exp \bigg[- \frac{1}{2(1-\rho^2)} \bigg( \frac{(x-\mu_x)^2}{\sigma_x^2} + \frac{(y-\mu_y)^2}{\sigma_y^2} - \frac{2 \rho (x-\mu_x) (y-\mu_y)} {\sigma_x \sigma_y} \bigg) \bigg] + bg\]

Attributes (non-fitting parameters)

Name Type Default Description
nX      
nY      

The attributes nX and nY store the dimension of the two histogram. This is useful when reconstructing a 2D histogram from a fit, which reduces the data 1D.

Properties (fitting parameters)

Name Default Description
A 1.0  
MuX 0.0  
MuY 0.0  
SigX 0.05  
SigY 0.05  
SigP 0.0  
Bg 0.0  

These are standard properties for a bivariate Gaussian distribution. See the equation above.

Usage

Here is an example of fitting a 2D histogram:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# import mantid algorithms, numpy and matplotlib
from mantid.simpleapi import *
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal
import numpy as np
import BivariateGaussian as BVG

sigmaX = 0.05
sigmaY = 0.15
sigmaXY = 0.0
muX = 0.1
muY = 0.1
p = 0.0
covariance = np.matrix([[sigmaX*sigmaX, p*sigmaX*sigmaY],
                        [p*sigmaX*sigmaY,sigmaY*sigmaY]])
rv = multivariate_normal([muX,muY], covariance)

# Generate some data to fit
x, y = np.mgrid[-0.5:0.5:.01, -0.5:0.5:.01]
pos = np.dstack((x, y))
Z = rv.pdf(pos)
Z += 0.1*(np.random.random(x.shape) - 0.5) # Noise

# Here we'll format it so we can fit this as a 1D function:
ZForFitting = np.empty(Z.shape + (2,))
ZForFitting[:,:,0] = Z
ZForFitting[:,:,1] = Z

pos = np.empty(x.shape + (2,))
pos[:, :, 0] = x
pos[:, :, 1] = y
bvgWS = CreateWorkspace(OutputWorkspace='bvgWS', DataX=pos.ravel(
        ), DataY=ZForFitting.ravel())

# Now we declare our function - we create one separately
# so that later we have access to functions which are not
# accessible to Function1D (e.g. 2D and 3D versions of the
# function.  The parameters we set here are an initial guess.
bvg = BVG.BivariateGaussian()
bvg.init()
bvg['A'] = 1.
bvg['SigX'] = 0.05
bvg['SigY'] = 0.15
bvg['SigP'] = 0.
bvg['MuX']=0.1
bvg['MuY']=0.1
bvg.setAttributeValue('nX', Z.shape[0])
bvg.setAttributeValue('nY', Z.shape[1])
Fit(Function=bvg, InputWorkspace='bvgWS', Output='bvgfit',
                        Minimizer='Levenberg-MarquardtMD')

ZFit = bvg.function2D(pos)

#Plot the results
plt.figure(1)
plt.clf()
plt.subplot(1,2,1)
plt.imshow(Z, origin='lower')
plt.title('Data')
plt.subplot(1,2,2)
plt.imshow(ZFit, origin='lower')
plt.title('Fit')
plt.show()

Categories: FitFunctions | General

Source

Python: BivariateGaussian.py