Package boxmin :: Module levenberg
[hide private]

Source Code for Module boxmin.levenberg

  1  # This program is public domain. 
  2   
  3  from numpy   import zeros, sqrt 
  4  from copy    import deepcopy 
  5  from _boxmin import levenberg_marquardt_bc 
  6  from _boxmin import levenberg_marquardt_cc 
  7   
  8  #  Compute the fit parameters and covariance matrix using 
  9  #  levenberg-marquardt. 
 10  # 
 11   
 12   
 13  # ==================================================================== 
 14  # This function is used to calculate the covariance matrix 
 15  # without bound constraints 
 16  # ==================================================================== 
17 -def levenberg_marquardt2(function, 18 initParams, 19 boundsConstraints, 20 numDataPoints 21 ):
22 #change this for bounds constraints 23 numParams = len(initParams) 24 theParams = deepcopy(initParams) 25 covariance = zeros((numParams, numParams), 'd') 26 tempArray = zeros((numParams,), 'd') 27 levenberg_marquardt_cc(function, 28 theParams, 29 numDataPoints, 30 covariance, 31 tempArray) 32 return (theParams, covariance)
33 34 35 36 37 # ==================================================================== 38 # This function is used to calculate the covariance matrix 39 # with bound constraints 40 # ====================================================================
41 -def levenberg_marquardt(function, 42 initParams, 43 boundsConstraints, 44 numDataPoints 45 ):
46 #change this for bounds constraints 47 numParams = len(initParams) 48 theParams = deepcopy(initParams) 49 boundsCon = deepcopy( boundsConstraints) 50 lowBounds = boundsCon[ 0:numParams ] 51 highBounds = boundsCon[ numParams:len(boundsCon) ] 52 covariance = zeros((numParams, numParams), 'd') 53 tempArray = zeros((numParams,), 'd') 54 levenberg_marquardt_bc(function, 55 theParams, 56 lowBounds, 57 highBounds, 58 numDataPoints, 59 covariance, 60 tempArray) 61 return (theParams, covariance)
62 63 64 65 66 # ==================================================================== 67 # This function is used to calculate the uncertainty 68 # with bound constraints by levenberg_marquardt method 69 # ====================================================================
70 -def LM_Uncertainty2(function, 71 initParams, 72 boundsConstraints, 73 numDataPoints 74 ):
75 numParams = len(initParams) 76 theParams = deepcopy(initParams) 77 boundsCon = deepcopy( boundsConstraints) 78 lowBounds = boundsCon[ 0:numParams ] 79 highBounds = boundsCon[ numParams:len(boundsCon) ] 80 covariance = zeros((numParams, numParams), 'd') 81 tempArray = zeros((numParams,), 'd') 82 83 # TODO: Please double check this function 84 levenberg_marquardt_bc_un(function, 85 theParams, 86 lowBounds, 87 highBounds, 88 numDataPoints, 89 uncertainty, 90 tempArray) 91 92 return (theParams, uncertainty)
93 94 95 96 # ==================================================================== 97 # This function is used to calculate the uncertainty 98 # without bound constraints 99 # ====================================================================
100 -def LM_Uncertainty(function, 101 initParams, 102 boundsConstraints, 103 numDataPoints 104 ):
105 numParams = len(initParams) 106 theParams = deepcopy(initParams) 107 uncertainty = zeros(numParams, 'd') 108 tempArray = zeros((numParams,), 'd') 109 110 levenberg_marquardt_un(function, 111 theParams, 112 numDataPoints, 113 uncertainty, 114 tempArray 115 ) 116 117 return (theParams, uncertainty)
118