Hi,
I am trying to use your TransportMaps library to track an airplane using ground based radar. However, I got an error : NameError: free variable 'gfk' referenced before assignment in enclosing scope.
I would very appreciate if you can find some time to answer me.
Best regards,
Jacks.
Here the python version information:
import sys, scipy, numpy,TransportMaps; print(scipy.__version__, numpy.__version__, sys.version_info,TransportMaps.__version__)
('1.0.0', '1.14.0', sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0), '1.1b2')
Here the python script I use:
import numpy as np
import TransportMaps.Distributions as DIST
import TransportMaps.Maps as MAPS
import TransportMaps as TM
import TransportMaps.Distributions.Decomposable as DISTDEC
import TransportMaps.Likelihoods as LKL
init_x = np.array([100, 200, 2000])
dt = 0.05
state_dim = 3
obs_dim = 1
F = np.eye(3) + np.array([[0, 1, 0],
[0, 0, 0],
[0, 0, 0]]) * dt
Q = np.eye(state_dim)
Q0 = 50 *np.eye(state_dim)
nu_x0 = DIST.GaussianDistribution(init_x, Q0)
nu_w = DIST.GaussianDistribution(np.zeros(state_dim), Q)
nsteps = 10
Z = np.zeros( (state_dim, nsteps+1) )
Z[:,0] = np.array([200, 100, 1000])
for i in range(nsteps):
Z[:,i+1] = np.dot(F, Z[:,i]) + nu_w.rvs(1)[0,:]
range_std = 5.
R = np.diag([range_std**2])
nu_v = DIST.GaussianDistribution(np.zeros(obs_dim), R)
Y = []
for i in range(nsteps+1):
Y.append(np.sqrt(Z[2,i]**2+ Z[0,i]**2) + nu_v.rvs(1)[0,:] )
class NonlinearMap(MAPS.Map):
def __init__(self):
super(NonlinearMap, self).__init__(state_dim, obs_dim)
def evaluate(self, x, *args, **kwargs):
out = np.zeros((x.shape[0], self.dim_out))
out[:,0] = (x[:,0]**2 + x[:,2]**2) ** 0.5
return out
def grad_x(self, x, *args, **kwargs):
out = np.zeros((x.shape[0], self.dim_out, self.dim_in)) # out.shape = (m x dy x dx)
horiz_dist = x[:,0]
altitude = x[:,2]
denom = np.sqrt(horiz_dist**2 + altitude**2)
out[:,0,0] = horiz_dist/denom
out[:,0,2] = altitude/denom
return out
def hess_x(self, x, *args, **kwargs):
# eq 3.83
out = np.zeros((x.shape[0], self.dim_out, self.dim_in, self.dim_in)) # out.shape = (m x dy x dx x dx)
horiz_dist = x[:,0]
altitude = x[:,2]
denom = np.sqrt(horiz_dist**2 + altitude**2)
out[:,0,0,0] = altitude**2 /denom**3
out[:,0,0,2] = -1 * horiz_dist * altitude /denom**3
out[:,0,2,0] = out[:,0,0,2]
out[:,0,2,2] = horiz_dist**2 /denom**3
return out
PhiMap = MAPS.LinearMap(np.zeros(state_dim), F)
pi_trans = DISTDEC.AR1TransitionDistribution(nu_w, PhiMap)
pi = DISTDEC.SequentialHiddenMarkovChainDistribution([], [])
for n in range(nsteps+1):
Hmap = NonlinearMap()
ll = LKL.AdditiveLogLikelihood(Y[n], nu_v, Hmap)
if n == 0: pin = nu_x0
else: pin = pi_trans
pi.append(pin, ll)
lap = TM.laplace_approximation(pi)
The whole Error information:
NameError Traceback (most recent call last)
<ipython-input-1-28eebd6a4657> in <module>()
66 else: pin = pi_trans
67 pi.append(pin, ll)
---> 68 lap = TM.laplace_approximation(pi)
/Users/junjie/anaconda/lib/python2.7/site-packages/TransportMaps/Routines.pyc in laplace_approximation(pi, params, x0, tol, ders, fungrad)
1486 method='newton-cg',
1487 tol=tol,
-> 1488 options=options)
1489 else:
1490 raise ValueError("ders parameter not valid. Chose between 0,1,2.")
/Users/junjie/anaconda/lib/python2.7/site-packages/scipy/optimize/_minimize.pyc in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
482 elif meth == 'newton-cg':
483 return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,
--> 484 **options)
485 elif meth == 'l-bfgs-b':
486 return _minimize_lbfgsb(fun, x0, args, jac, bounds,
/Users/junjie/anaconda/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback, xtol, eps, maxiter, disp, return_all, **unknown_options)
1597 msg = ("Warning: CG iterations didn't converge. The Hessian is not "
1598 "positive definite.")
-> 1599 return terminate(3, msg)
1600
1601 pk = xsupi # search direction is solution to system.
/Users/junjie/anaconda/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in terminate(warnflag, msg)
1517 print(" Hessian evaluations: %d" % hcalls)
1518 fval = old_fval
-> 1519 result = OptimizeResult(fun=fval, jac=gfk, nfev=fcalls[0],
1520 njev=gcalls[0], nhev=hcalls, status=warnflag,
1521 success=(warnflag == 0), message=msg, x=xk,
NameError: free variable 'gfk' referenced before assignment in enclosing scope