def convert_to_adnumber(coordinate_list): '''Convert the location vectors from floats into adnumbers to enable differentiation''' adnumber_coordinate_list = [] for i in range(len(coordinate_list)): adnumber_coordinate_list.append([adnumber(coordinate_list[i][0]), adnumber(coordinate_list[i][1])]) coordinate_list = adnumber_coordinate_list return coordinate_list
def convert_to_adnumber(self, coordinate_list): '''Convert the location vectors from floats into adnumbers to enable differentiation''' adnumber_coordinate_list = [] for i in range(len(coordinate_list)): adnumber_coordinate_list.append([adnumber(coordinate_list[i][0]), adnumber(coordinate_list[i][1])]) coordinate_list = adnumber_coordinate_list return coordinate_list
def test_rate_function(): K = 10 s = np.diff(np.logspace(np.log10(.01), np.log10(3.), 41)) model = smcpp.model.SMCModel(s, np.logspace(np.log10(.01), np.log10(3.), K)) model[:] = [ ad.adnumber(0.002676322760403453), ad.adnumber(-0.010519987448975402), ad.adnumber(0.006727140517177145), ad.adnumber(0.0031333684894676865), ad.adnumber(-0.02302979056648467), ad.adnumber(0.0026368097606793172), ad.adnumber(0.0019921562626012993), ad.adnumber(0.004958301100037235), ad.adnumber(0.003199704865436452), ad.adnumber(0.0050129872575249744) ] hidden_states = np.concatenate([[0.], np.logspace(-2, 1, 10), [np.inf]]) eta = smcpp._smcpp.PyRateFunction(model, hidden_states) t = 1.08 Rt = eta.R(t) for k in range(K): dq = Rt.d(model[k]) model[k] += 1e-8 Rt1 = smcpp._smcpp.PyRateFunction(model, hidden_states).R(t) a = float(Rt1 - Rt) * 1e8 print(k, a, dq) model[k] -= 1e-8
def Hmat(d,i,j,obs_str,Mlist): #Creates H^hat matrix for given obs, i,j and data d Hlist=[-9999]*(j-i) Obslist=re.findall(r'[^,;\s]+', obs_str) Hhold=[-9999]*len(Obslist) obsdict={'nee': Mod.NEE, 'lf': Mod.LF, 'lw': Mod.LW, 'cf': Mod.Cf, \ 'cr': Mod.Cr, 'cw': Mod.Cw, 'cl': Mod.Cl, 'cs': Mod.Cs} for x in xrange(i,j): Cf=ad.adnumber(d.Cf) #Clist[x-i][0]) #Redo this!!! Cr=ad.adnumber(d.Cr) #Clist[x-i][1]) Cw=ad.adnumber(d.Cw) #Clist[x-i][2]) Cl=ad.adnumber(d.Cl) #Clist[x-i][3]) Cs=ad.adnumber(d.Cs) #Clist[x-i][4]) for y in range(0,len(Obslist)): Hhold[y]=ad.jacobian(obsdict[Obslist[y]](Cf,Cr,Cw,Cl,Cs,x,d),[Cf,Cr,Cw,Cl,Cs]) Hlist[x-i]=np.vstack(Hhold) stacklist=[Hlist[0]]+[-9999]*(j-i-1) #Creates H hat matrix for x in xrange(1,j-i): stacklist[x]=Hlist[x]*Mfac(Mlist,x-1) Hmat=np.vstack(stacklist) return np.matrix(Hmat)
def test_ad(): def ssq(x,y): return x*x+y*y+2*x*y+3*y def fn(x,z): y = z*z return ssq(x,y) inc = 1e-5 x=1.234 y=5.678 z=3.678 dx = adnumber(x) dy = adnumber(y) dz = adnumber(z) s0 = ssq(x,y) dsx = (ssq(x+inc,y) - ssq(x-inc,y))/2/inc dsy = (ssq(x,y+inc) - ssq(x,y-inc))/2/inc dsx2=ssq(dx,y).d(dx) dsy2=ssq(x,dy).d(dy) print("ssq: dxs=" + str([dsx, dsx2])) print("ssq: dys=" + str([dsy, dsy2])) f0 = fn(x,z) dfx = (fn(x+inc,z) - fn(x-inc,z))/2/inc dfz = (ssq(x,z+inc) - ssq(x,z-inc))/2/inc dfx2=fn(dx,z).d(dx) dfz2=ssq(x,dz).d(dz) print("fn: dxs=" + str([dfx, dfx2])) print("fn: dzs=" + str([dfz, dfz2]))
def LinDALEC(Cf, Cr, Cw, Cl, Cs, x, d): #Tangent Linear DALEC evergreen model Cf = ad.adnumber(Cf) Cr = ad.adnumber(Cr) Cw = ad.adnumber(Cw) Cl = ad.adnumber(Cl) Cs = ad.adnumber(Cs) Dalecoutput = DALEC(Cf, Cr, Cw, Cl, Cs, x, d) M = np.matrix(ad.jacobian(Dalecoutput, [Cf, Cr, Cw, Cl, Cs])) return Dalecoutput, M
def assignLog(self, logValue, units=None): if not units == None: self.setUnits(units) if self.useLog: self.value._magnitude = numpy.exp(logValue) self.ADvalue = ad.adnumber(numpy.exp(logValue), self.name) else: self.value._magnitude = logValue self.ADvalue = ad.adnumber(logValue, self.name) self.checkValue() # a weak version of integrity()
def test_d(im): eps = 1e-4 model = im.model K = model.K model[:] = ad.adnumber(np.arange(1, K + 1, dtype=float) / K) y = model[:].copy() print(y) print(model.stepwise_values()) im.model = model im.E_step() em1 = im.emission print(em1) M = em1.shape[0] I = np.eye(K) for k in range(K): aa = [float(_) for _ in y] aa += eps * I[k] model[:] = aa im.model = model im.Q() em2 = im.emission for i in range(M): for j in range(M): dx = em1[i, j].d(y[k]) dx2 = (em2[i, j] - float(em1[i, j])) / eps print(k, i, j, dx, dx2, (dx - dx2) / dx) assert False
def test_bug7(): d = { 'class': 'SMCModel', 'spline_class': 'BSpline', 'y': [ -2.433625923004, -4.6051701859880909, 3.6726078178617159, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], 'knots': [ 0.02, 0.031697863849222269, 0.050237728630191596, 0.079621434110699482, 0.12619146889603869, 0.20000000000000004, 0.31697863849222285, 0.50237728630191625, 0.79621434110699474, 1.2619146889603867, 2.0 ], 's': [ 0.02, 0.0030956396937891623, 0.0035747889494773186, 0.0041281018779233576, 0.0047670576795886162, 0.0055049123283644683, 0.0063569735840901878, 0.0073409185720541212, 0.0084771605180803608, 0.0097892722475999561, 0.011304475234748909, 0.013054204347456175, 0.0150747600048972, 0.017408061277172454, 0.020102515551248895, 0.0232140228055219, 0.026807135327986953, 0.030956396937891623, 0.0357478894947732, 0.041281018779233569, 0.047670576795886155, 0.055049123283644696, 0.063569735840901809, 0.073409185720541281, 0.084771605180803622, 0.097892722475999561, 0.11304475234748901, 0.13054204347456178, 0.15074760004897203, 0.17408061277172449, 0.2010251555124889, 0.23214022805521917, 0.26807135327986908 ] } m = model.SMCModel.from_dict(d) m[0] = ad.adnumber(m[0], 0) assert m[0] in m.regularizer().d()
def evaluate_linked(constants, linked): "Evaluates the values and gradients of linked variables." kdc = KeyDict( {k: adnumber(maybe_flatten(v), k) for k, v in constants.items()}) kdc_plain = None array_calulated = {} for v, f in linked.items(): try: if v.veckey and v.veckey.original_fn: if v.veckey not in array_calulated: ofn = v.veckey.original_fn array_calulated[v.veckey] = np.array(ofn(kdc)) out = array_calulated[v.veckey][v.idx] else: out = f(kdc) constants[v] = out.x v.descr["gradients"] = { adn.tag: grad for adn, grad in out.d().items() if adn.tag } # else it's user-created except Exception as exception: # pylint: disable=broad-except from .. import settings if settings.get("ad_errors_raise", None): raise print("Warning: skipped auto-differentiation of linked variable" " %s because %s was raised. Set `gpkit.settings" "[\"ad_errors_raise\"] = True` to raise such Exceptions" " directly.\n" % (v, repr(exception))) if kdc_plain is None: kdc_plain = KeyDict(constants) constants[v] = f(kdc_plain) v.descr.pop("gradients", None)
def test_d(im): eps = 1e-8 model = im.model K = model.K model[:] = [ad.adnumber(1. * k / K, tag=k) for k in range(1, K + 1)] print(model.stepwise_values()) im.model = model im.E_step() im.Q() trans1 = im.transition print(trans1) M = trans1.shape[0] m2 = smcpp.model.SMCModel.from_dict(model.to_dict()) im.model = m2 for k in range(K): m2[k] += 1e-8 im.model = m2 im.Q() m2[k] -= 1e-8 trans2 = im.transition for i in range(M): for j in range(M): try: dx = trans1[i, j].d(model[k]) except AttributeError: dx = 0. dx2 = (trans2[i, j] - float(trans1[i, j])) / eps print(k, i, j, dx, dx2) assert False
def nextguess(guess, itersLeft): # print( guess) x = adnumber(guess) cc = fn(x) if (abs(cc.x) > accuracy) and (abs(cc.d(x)) > accuracy * 10.0) and (itersLeft > 0): return nextguess(max(min((guess - cc.x / cc.d(x)), (2.0 * guess)), (guess / 2.0)), itersLeft - 1) else: return guess
def convert_to_adnumber(coordinate_list): '''Convert the location vectors from floats into adnumbers to enable differentiation''' adnumber_coordinate_list = [] for i in range(len(coordinate_list)): adnumber_coordinate_list.append( adnumber(float(coordinate_list[i].values()))) coordinate_list = adnumber_coordinate_list return coordinate_list
def __init__(self, f, g, x): self.f = f self.f = self.f.replace('math', 'admath') self.g = g self.x = ad.adnumber(x) self.g_x = 0 self.g_x_prime = 0 self.f_x = 0 self.f_x_prime = 0
def DoJac(f, X): """Takes a function and a numpy array. Returns (J,Y) where Y = f(X) and J is the Jacobian.""" Xad = adnumber(X) Yad = f(Xad) J = np.array(jacobian(Yad.flatten(), Xad.flatten())) return J, Yad.astype(float)
def H(d, i, j, obs_str): Hlist = [-9999]*(j-i) Obslist = re.findall(r'[^,;\s]+', obs_str) Hhold = [-9999]*len(Obslist) obsdict = {'nee': Mod.NEE, 'lf': Mod.LF, 'lw': Mod.LW, 'cf': Mod.Cf, \ 'cr': Mod.Cr, 'cw': Mod.Cw, 'cl': Mod.Cl, 'cs': Mod.Cs} for x in xrange(i,j): Cf = ad.adnumber(d.Cf) #Clist[x-i][0]) #Redo this!!! Cr = ad.adnumber(d.Cr) #Clist[x-i][1]) Cw = ad.adnumber(d.Cw) #Clist[x-i][2]) Cl = ad.adnumber(d.Cl) #Clist[x-i][3]) Cs = ad.adnumber(d.Cs) #Clist[x-i][4]) for y in range(0,len(Obslist)): Hhold[y] = ad.jacobian(obsdict[Obslist[y]](Cf,Cr,Cw,Cl,Cs,x,d),[Cf,Cr,Cw,Cl,Cs]) Hlist[x-i] = np.vstack(Hhold) return np.vstack(Hlist)
def linob(ob, pvals, dC, x): """Function returning jacobian of observation with respect to the parameter list. Takes an obs string, a parameters list, a dataClass and a time step x. """ modobdict = {'gpp': gpp, 'nee': nee, 'rt': rec, 'cf': cf, 'clab': clab, 'cr': cr, 'cw': cw, 'cl': cl, 'cs': cs, 'lf': lf, 'lw': lw, 'lai':lai} dpvals = ad.adnumber(pvals) output = modobdict[ob](dpvals, dC, x) return np.array(ad.jacobian(output, dpvals))
def lin_dalecv2(pvals, dC, x): """Linear DALEC model passes a list or array of parameters to the function dalecv2 and returns a linearized model M for timestep xi. Takes, a list of parameters (pvals), a dataClass (dC) and a time step (x). """ p = ad.adnumber(pvals) dalecoutput = dalecv2(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15], p[16], p[17], p[18], p[19], p[20], p[21], p[22], dC, x) lin_model = ad.jacobian(dalecoutput, p) modval = np.array([float(x) for x in dalecoutput]) return modval, lin_model
def linmod_list2(pvals, dC, start, fin): """Creates an array of linearized models (Mi's) taking a list of initial param values, a dataClass (dC) and a start and finish time. """ mod_list = np.concatenate((np.array([pvals]),\ np.ones((fin - start, len(pvals)))*-9999.)) matlist = np.ones((fin - start,23,23))*-9999. dv2=dalecv2_input for x in xrange(start, fin): p=ad.adnumber(mod_list[x-start]) mod_list[(x+1)-start] = dv2(p, dC, x) matlist[x-start] = ad.jacobian(mod_list[(x+1)-start], p) mod_list[(x+1)-start] = np.array([float(y) for y in\ mod_list[(x+1)-start]]) return mod_list, matlist
def calculateDerivates(self, func, xs): retYs = np.zeros(len(xs)) retDYs = np.zeros(len(xs)) retDDYs = np.zeros(len(xs)) for i in range(0, len(xs)): currX = xs[i] adCurrX = adnumber(currX) ys = func(adCurrX) retYs[i] = ys.x retDYs[i] = ys.d(adCurrX) retDDYs[i] = ys.d2(adCurrX) return retYs, retDYs, retDDYs
def test_d(model12): ts = [0.0, 0.5, 1.0, np.inf] n1 = 10 n2 = 8 model1 = model12.model1 ders = model1.a = np.array( [ad.adnumber(x, tag=i) for i, x in enumerate(model1.a)], dtype=object) j0 = smcpp._smcpp.joint_csfs(n1, n2, 2, 0, model12, [0., 1.0], 100)[0] for i in range(2): model1.a[i].x += 1e-8 j1 = np.array( smcpp._smcpp.joint_csfs(n1, n2, 2, 0, model12, [0., 1.0], 100))[0] model1.a[i].x -= 1e-8 for x, y in zip(j0.flat, j1.flat): print(x.d(ders[i]), float(y - x) * 1e8) assert False
def evaluate_linked(constants, linked): "Evaluates the values and gradients of linked variables." if adnumber: kdc = KeyDict( {k: adnumber(maybe_flatten(v)) for k, v in constants.items()}) kdc.log_gets = True kdc_plain = None array_calulated, logged_array_gets = {}, {} for v, f in linked.items(): try: assert adnumber # trigger exit if ad not found if v.veckey and v.veckey.original_fn: if v.veckey not in array_calulated: ofn = v.veckey.original_fn array_calulated[v.veckey] = np.array(ofn(kdc)) logged_array_gets[v.veckey] = kdc.logged_gets logged_gets = logged_array_gets[v.veckey] out = array_calulated[v.veckey][v.idx] else: logged_gets = kdc.logged_gets out = f(kdc) constants[v] = out.x v.descr["gradients"] = {} for key in logged_gets: if key.shape: grad = out.gradient(kdc[key]) v.gradients[key] = np.array(grad) else: v.gradients[key] = out.d(kdc[key]) except Exception as exception: # pylint: disable=broad-except from .. import settings if settings.get("ad_errors_raise", None): raise if adnumber: print( "Warning: skipped auto-differentiation of linked variable" " %s because %s was raised. Set `gpkit.settings" "[\"ad_errors_raise\"] = True` to raise such Exceptions" " directly.\n" % (v, repr(exception))) if kdc_plain is None: kdc_plain = KeyDict(constants) constants[v] = f(kdc_plain) v.descr.pop("gradients", None) finally: if adnumber: kdc.logged_gets = set()
def _derivatives(self, state): """ Computes the jacobian and state derivatives Args: state (np.ndarray): state vector to find derivatives of Returns: (np.ndarray [1 x n], np.ndarray [n x n]) """ ad_state = ad.adnumber(state) state_deriv = self.force_model(0, ad_state) a_matrix = jacobian(state_deriv, ad_state) # remove ad number from all state values before returning state_deriv = [state.real for state in state_deriv] return state_deriv, a_matrix
def evaluate_linked(constants, linked): "Evaluates the values and gradients of linked variables." if adnumber: kdc = KeyDict( {k: adnumber(maybe_flatten(v)) for k, v in constants.items()}) kdc.log_gets = True kdc_plain = None array_calulated, logged_array_gets = {}, {} for v, f in linked.items(): try: assert adnumber # trigger exit if ad not found if v.veckey and v.veckey.original_fn: if v.veckey not in array_calulated: ofn = v.veckey.original_fn array_calulated[v.veckey] = np.array(ofn(kdc)) logged_array_gets[v.veckey] = kdc.logged_gets logged_gets = logged_array_gets[v.veckey] out = array_calulated[v.veckey][v.idx] else: logged_gets = kdc.logged_gets out = f(kdc) constants[v] = out.x v.descr["gradients"] = {} for key in logged_gets: if key.shape: grad = out.gradient(kdc[key]) v.gradients[key] = np.array(grad) else: v.gradients[key] = out.d(kdc[key]) except Exception: # pylint: disable=broad-except from .. import settings if settings.get("ad_errors_raise", None): raise if adnumber: print("Couldn't auto-differentiate linked variable %s\n " "(to raise the error directly for debugging purposes," " set gpkit.settings[\"ad_errors_raise\"] to True)" % v) if kdc_plain is None: kdc_plain = KeyDict(constants) constants[v] = f(kdc_plain) v.descr.pop("gradients", None) finally: if adnumber: kdc.logged_gets = set()
def test_bug5(): d = { 'class': 'SMCModel', 'spline_class': 'PChipSpline', 'y': [ -2.433625923004, -4.6051701859880909, 3.6726078178617159, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], 'knots': [ 0.02, 0.031697863849222269, 0.050237728630191596, 0.079621434110699482, 0.12619146889603869, 0.20000000000000004, 0.31697863849222285, 0.50237728630191625, 0.79621434110699474, 1.2619146889603867, 2.0 ], 's': [ 0.02, 0.0030956396937891623, 0.0035747889494773186, 0.0041281018779233576, 0.0047670576795886162, 0.0055049123283644683, 0.0063569735840901878, 0.0073409185720541212, 0.0084771605180803608, 0.0097892722475999561, 0.011304475234748909, 0.013054204347456175, 0.0150747600048972, 0.017408061277172454, 0.020102515551248895, 0.0232140228055219, 0.026807135327986953, 0.030956396937891623, 0.0357478894947732, 0.041281018779233569, 0.047670576795886155, 0.055049123283644696, 0.063569735840901809, 0.073409185720541281, 0.084771605180803622, 0.097892722475999561, 0.11304475234748901, 0.13054204347456178, 0.15074760004897203, 0.17408061277172449, 0.2010251555124889, 0.23214022805521917, 0.26807135327986908 ] } model1 = model.SMCModel.from_dict(d) c = model1._spline._coef model1[:] += np.random.normal(0, .01, size=len(model1[:])) for k in range(len(model1[:])): model1[k] = ad.adnumber(model1[k], tag=k) c1 = model1._spline.integrated_curvature() try: d = c1.d(model1[k]) except AttributeError: d = 0. model1[k] = model1[k] + 1e-8 c2 = model1._spline.integrated_curvature() model1[k] = float(model1[k]) - 1e-8 print(k, float(c2 - c1) * 1e8, d)
def _msr_resid(self, msr, state_prop): """ Computes the measurement residual and measurement partials Args: msr (filtering.MSR): measurement to use for computations state_prop (np.ndarray): nominal state vector propagated to the measurement time Returns: (np.ndarray [1 x len(MSR.msr)], np.ndarray [len(MSR.msr) x n]) """ # get estimated station position and estimated msr dummymsr = msr.__class__(msr.time, None, msr.stn, None) stn_state_est = msr.stn.state(msr.time) est_msr = dummymsr.calc_msr(state_prop, stn_state_est) # compute residuals and partials matrix y_i = np.subtract(msr.msr, est_msr).reshape(len(msr.msr), 1) h_tilde = msr.partials(ad.adnumber(state_prop), stn_state_est) return (y_i, h_tilde)
def evaluate_linked(constants, linked): "Evaluates the values and gradients of linked variables." if adnumber: kdc = KeyDict({k: adnumber(maybe_flatten(v)) for k, v in constants.items()}) kdc.log_gets = True kdc_plain = KeyDict(constants) array_calulated, logged_array_gets = {}, {} for v, f in linked.items(): try: assert adnumber # trigger exit if ad not found if v.veckey and v.veckey.original_fn: if v.veckey not in array_calulated: ofn = v.veckey.original_fn array_calulated[v.veckey] = np.array(ofn(kdc)) logged_array_gets[v.veckey] = kdc.logged_gets logged_gets = logged_array_gets[v.veckey] out = array_calulated[v.veckey][v.idx] else: logged_gets = kdc.logged_gets out = f(kdc) constants[v] = out.x v.descr["gradients"] = {} for key in logged_gets: if key.shape: grad = out.gradient(kdc[key]) v.gradients[key] = np.array(grad) else: v.gradients[key] = out.d(kdc[key]) except Exception: # pylint: disable=broad-except from .. import settings if settings.get("ad_errors_raise", None): raise if adnumber: print("Couldn't auto-differentiate linked variable %s.\n " "(to raise the error directly for debugging purposes," " set gpkit.settings[\"ad_errors_raise\"] to True)" % v) constants[v] = f(kdc_plain) v.descr.pop("gradients", None) finally: if adnumber: kdc.logged_gets = set()
def test_inference_apart(hs, s): K = 10 model1 = smcpp.model.SMCModel(s, np.logspace(np.log10(.01), np.log10(3.), K)) model2 = smcpp.model.SMCModel(s, np.logspace(np.log10(.01), np.log10(3.), K)) n = 10 fakeobs = [[1, -1, 0, 0, -1, 0, 0], [1, 1, 0, 0, 1, 0, 0], [10, 0, 0, 0, 0, 0, 0], [10, -1, 0, 0, 1, 0, 0], [200000, 1, 0, n - 2, 0, 0, n - 1], [1, 0, n - 4, n - 2, -1, n - 3, n - 1], [1, 0, n - 4, n - 2, 1, n - 3, n - 1], [1, 1, n - 4, n - 2, 1, n - 3, n - 1]] # fakeobs *= 20 im = smcpp._smcpp.PyTwoPopInferenceManager( n - 2, n - 1, 1, 1, np.array([fakeobs] * 2, dtype=np.int32), hs) model1[:] = [ ad.adnumber(x, i) for i, x in enumerate([ 0.002676322760403453, -0.010519987448975402, 0.006727140517177145, 0.0031333684894676865, -0.02302979056648467, 0.0026368097606793172, 0.001992156262601299, 0.004958301100037235, 0.003199704865436452, 0.0050129872575249744 ]) ] model2[:] = model1[:].astype('float') split = 0.5 model = smcpp.model.SMCTwoPopulationModel(model1, model2, split) im.model = model im.theta = 0.0025000000000000001 im.rho = 0.0031206103977654887 im.E_step() e = im.emission_probs q0 = im.Q(1) for k in range(K): coord = (0, k) dq = q0.d(model[coord]) model[coord] += 1e-8 im.model = model q1 = im.Q(1) a = float(q1 - q0) * 1e8 print(k, a, dq) model[coord] -= 1e-8
def GPPdiff(Cf, d, x): """Gross Primary Production derivative (GPP) function ----------------------------------------------------- Takes a foliar carbon (Cf) value, a DataClass (d) and a time step (x) and returns the estimated value for the derivative of the GPP of the forest. Uses the module ad for automatic differentiation. """ Cf = ad.adnumber(Cf) L = Cf / 111. q = d.a_3 - d.a_4 gc = (abs(d.phi_d[x]))**(d.a_10) / (0.5*d.T_range[x] + d.a_6*d.R_tot[x]) p = ((d.a_1*d.N*L) / gc)*np.exp(d.a_8*d.T_max[x]) Ci = 0.5*(d.C_a + q - p + np.sqrt((d.C_a + q - p)**2 \ -4*(d.C_a*q - p*d.a_3))) E0 = (d.a_7*L**2) / (L**2 + d.a_9) delta = -0.408*np.cos(((360*(d.D[x] + 10)*np.pi) / (365*180))) s = 24*np.arccos(( - np.tan(d.bigdelta)*np.tan(delta))) / np.pi GPP = (E0*d.I[x]*gc*(d.C_a - Ci)*(d.a_2*s + d.a_5)) / \ (E0*d.I[x] + gc*(d.C_a - Ci)) return GPP.d(Cf)
def rpc_affine_approximation(rpc, p): """ Compute the first order Taylor approximation of an RPC projection function. Args: rpc (rpc_model.RPCModel): RPC camera model p (3-tuple of float): lon, lat, z coordinates Return: array of shape (3, 4) representing the affine camera matrix equal to the first order Taylor approximation of the RPC projection function at point p. """ p = ad.adnumber(p) q = rpc.projection(*p) J = ad.jacobian(q, p) A = np.zeros((3, 4)) A[:2, :3] = J A[:2, 3] = np.array(q) - np.dot(J, p) A[2, 3] = 1 return A
def test_lsig(): f = 100.0 t = 0.5 x= 98.0 a = 0.3 b = 0.7 r = -0.45 v = 2.3 df = adnumber(100.0) dt = adnumber(0.5) dx= adnumber(98.0) da = adnumber(0.3) db = adnumber(0.7) dr = adnumber(-0.45) dv = adnumber(2.3) s = lsig(f,x,t,a,b,r,v) dsdf = lsig(df,x,t,a,b,r,v).d(df) dsdx = lsig(f,dx,t,a,b,r,v).d(dx) dsdt = lsig(f,x,dt,a,b,r,v).d(dt) dsda = lsig(f,x,t,da,b,r,v).d(da) dsdb = lsig(f,x,t,a,db,r,v).d(db) dsdr = lsig(f,x,t,a,b,dr,v).d(dr) dsdv = lsig(f,x,t,a,b,r,dv).d(dv) inc = 1e-5 dsdf_fd = (lsig(f+inc,x,t,a,b,r,v)-lsig(f-inc,x,t,a,b,r,v))/2/inc dsdx_fd = (lsig(f,x+inc,t,a,b,r,v)-lsig(f,x-inc,t,a,b,r,v))/2/inc dsdt_fd = (lsig(f,x,t+inc,a,b,r,v)-lsig(f,x,t-inc,a,b,r,v))/2/inc dsda_fd = (lsig(f,x,t,a+inc,b,r,v)-lsig(f,x,t,a-inc,b,r,v))/2/inc dsdb_fd = (lsig(f,x,t,a,b+inc,r,v)-lsig(f,x,t,a,b-inc,r,v))/2/inc dsdr_fd = (lsig(f,x,t,a,b,r+inc,v)-lsig(f,x,t,a,b,r-inc,v))/2/inc dsdv_fd = (lsig(f,x,t,a,b,r,v+inc)-lsig(f,x,t,a,b,r,v-inc))/2/inc print("dfs:" + str([dsdf, dsdf_fd])) print("dxs:" + str([dsdx, dsdx_fd])) print("dts:" + str([dsdt, dsdt_fd])) print("das:" + str([dsda, dsda_fd])) print("dbs:" + str([dsdb, dsdb_fd])) print("drs:" + str([dsdr, dsdr_fd])) print("dvs:" + str([dsdv, dsdv_fd]))
def _test_spline(klass): y = ad.adnumber([1.18, 0.13, -0.01, -0.26, -0.02, 0.41, 0., 0., 0., 0., 0.]) K = len(y) x = np.array([0.0025, 0.00572989, 0.01313264, 0.03009941, 0.06898648, 0.15811388, 0.36238983, 0.83058102, 1.90365394, 4.36308829, 10.]) # y = ad.adnumber(np.random.normal(size=20)) s = [0.0025, 0.00057604, 0.00070876, 0.00087207, 0.00107301, 0.00132025, 0.00162445, 0.00199875, 0.0024593, 0.00302595, 0.00372318, 0.00458105, 0.00563659, 0.00693535, 0.00853335, 0.01049956, 0.01291881, 0.0158955, 0.01955805, 0.02406452, 0.02960933, 0.03643176, 0.04482617, 0.05515478, 0.06786325, 0.08349993, 0.10273954, 0.12641224, 0.15553947, 0.19137805, 0.23547436, 0.28973111, 0.3564894, 0.43862979, 0.53969653, 0.66405052, 0.8170575, 1.00531954, 1.23695991, 1.52197361, 1.87265866] sp = klass(x, y) pts = sp.eval(s) for i in [4, 5, 6]: y2 = np.array(y).astype('float') y2[i] += EPS sp2 = klass(x, y2) pts2 = sp2.eval(s) for p1, p2 in zip(pts, pts2): g1 = p1.d(y[i]) g2 = (p2 - p1.x) / EPS print(i, g1, g2)
def test_d(im): eps = 1e-8 model = im.model K = len(model[:]) model[:] = ad.adnumber(np.random.normal(0, .1, size=K)) y = model[:].copy() im.model = model im.E_step() im.Q() e1 = im.emission I = np.eye(K) for k in range(K): aa = [float(_) for _ in y] aa += eps * I[k] model[:] = aa im.model = model im.Q() e2 = im.emission for i in range(e1.shape[0]): for j in range(e1.shape[1]): d1 = e1[i, j].d(y[k]) d2 = (e2[i,j] - float(e1[i,j])) / eps
def _prepare_x(self, x): return [ad.adnumber(xx, tag=i) for i, xx in enumerate(x)]
__author__ = 'Sebastian Sanchez Perez-Moreno. Email: [email protected]' from ad import adnumber from ad.admath import * x = adnumber([1, -2, 4]) p = 9 * (x[1] * x[2] * x[0]) / (x[1] + x[2] + x[0]) print p.d(x[0])
def return_cosm1(self, c): eta = c(self.wing.planform.eta).to("dimensionless").magnitude return hstack([adnumber(1e-10), 1-array(cos(eta[1:]*pi/2))])
def assign(self, value, units=None): if not units == None: self.setUnits(units) self.value._magnitude = value self.ADvalue = ad.adnumber(value, self.name) self.checkValue() # a weak version of integrity()
import mpmath as mp import numpy as np import ad import itertools as it import etjj def test_autodiff(): Ninv = [ad_iv(x) for x in [0.01, 0.05, 0.2, 1.0]] ts = [0., 0.1, 0.5, 0.6, np.inf] sps = np.cumsum([ad_iv(0.5)**2 for _ in range(10)]).tolist() N = 10 etjj_below = etjj.etjj_below(N, Ninv, ts, sps) for x in etjj_below.flat: for v in Ninv + sps: x.d(v) x.d2(v) print(v) if __name__ == "__main__": Ninv = ad.adnumber(np.array([0.1, 0.2, 0.001, 0.003])) sp = ad.adnumber([0.01, 0.02, 0.03, 0.4, 0.6, 0.5, 0.6, 0.8, 1.01]) x = etjj.etjj_above(10, Ninv, np.array([0., 0.1, 0.2, 1.0, np.inf]), sp)
def vectorlink(c): "linked vector function" if isinstance(c[y], ADV): return np.array(c[y])+adnumber([1, 2, 3]) return c[y]+np.array([1, 2, 3])
def vectorlink(c): "linked vector function" if isinstance(c[y], ADV): return np.array(c[y]) + adnumber([1, 2, 3]) return c[y] + np.array([1, 2, 3])
def residual(self, state): ad_state = ad.adnumber(state) est_msr = self.gen_meas(ad_state) resid = self.value - est_msr.real H_tilde = ad.jacobian(est_msr, ad_state) return resid, H_tilde
def dsumerrs(ps): dr = adnumber(ps[0]) dvv = adnumber(ps[1]) return np.array([sumerrs([dr, ps[1]]).d(dr), sumerrs([ps[0], dvv]).d(dvv)])
import scipy.stats import scipy.optimize matplotlib.rcParams['ytick.labelsize'] = 16 matplotlib.rcParams['xtick.labelsize'] = 16 matplotlib.rcParams['axes.labelsize'] = 20 matplotlib.rcParams['legend.fontsize'] = 20 matplotlib.rcParams['text.usetex'] = True # ## Simple examples # In[ ]: x = adnumber(5.0) print "x = ", x # In[ ]: y = x**2 print "y = x**2" print "y = ", y print "dy/dx = ", y.d(x) print "d^2y/dx^2 = ", y.d2(x) # In[ ]:
def neutron_chemical_potential(n_n, n_p): n_n = adnumber(n_n) return nucleon_energy_density(n_n, n_p).d(n_n)
def proton_chemical_potential(n_n, n_p): n_p = adnumber(n_p) return nucleon_energy_density(n_n, n_p).d(n_p)
def derr(r, vv, x, sig): dr=adnumber(r) dvv=adnumber(vv) return np.array([err(dr, vv, x, sig).d(dr), err(r, dvv, x, sig).d(dvv)])
def test_inference(): logging.basicConfig(level=logging.DEBUG) hs = [ 0, 0.002, 0.0024992427075529156, 0.0031231070556282147, 0.0039027012668429368, 0.0048768988404573679, 0.0060942769312431738, 0.0076155385891087321, 0.0095165396414589112, 0.011892071150027212, 0.014860586049702963, 0.018570105657341358, 0.023205600571298769, 0.028998214001102113, 0.036236787437156658, 0.045282263373729439, 0.0565856832591419, 0.070710678118654752, 0.088361573317084705, 0.11041850887031313, 0.1379813265364985, 0.15879323234898887, 0.22690003122622915, 0.28675095012241164, 0.35039604900931776, 0.4174620285802807, 0.48093344839252727, 0.54048403452772453, 0.58902987679112695, 0.63973400753929655, 0.6661845719884536, 0.68097444812291441, 0.69652310395210704, 0.71291262669986732, 0.73023918985303526, 0.74861647270557707, 0.76818018497781393, 0.7890941548490632, 0.81155867710242946, 0.8429182938518559, 0.88146343535942318, 0.92368486081866963, 0.97035848127888702, 1.0225351498208244, 1.1293598575982273, 1.2553186915845398, 1.468142830257521, 1.7982719448467761, 2.3740247153419043, 3.2719144602927757, 4.8068671176749671, np.inf ] s = [ 0.002, 0.00049924270755291556, 0.00062386434807529907, 0.00077959421121472213, 0.00097419757361443156, 0.0012173780907858058, 0.0015212616578655584, 0.0019010010523501783, 0.0023755315085683005, 0.0029685148996757508, 0.0037095196076383959, 0.0046354949139574102, 0.0057926134298033442, 0.0072385734360545448, 0.0090454759365727819, 0.011303419885412461, 0.014124994859512852, 0.017650895198429953, 0.022056935553228421, 0.027562817666185374, 0.034443085525912243, 0.043040815163128798, 0.0537847217117913, 0.067210536757978667, 0.083987721931547688, 0.10495285078070138, 0.13115132347527858, 0.16388949439075173, 0.20479981185031038, 0.25592221813754867, 0.31980586869051764, 0.39963624257870101, 0.49939398246933342 ] K = 10 model = smcpp.model.SMCModel(s, np.logspace(np.log10(.01), np.log10(3.), K), smcpp.spline.CubicSpline) n = 30 fakeobs = [[1, -1, 0, 0], [1, 1, 0, 0], [10, 0, 0, 0], [10, -1, 0, 0], [200000, 0, 0, n - 2], [1, 1, n - 4, n - 2]] fakeobs *= 20 im = smcpp._smcpp.PyOnePopInferenceManager( n - 2, np.array([fakeobs] * 40, dtype=np.int32), hs) model[:] = [ ad.adnumber(x, i) for i, x in enumerate([ 0.002676322760403453, -0.01051998744897540, 0.006727140517177145, 0.003133368489467686, -0.02302979056648467, 0.002636809760679317, 0.001992156262601299, 0.004958301100037235, 0.003199704865436452, 0.005012987257524974 ]) ] print(model[:]) print(model.stepwise_values()) im.model = model im.theta = 0.0025000000000000001 im.rho = 0.0031206103977654887 im.E_step() q0 = im.Q() r0 = model.regularizer() for k in range(K): dq = q0.d(model[k]) dr = r0.d(model[k]) model[k] += 1e-8 im.model = model q1 = im.Q() r1 = model.regularizer() a = float(q1 - q0) * 1e8 b = float(r1 - r0) * 1e8 print(k, a, dq) print(k, b, dr) model[k] -= 1e-8
def tsne(X = Math.array([]), no_dims = 2, initial_dims = 50, perplexity = 30.0, D = None): """Runs t-SNE on the dataset in the NxD array X to reduce its dimensionality to no_dims dimensions. The syntaxis of the function is Y = tsne.tsne(X, no_dims, perplexity), where X is an NxD NumPy array.""" # Check inputs if X.dtype != "float64": print "Error: array X should have type float64."; return -1; #if no_dims.__class__ != "<type 'int'>": # doesn't work yet! # print "Error: number of dimensions should be an integer."; # return -1; # Initialize variables # X = pca(X, initial_dims).real; (n, d) = X.shape; max_iter = 1000; initial_momentum = 0.5; final_momentum = 0.8; eta = 500; min_gain = 0.01; Y = Math.array(list([Math.cos(a), Math.sin(a)] for a in Math.arange(0.0, 2.0 * Math.pi, 2.0 * Math.pi / n))) print Y #Y = Math.random.randn(n, no_dims); Y_ad = Math.array(list(list(adnumber(e) for e in row) for row in Y)) print Y - Y_ad print "Y_ad!" dY = Math.zeros((n, no_dims)); iY = Math.zeros((n, no_dims)); gains = Math.ones((n, no_dims)); # Compute P-values P = x2p(X, 1e-5, perplexity, D); P = P + Math.transpose(P); P = P / Math.sum(P); P = P * 4; # early exaggeration P = Math.maximum(P, 1e-12); ad_0 = adnumber(0) ad_eps = adnumber(1e-12) fd_eps = 0.00001 # Run iterations for iter in xrange(max_iter): Y_p = Math.array(Y) Y_p[1,0] += fd_eps # Compute pairwise affinities sum_Y = Math.sum(Math.square(Y), 1); sum_Y_p = Math.sum(Math.square(Y_p), 1); sum_Y_ad = Math.sum(Math.square(Y_ad), 1); # print sum_Y # print sum_Y_ad print sum_Y - sum_Y_ad print "sum_Y_ad!" num = 1 / (1 + Math.add(Math.add(-2 * Math.dot(Y, Y.T), sum_Y).T, sum_Y)); num_p = 1 / (1 + Math.add(Math.add(-2 * Math.dot(Y_p, Y_p.T), sum_Y_p).T, sum_Y_p)); num_ad = 1 / (1 + Math.add(Math.add(-2 * Math.dot(Y_ad, Y_ad.T), sum_Y_ad).T, sum_Y_ad)); num[range(n), range(n)] = 0 num_p[range(n), range(n)] = 0 num_ad[range(n), range(n)] = ad_0 # print num # print num_ad print num - num_ad print "num_ad!" Q = num / Math.sum(num) Q_p = num_p / Math.sum(num_p) Q_ad = num_ad / Math.sum(num_ad) Q = Math.maximum(Q, 1e-12); Q_p = Math.maximum(Q_p, 1e-12); Q_ad = Math.maximum(Q_ad, ad_eps) print Q - Q_ad print "Q_ad!" # C_ad = Math.sum(P * Math.log(P / Q_ad)); C_ad = Math.sum(P * Math.array(log(P / Q_ad))); C = Math.sum(P * Math.log(P / Q)); C_p = Math.sum(P * Math.log(P / Q_p)); print C - C_ad print "C_ad!" print "finite-difference approximation: ", (C_p - C) / fd_eps dY_ad = [] for i in xrange(n): v = [] for j in xrange(no_dims): v.append(C_ad.d(Y_ad[i,j])) dY_ad.append(v) dY_ad = Math.array(dY_ad) print "our autodiff gradients: " print dY_ad # Compute gradient PQ = P - Q; for i in range(n): dY[i,:] = 4.0 * Math.sum(Math.tile(PQ[:,i] * num[:,i], (no_dims, 1)).T * (Y[i,:] - Y), 0); print "their gradients:" print dY print dY / dY_ad print "gradient!" exit(0); # Perform the update if iter < 20: momentum = initial_momentum else: momentum = final_momentum gains = (gains + 0.2) * ((dY > 0) != (iY > 0)) + (gains * 0.8) * ((dY > 0) == (iY > 0)); gains[gains < min_gain] = min_gain; iY = momentum * iY - eta * (gains * dY); Y = Y + iY; Y = Y - Math.tile(Math.mean(Y, 0), (n, 1)); # Compute current value of cost function if (iter + 1) % 10 == 0: C = Math.sum(P * Math.log(P / Q)); print "Iteration ", (iter + 1), ": error is ", C json.dump(list(list(v) for v in Y), file("web/out.tmp", 'w')) shutil.move("web/out.tmp", "web/out.json") # Stop lying about P-values if iter == 100: P = P / 4; # Return solution return Y;
def adnumber(val, name): return ad.adnumber(val, name)