def __init__(self, x_kernel_pars=None, g_kernel_pars=None, gammas=None, sigmas=None, As=None, data_time=None, data_Y=None, x_kernel_type="sqexp"): if isinstance(sigmas, np.ndarray): _sigmas = [] for i, sval in enumerate(sigmas): sigpar = Parameter("sigma_{}".format(i)) sigpar.value = sval _sigmas.append(sigpar) self._sigmas = ParameterCollection(_sigmas) if isinstance(gammas, np.ndarray): _gammas = [] for i, gval in enumerate(gammas): gpar = Parameter("gamma_{}".format(i)) gpar.value = gval _gammas.append(gpar) self._gammas = ParameterCollection(_gammas) if all(isinstance(kp, np.ndarray) for kp in x_kernel_pars): if x_kernel_type == "sqexp": _x_kernels = [] for kp in x_kernel_pars: _x_kernels.append( GradientMultioutputKernel.SquareExponKernel(kp)) self._x_kernels = _x_kernels if all(isinstance(kp, np.ndarray) for kp in g_kernel_pars): _g_kernels = [] for kp in g_kernel_pars: _g_kernels.append(Kernel.SquareExponKernel(kp)) self._g_kernels = _g_kernels self._As = np.array(As) self.R = len(As) - 1 if data_Y is not None: self.data = Data(data_time, data_Y) self.K = data_Y.shape[1] self.N = data_time.size try: self._store_gpg_covs() except: pass
def mlfm_setup(mod): from gpode.bayes import Parameter, ParameterCollection from lindod_src import _x_gp_pars K = 2 xkp = [] for k in range(K): p1 = Parameter("phi_{}_1".format(k), prior=("gamma", (4, 0.2)), proposal=("normal rw", 0.1)) p2 = Parameter("phi_{}_2".format(k), prior=("gamma", (4, 0.2)), proposal=("normal rw", 0.1)) phi_k = ParameterCollection([p1, p2], independent=True) xkp.append(phi_k) m = MGPLinearAdapGrad3(xkp) m.model_setup() m.data = mod.data m._X = mod.latent_X m._dXdt = mod._dXdt m._As = mod.As m._Gs = mod.Gs m._gammas = mod.gammas m._sigmas = mod.sigmas m._Lxxs = [] m._mdxs = [] m._dCds = [] for i, kern in enumerate(m._x_kernels): Lxx, mdx_x, Cdxdx_x = _x_gp_pars(kern.kpar.value(), kern, m.data.time, m._X[:, i]) m._Lxxs.append(Lxx) m._mdxs.append(mdx_x) m._dCds.append(Cdxdx_x) return m
import numpy as np from gpode.bayes import Parameter, ParameterCollection from gpode.kernels import Kernel p1 = Parameter("p1", prior=("gamma", (4, 0.2)), proposal=("normal rw", 0.1)) p2 = Parameter("p2", prior=("gamma", (4, 0.2)), proposal=("normal rw", 0.1)) phi_k = ParameterCollection([p1, p2], independent=True) for pname, par in phi_k.parameters.items(): par.value = par.prior.rvs() kse = Kernel.SquareExponKernel(kpar=phi_k) print(phi_k.value()) print(np.array(phi_k.proposal.rvs(phi_k.value())))
MLFM = lfm.MulLatentForceModel_adapgrad tt = np.linspace(.0, 5., 12) mathieudat = DataLoader.load("mathieu", 57, tt, [0.1, 0.1], a=1., h=.9) np.random.seed(3) K = 2 xkp = [] for k in range(K): p1 = Parameter("phi_{}_1".format(k), prior=("gamma", (4, 0.2)), proposal=("normal rw", 0.1)) p2 = Parameter("phi_{}_2".format(k), prior=("gamma", (4, 0.2)), proposal=("normal rw", 0.1)) phi_k = ParameterCollection([p1, p2], independent=True) xkp.append(phi_k) gkp = [] for r in range(1): p1 = Parameter("psi_{}_1".format(r + 1), prior=("gamma", (4, 4.)), proposal=("normal rw", 0.1)) p2 = Parameter("psi_{}_2".format(r + 1), prior=("gamma", (4, 4.)), proposal=("normal rw", 0.1)) psi_r = ParameterCollection([p1, p2], independent=True) gkp.append(psi_r) # Make the obs. noise parameters sigmas = [
class VariationalMLFM: def __init__(self, x_kernel_pars=None, g_kernel_pars=None, gammas=None, sigmas=None, As=None, data_time=None, data_Y=None, x_kernel_type="sqexp"): if isinstance(sigmas, np.ndarray): _sigmas = [] for i, sval in enumerate(sigmas): sigpar = Parameter("sigma_{}".format(i)) sigpar.value = sval _sigmas.append(sigpar) self._sigmas = ParameterCollection(_sigmas) if isinstance(gammas, np.ndarray): _gammas = [] for i, gval in enumerate(gammas): gpar = Parameter("gamma_{}".format(i)) gpar.value = gval _gammas.append(gpar) self._gammas = ParameterCollection(_gammas) if all(isinstance(kp, np.ndarray) for kp in x_kernel_pars): if x_kernel_type == "sqexp": _x_kernels = [] for kp in x_kernel_pars: _x_kernels.append( GradientMultioutputKernel.SquareExponKernel(kp)) self._x_kernels = _x_kernels if all(isinstance(kp, np.ndarray) for kp in g_kernel_pars): _g_kernels = [] for kp in g_kernel_pars: _g_kernels.append(Kernel.SquareExponKernel(kp)) self._g_kernels = _g_kernels self._As = np.array(As) self.R = len(As) - 1 if data_Y is not None: self.data = Data(data_time, data_Y) self.K = data_Y.shape[1] self.N = data_time.size try: self._store_gpg_covs() except: pass def _parse_component_k_for_g(self, k, mux, Sxx): N = self.N I = np.diag(np.ones(N)) Skinv = _back_sub(self.S_chol[k], I) Pk = np.dot(self.Cxdx[k].T, _back_sub(self.Lxx[k], I)) mk, cov_invk = _parse_component_k_for_g(k, self._As, mux, Sxx, Skinv, Pk, N) """ print(mk) def _obj_func(g): G = np.concatenate((np.ones(self.N), g)).reshape(self.R+1, self.N) return _comp_k(k, G, Skinv, self._As, Pk, mux, Sxx, N) from scipy.optimize import minimize res = minimize(_obj_func, np.ones(N*self.R)) print(res.x) assert(np.all(mk == res.x)) """ return mk, cov_invk def _parse_component_k_for_x(self, k, mug, Sgg): N = self.N I = np.diag(np.ones(N)) Skinv = _back_sub(self.S_chol[k], I) Pk = np.dot(self.Cxdx[k].T, _back_sub(self.Lxx[k], I)) return hfunc(k, Skinv, Pk, mug, Sgg, self._As, self.N, self.K, self.R) """ sigmas = self._sigmas.value() def _obj_func(x): X = x.reshape(self.K, self.N) val1 = _comp_k_for_x(k, X, Skinv, self._As, Pk, mug, Sgg, self.N, self.R) val2 = 0 # val2 = np.sum([norm.logpdf(self.data.Y[:, k], loc=X[k, :], scale=sigmas[k]) # for k in range(self.K)]) return -(val1+val2) x0 = self.data.Y.T.ravel() from scipy.optimize import minimize res = minimize(_obj_func, x0) print("=======================") print(np.linalg.inv(res.hess_inv)) print("=======================") # print(_obj_func(np.zeros(res.x.size)) <= res.fun) return res.x, np.linalg.inv(res.hess_inv) """ def _store_gpdx_covs(self): _store_gpdx_covs(self) def _store_gpg_covs(self): _store_gpg_covs(self) def _get_g_conditional(self, mux, Sxx): ms = [] cinvs = [] for k in range(self.K): try: m, ci = self._parse_component_k_for_g(k, mux, Sxx) ms.append(m) cinvs.append(ci) except: pass ci_prior = block_diag(*(cinv for cinv in self.Cgg_inv)) mean_prior = np.zeros(ci_prior.shape[0]) print(np.linalg.norm(cinvs[0])) print(np.linalg.norm(ci_prior)) ms.append(mean_prior) cinvs.append(ci_prior) mean, cov = _prod_norm_pars(ms, cinvs) return mean, cov def _get_x_conditional(self, mug, Sgg): ms = [] cinvs = [] for k in range(self.K): try: m, ci = self._parse_component_k_for_x(k, mug, Sgg) ms.append(m) cinvs.append(ci) except: print("Sad face!") pass ms.append(self.data.Y.T.ravel()) sigmas = self._sigmas.value() In = np.ones(self.N) ci_data = np.diag( np.concatenate((In / sigmas[0]**2, In / sigmas[1]**2))) cinvs.append(ci_data) C1inv = _back_sub(self.Lxx[0], np.diag(np.ones(self.N))) C2inv = _back_sub(self.Lxx[1], np.diag(np.ones(self.N))) ci = block_diag(C1inv, C2inv) mi = np.zeros(ci.shape[0]) ms.append(mi) cinvs.append(ci) mean, cov = _prod_norm_pars(ms, cinvs) return mean, cov def func(self, k, mg, Cgg): I = np.diag(np.ones(self.N)) Skinv = _back_sub(self.S_chol[k], I) Pk = np.dot(self.Cxdx[k].T, _back_sub(self.Lxx[k], I)) hfunc(k, Skinv, Pk, mg, Cgg, self._As, self.N, self.K, self.R) self._parse_component_k_for_x(k, mg, Cgg)
def test2(mod): # Tests the update step for _phi_k_mh_update from gpode.bayes import Parameter, ParameterCollection from lindod_src import MGPLinearAdapGrad3, _log_eq20_k, _x_gp_pars from scipy.optimize import minimize import matplotlib.pyplot as plt K = 2 xkp = [] for k in range(K): p1 = Parameter("phi_{}_1".format(k), prior=("gamma", (4, 0.2)), proposal=("normal rw", 0.1)) p2 = Parameter("phi_{}_2".format(k), prior=("gamma", (4, 0.2)), proposal=("normal rw", 0.1)) phi_k = ParameterCollection([p1, p2], independent=True) xkp.append(phi_k) m = MGPLinearAdapGrad3(xkp) m.model_setup() m.data = mod.data m._X = mod.latent_X m._dXdt = mod._dXdt m._As = mod.As m._Gs = mod.Gs m._gammas = mod.gammas rv_phi_0 = [] rv_phi_1 = [] N_SIM = 2000 BURN_IN = N_SIM / 4 for nt in range(N_SIM): for k in range(2): m._phi_k_mh_update(k) if nt > BURN_IN and nt % 10 == 0: rv_phi_0.append(m._x_kernels[0].kpar.value()) rv_phi_1.append(m._x_kernels[1].kpar.value()) rv_phi_0 = np.array(rv_phi_0) rv_phi_1 = np.array(rv_phi_1) fig1 = plt.figure() ax = fig1.add_subplot(121) ax.plot(rv_phi_0) ax = fig1.add_subplot(122) ax.plot(rv_phi_1) print(np.mean(rv_phi_0, axis=0)) print(np.mean(rv_phi_1, axis=0)) def objfunc(z): try: val = 0. zks = [z[:2], z[2:]] for k, zk in enumerate(zks): kern = m._x_kernels[k] kpar = kern.kpar xk = m._X[:, k] fk = m._dXdt(m._X, m._As, m._Gs)[:, k] Lxx, mdx_x, Cdxdx_x = _x_gp_pars(zk, kern, m.data.time, m._X[:, k]) val += _log_eq20_k(xk, fk, mdx_x, Lxx, Cdxdx_x, m._gammas[k], phi_k_val=zk, phi_k_prior=kpar.prior) return -val except: return np.inf res = minimize(objfunc, np.ones(4)) print(res.x[:2]) print(res.x[2:]) plt.show()