1
2
3 """
4 Performing basic statistics.
5
6 Author: R. Lombaert
7
8 """
9
10 import types
11 import numpy as np
12
13
15
16 """
17 Calculate the reduced chi-squared value of a data array minus a model array,
18 taking into account the noise in the data array.
19
20 @param data: The data set. Must have same dimensions as model!
21 @type data: array
22 @param model: The model array. Must have same dimensions as data!
23 @type model: array
24 @param noise: the noise in the data array. Give one value for overall noise
25 or individual values for every entry in data/model.
26 @type noise: float/array
27
28 @keyword ndf: Number of degrees of freedom. Default in case of calculating
29 for one single model. Typically the number of variable grid
30 parameters in a grid calculation.
31
32 (default: 0)
33 @type ndf: int
34 @keyword mode: The method used for the chi^2 calculation. 'diff' is the
35 standard differentiation of the chi^2. 'log' redistributes
36 the ratio of data and model points on a logarithmic scale
37 such that lower than 1 or larger than 1 are essentially
38 equivalent. This removes bias in either direction of 1. Other
39 than the input array distribution the chi^2 'log' method is
40 mathematically equivalent to the differentiation.
41
42 (default: 'diff')
43 @type mode: str
44
45 @return: The chi squared value
46 @rtype: float
47
48 """
49
50 mode = str(mode).lower()
51 if type(data) not in [types.ListType,np.ndarray]:
52 data = [data]
53 data, model, noise = np.array(data), np.array(model), np.array(noise)
54 if mode == 'diff':
55 chi2 = ((data - model)**2./noise**2.).sum()/(len(data)-ndf-1)
56 elif mode == 'log':
57 chi2 = ((10**abs(np.log10(data/model))-1)**2./(noise/model)**2.).sum()
58 chi2 /= (len(data)-ndf-1)
59 else:
60 print 'Chi^2 mode not recognized.'
61 chi2 = None
62 return chi2
63
64
65
67
68 """
69 Calculate the loglikelihood value of a data array minus a model array,
70 taking into account the noise in the data array.
71
72 @param data: The data set. Must have same dimensions as model!
73 @type data: array
74 @param model: The model array. Must have same dimensions as data!
75 @type model: array
76 @param noise: the noise in the data array.
77 @type noise: float/array
78
79 @return: The loglikelihood value
80 @rtype: float
81
82 """
83
84 data, model, noise = np.array(data), np.array(model), np.array(noise)
85 lll = (-np.log(np.sqrt(2.*np.pi)) - np.log(noise) - 1./2.*((data-model)/noise)**2.).sum()
86 return lll
87