3 "Function to calculate the chi-squared value."
4
5
6 - def chi2(self, params, diff_type, diff_params, model, relax_data, errors, print_flag=0):
7 """Function to calculate the chi-squared value.
8
9 Function arguments
10 ~~~~~~~~~~~~~~~~~~
11
12 1: params - a list containing the parameter values specific for the given model.
13 The order of parameters must be as follows:
14 m1 - {S2}
15 m2 - {S2, te}
16 m3 - {S2, Rex}
17 m4 - {S2, te, Rex}
18 m5 - {S2f, S2s, ts}
19 2: diff_type - string. The diffusion tensor, ie 'iso', 'axial', 'aniso'
20 3: diff_params - array. An array with the diffusion parameters
21 4: model - string. The model
22 5: relax_data - array. An array containing the experimental relaxation values.
23 6: errors - array. An array containing the experimental errors.
24
25
26 The chi-sqared equation
27 ~~~~~~~~~~~~~~~~~~~~~~~
28
29 Data structure: self.data.chi2
30 Type: Double precision floating point value.
31 Dependencies: self.data.ri
32 Required by: None
33
34
35 Formula
36 ~~~~~~~
37 _n_
38 \ (Ri - Ri()) ** 2
39 Chi2 = > ----------------
40 /__ sigma_i ** 2
41 i=1
42
43 where:
44 Ri are the values of the measured relaxation data set.
45 Ri() are the values of the back calculated relaxation data set.
46 sigma_i are the values of the error set.
47
48 Returned is the chi-squared value.
49 """
50
51
52 if print_flag == 2:
53 print "\n< chi2 >"
54 print "Params: " + `params`
55
56 self.data.params = params
57 self.data.diff_type = diff_type
58 self.data.diff_params = diff_params
59 self.data.model = model
60 self.data.relax_data = relax_data
61 self.data.errors = errors
62
63
64
65
66
67 test = [ self.data.relax_data[:], self.data.errors[:], self.data.params.tolist(), self.data.model ]
68 if test != self.data.relax_test:
69 self.init_param_types()
70 self.Ri()
71 self.data.relax_test = test[:]
72
73
74 self.data.chi2 = 0.0
75 for i in range(len(self.data.relax_data)):
76 if self.data.errors[i] != 0.0:
77 self.data.chi2 = self.data.chi2 + ((self.data.relax_data[i] - self.data.ri[i]) / self.data.errors[i])**2
78 else:
79 self.data.chi2 = 1e99
80 continue
81
82
83 if print_flag == 2:
84 print "J(w): " + `self.data.jw`
85 print "Ri: " + `self.data.ri`
86 print "chi2: " + `self.data.chi2`
87 print "\n"
88
89 return self.data.chi2
90