1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23   
 24  from Numeric import Float64, sum, transpose, zeros 
 25   
 26   
 27   
 28   
 29   
 30 -def chi2(data, back_calc_vals, errors): 
  31      """Function to calculate the chi-squared value. 
 32   
 33      The chi-sqared equation 
 34      ~~~~~~~~~~~~~~~~~~~~~~~ 
 35              _n_ 
 36              \    (yi - yi()) ** 2 
 37      Chi2  =  >   ---------------- 
 38              /__    sigma_i ** 2 
 39              i=1 
 40   
 41      where: 
 42          yi are the values of the measured data set. 
 43          yi() are the values of the back calculated data set. 
 44          sigma_i are the values of the error set. 
 45   
 46      The chi-squared value is returned. 
 47      """ 
 48   
 49       
 50      return sum((1.0 / errors * (data - back_calc_vals))**2) 
  51   
 52   
 53   
 54   
 55   
 56 -def dchi2(data, back_calc_vals, back_calc_grad, errors): 
  57      """Function to create the chi-squared gradient. 
 58   
 59      The chi-sqared gradient 
 60      ~~~~~~~~~~~~~~~~~~~~~~~ 
 61                     _n_ 
 62       dChi2         \   /  yi - yi()      dyi()  \  
 63      -------  =  -2  >  | ----------  .  ------- | 
 64      dthetaj        /__ \ sigma_i**2     dthetaj / 
 65                     i=1 
 66   
 67      where: 
 68          yi are the values of the measured data set. 
 69          yi() are the values of the back calculated data set. 
 70          sigma_i are the values of the error set. 
 71   
 72      The chi-squared gradient vector is returned. 
 73      """ 
 74   
 75       
 76      return -2.0 * sum(1.0 / (errors**2) * (data - back_calc_vals) * back_calc_grad) 
  77   
 78   
 79   
 80   
 81   
 82 -def d2chi2(data, back_calc_vals, back_calc_grad_j, back_calc_grad_k, back_calc_hess, errors): 
  83      """Function to create the chi-squared Hessian. 
 84   
 85      The chi-squared Hessian 
 86      ~~~~~~~~~~~~~~~~~~~~~~~ 
 87                            _n_ 
 88           d2chi2           \       1      /  dyi()     dyi()                         d2yi()     \  
 89      ---------------  =  2  >  ---------- | ------- . -------  -  (yi - yi()) . --------------- | 
 90      dthetaj.dthetak       /__ sigma_i**2 \ dthetaj   dthetak                   dthetaj.dthetak / 
 91                            i=1 
 92   
 93      where: 
 94          yi are the values of the measured relaxation data set. 
 95          yi() are the values of the back calculated relaxation data set. 
 96          sigma_i are the values of the error set. 
 97      """ 
 98   
 99       
100       
101       
102   
103       
104       
105      d2chi2 = 0.0 
106      for i in xrange(len(data)): 
107          d2chi2 = d2chi2 + 2.0 / (errors[i]**2) * (back_calc_grad_j[i] * back_calc_grad_k[i] - (data[i] - back_calc_vals[i]) * back_calc_hess[i]) 
108   
109      return d2chi2 
 110