-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathleastsqbound.py
178 lines (122 loc) · 4.71 KB
/
leastsqbound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Constrained multivariate Levenberg-Marquardt optimization
An updated version of this file can be found at
https://github.com/jjhelmus/leastsqbound-scipy
The version here has known bugs which have been
fixed above, proceed at your own risk.
- Jonathan J. Helmus ([email protected])
"""
from scipy.optimize import leastsq
import numpy as np
def internal2external_grad(xi,bounds):
"""
Calculate the internal to external gradiant
Calculates the partial of external over internal
"""
ge = np.empty_like(xi)
for i,(v,bound) in enumerate(zip(xi,bounds)):
a = bound[0] # minimum
b = bound[1] # maximum
if a == None and b == None: # No constraints
ge[i] = 1.0
elif b == None: # only min
ge[i] = v/np.sqrt(v**2+1)
elif a == None: # only max
ge[i] = -v/np.sqrt(v**2+1)
else: # both min and max
ge[i] = (b-a)*np.cos(v)/2.
return ge
def i2e_cov_x(xi,bounds,cov_x):
grad = internal2external_grad(xi,bounds)
grad = grad = np.atleast_2d(grad)
return np.dot(grad.T,grad)*cov_x
def internal2external(xi,bounds):
""" Convert a series of internal variables to external variables"""
xe = np.empty_like(xi)
for i,(v,bound) in enumerate(zip(xi,bounds)):
a = bound[0] # minimum
b = bound[1] # maximum
if a == None and b == None: # No constraints
xe[i] = v
elif b == None: # only min
xe[i] = a-1.+np.sqrt(v**2.+1.)
elif a == None: # only max
xe[i] = b+1.-np.sqrt(v**2.+1.)
else: # both min and max
xe[i] = a+((b-a)/2.)*( np.sin(v)+1.)
return xe
def external2internal(xe,bounds):
""" Convert a series of external variables to internal variables"""
xi = np.empty_like(xe)
for i,(v,bound) in enumerate(zip(xe,bounds)):
a = bound[0] # minimum
b = bound[1] # maximum
if a == None and b == None: # No constraints
xi[i] = v
elif b == None: # only min
xi[i] = np.sqrt( (v-a+1.)**2.-1 )
elif a == None: # only max
xi[i] = np.sqrt( (b-v+1.)**2.-1 )
else: # both min and max
xi[i] = np.arcsin( (2.*(v-a)/(b-a))-1.)
return xi
def err(p,bounds,efunc,args):
pe = internal2external(p,bounds) # convert to external variables
return efunc(pe,*args)
def calc_cov_x(infodic,p):
"""
Calculate cov_x from fjac, ipvt and p as is done in leastsq
"""
fjac = infodic['fjac']
ipvt = infodic['ipvt']
n = len(p)
# adapted from leastsq function in scipy/optimize/minpack.py
perm = np.take(np.eye(n),ipvt-1,0)
r = np.triu(np.transpose(fjac)[:n,:])
R = np.dot(r,perm)
try:
cov_x = np.linalg.inv(np.dot(np.transpose(R),R))
except LinAlgError:
cov_x = None
return cov_x
def leastsqbound(func,x0,bounds,args=(),**kw):
"""
Constrained multivariant Levenberg-Marquard optimization
Minimize the sum of squares of a given function using the
Levenberg-Marquard algorithm. Contraints on parameters are inforced using
variable transformations as described in the MINUIT User's Guide by
Fred James and Matthias Winkler.
Parameters:
* func functions to call for optimization.
* x0 Starting estimate for the minimization.
* bounds (min,max) pair for each element of x, defining the bounds on
that parameter. Use None for one of min or max when there is
no bound in that direction.
* args Any extra arguments to func are places in this tuple.
Returns: (x,{cov_x,infodict,mesg},ier)
Return is described in the scipy.optimize.leastsq function. x and con_v
are corrected to take into account the parameter transformation, infodic
is not corrected.
Additional keyword arguments are passed directly to the
scipy.optimize.leastsq algorithm.
"""
# check for full output
if "full_output" in kw and kw["full_output"]:
full=True
else:
full=False
# convert x0 to internal variables
i0 = external2internal(x0,bounds)
# perfrom unconstrained optimization using internal variables
r = leastsq(err,i0,args=(bounds,func,args),**kw)
# unpack return convert to external variables and return
if full:
xi,cov_xi,infodic,mesg,ier = r
xe = internal2external(xi,bounds)
cov_xe = i2e_cov_x(xi,bounds,cov_xi)
# XXX correct infodic 'fjac','ipvt', and 'qtf'
return xe,cov_xe,infodic,mesg,ier
else:
xi,ier = r
xe = internal2external(xi,bounds)
return xe,ier