Package lib :: Module model_selection
[hide private]
[frames] | no frames]

Source Code for Module lib.model_selection

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2013 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module for the statistical concept of model selection.""" 
 24   
 25  # Python module imports. 
 26  from math import log 
 27   
 28  # relax module imports. 
 29  from lib.errors import RelaxError 
 30   
 31   
32 -def aic(chi2, k, n):
33 """Akaike's Information Criteria (AIC). 34 35 The formula is:: 36 37 AIC = chi2 + 2k 38 39 40 @param chi2: The minimised chi-squared value. 41 @type chi2: float 42 @param k: The number of parameters in the model. 43 @type k: int 44 @param n: The dimension of the relaxation data set. 45 @type n: int 46 @return: The AIC value. 47 @rtype: float 48 """ 49 50 return chi2 + 2.0*k
51 52
53 -def aicc(chi2, k, n):
54 """Small sample size corrected AIC. 55 56 The formula is:: 57 58 2k(k + 1) 59 AICc = chi2 + 2k + --------- 60 n - k - 1 61 62 63 @param chi2: The minimised chi-squared value. 64 @type chi2: float 65 @param k: The number of parameters in the model. 66 @type k: int 67 @param n: The dimension of the relaxation data set. 68 @type n: int 69 @return: The AIC value. 70 @rtype: float 71 """ 72 73 if n > (k+1): 74 return chi2 + 2.0*k + 2.0*k*(k + 1.0) / (n - k - 1.0) 75 elif n == (k+1): 76 raise RelaxError("The size of the dataset, n=%s, is too small for this model of size k=%s. This situation causes a fatal division by zero as:\n AICc = chi2 + 2k + 2k*(k + 1) / (n - k - 1).\n\nPlease use AIC model selection instead." % (n, k)) 77 elif n < (k+1): 78 raise RelaxError("The size of the dataset, n=%s, is too small for this model of size k=%s. This situation produces a negative, and hence nonsense, AICc score as:\n AICc = chi2 + 2k + 2k*(k + 1) / (n - k - 1).\n\nPlease use AIC model selection instead." % (n, k))
79 80
81 -def bic(chi2, k, n):
82 """Bayesian or Schwarz Information Criteria. 83 84 The formula is:: 85 86 BIC = chi2 + k ln n 87 88 89 @param chi2: The minimised chi-squared value. 90 @type chi2: float 91 @param k: The number of parameters in the model. 92 @type k: int 93 @param n: The dimension of the relaxation data set. 94 @type n: int 95 @return: The AIC value. 96 @rtype: float 97 """ 98 99 return chi2 + k * log(n)
100