$$\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# import mantid algorithms, numpy and matplotlib
2from mantid.simpleapi import *
3import matplotlib.pyplot as plt
4from scipy.stats import multivariate_normal
5import numpy as np
6import BivariateGaussian as BVG
7
8sigmaX = 0.05
9sigmaY = 0.15
10sigmaXY = 0.0
11muX = 0.1
12muY = 0.1
13p = 0.0
14covariance = np.matrix([[sigmaX*sigmaX, p*sigmaX*sigmaY],
15                        [p*sigmaX*sigmaY,sigmaY*sigmaY]])
16rv = multivariate_normal([muX,muY], covariance)
17
18# Generate some data to fit
19x, y = np.mgrid[-0.5:0.5:.01, -0.5:0.5:.01]
20pos = np.dstack((x, y))
21Z = rv.pdf(pos)
22Z += 1.5*(np.random.random(x.shape) - 0.5) # Noise
23
24# Here we'll format it so we can fit this as a 1D function:
25ZForFitting = np.empty(Z.shape + (2,))
26ZForFitting[:,:,0] = Z
27ZForFitting[:,:,1] = Z
28
29pos = np.empty(x.shape + (2,))
30pos[:, :, 0] = x
31pos[:, :, 1] = y
32bvgWS = CreateWorkspace(OutputWorkspace='bvgWS', DataX=pos.ravel(
33        ), DataY=ZForFitting.ravel())
34
35# Now we declare our function - we create one separately
36# so that later we have access to functions which are not
37# accessible to Function1D (e.g. 2D and 3D versions of the
38# function.  The parameters we set here are an initial guess.
39bvg = BVG.BivariateGaussian()
40bvg.init()
41bvg['A'] = 1.
42bvg['SigX'] = 0.05
43bvg['SigY'] = 0.15
44bvg['SigP'] = 0.
45bvg['MuX']=0.1
46bvg['MuY']=0.1
47bvg.setAttributeValue('nX', Z.shape[0])
48bvg.setAttributeValue('nY', Z.shape[1])
49Fit(Function=bvg, InputWorkspace='bvgWS', Output='bvgfit',
50                        Minimizer='Levenberg-MarquardtMD')
51
52ZFit = bvg.function2D(pos)
53
54#Plot the results
55fig, axes = plt.subplots(nrows=1, ncols=2, subplot_kw={'projection': 'mantid'})
56axes[0].imshow(Z, origin='lower')
57axes[0].set_title('Data')
58axes[1].imshow(ZFit, origin='lower')
59axes[1].set_title('Fit')
60fig.show()


Categories: FitFunctions | General

## Source¶

Python: BivariateGaussian.py