Skip to content

Commit

Permalink
#3 renamed multi-output GP/TP moment transforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobnzw committed Dec 11, 2018
1 parent 0be924b commit c266ad6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
18 changes: 13 additions & 5 deletions ssmtoybox/bq/bqmtran.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,14 @@ def _covariance(self, weights, fcn_evals, mean_out):
return fcn_evals.dot(weights).dot(fcn_evals.T) - np.outer(mean_out, mean_out.T) + emv


class GPQMO(BQTransform):
"""
Warning: EXPERIMENTAL!
Moment transforms based on Bayesian quadrature with multi-output GP/TP integrand models.
"""


class MultiOutputGaussianProcessTransform(BQTransform):
def __init__(self, dim_in, dim_out, kern_par, kern_str='rbf', point_str='ut', point_par=None, estimate_par=False):
"""
Expand All @@ -442,7 +449,8 @@ def __init__(self, dim_in, dim_out, kern_par, kern_str='rbf', point_str='ut', po
when provided with the same parameters.
"""
super(GPQMO, self).__init__(dim_in, dim_out, kern_par, 'gp-mo', kern_str, point_str, point_par, estimate_par)
super(MultiOutputGaussianProcessTransform, self).__init__(dim_in, dim_out, kern_par, 'gp-mo', kern_str,
point_str, point_par, estimate_par)

# output dimension (number of outputs)
self.e = dim_out
Expand Down Expand Up @@ -515,13 +523,13 @@ def _integral_variance(self, points, kern_par):
pass


class TPQMO(BQTransform):
class MultiOutputStudentTProcessTransform(BQTransform):

def __init__(self, dim_in, dim_out, kern_par, kern_str='rbf', point_str='ut', point_par=None, estimate_par=False,
nu=3.0):

super(TPQMO, self).__init__(dim_in, dim_out, kern_par, 'tp-mo', kern_str, point_str, point_par, estimate_par,
nu=nu)
super(MultiOutputStudentTProcessTransform, self).__init__(dim_in, dim_out, kern_par, 'tp-mo', kern_str,
point_str, point_par, estimate_par, nu=nu)

# output dimension (number of outputs)
self.e = dim_out
Expand Down
12 changes: 7 additions & 5 deletions ssmtoybox/ssinf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from scipy.stats import multivariate_normal
from numpy import newaxis as na
from ssmtoybox.ssmod import TransitionModel, MeasurementModel
from ssmtoybox.bq.bqmtran import GaussianProcessTransform, GPQMO, StudentTProcessTransform, TPQMO, BayesSardTransform
from ssmtoybox.bq.bqmtran import GaussianProcessTransform, MultiOutputGaussianProcessTransform, StudentTProcessTransform, MultiOutputStudentTProcessTransform, BayesSardTransform
from ssmtoybox.mtran import MomentTransform, LinearizationTransform, TaylorGPQDTransform, \
SphericalRadialTransform, UnscentedTransform, GaussHermiteTransform, FullySymmetricStudentTransform, \
TruncatedSphericalRadialTransform, TruncatedUnscentedTransform, TruncatedGaussHermiteTransform
Expand Down Expand Up @@ -952,8 +952,8 @@ class MultiOutputGaussianProcessKalman(GaussianInference):
"""

def __init__(self, dyn, obs, kern_par_dyn, kern_par_obs, kernel='rbf', points='ut', point_hyp=None):
t_dyn = GPQMO(dyn.dim_in, dyn.dim_state, kern_par_dyn, kernel, points, point_hyp)
t_obs = GPQMO(obs.dim_in, obs.dim_out, kern_par_obs, kernel, points, point_hyp)
t_dyn = MultiOutputGaussianProcessTransform(dyn.dim_in, dyn.dim_state, kern_par_dyn, kernel, points, point_hyp)
t_obs = MultiOutputGaussianProcessTransform(obs.dim_in, obs.dim_out, kern_par_obs, kernel, points, point_hyp)
super(MultiOutputGaussianProcessKalman, self).__init__(dyn, obs, t_dyn, t_obs)


Expand Down Expand Up @@ -1013,8 +1013,10 @@ def __init__(self, dyn, obs, kern_par_dyn, kern_par_obs, point_par=None, dof=4.0
point_par_dyn.update({'dof': q_dof})
point_par_obs.update({'dof': r_dof})

t_dyn = TPQMO(dyn.dim_in, dyn.dim_state, kern_par_dyn, 'rbf-student', 'fs', point_par_dyn, nu=dof_tp)
t_obs = TPQMO(obs.dim_in, obs.dim_out, kern_par_obs, 'rbf-student', 'fs', point_par_obs, nu=dof_tp)
t_dyn = MultiOutputStudentTProcessTransform(dyn.dim_in, dyn.dim_state, kern_par_dyn,
'rbf-student', 'fs', point_par_dyn, nu=dof_tp)
t_obs = MultiOutputStudentTProcessTransform(obs.dim_in, obs.dim_out, kern_par_obs,
'rbf-student', 'fs', point_par_obs, nu=dof_tp)
super(MultiOutputStudentProcessStudent, self).__init__(dyn, obs, t_dyn, t_obs)


Expand Down
14 changes: 7 additions & 7 deletions ssmtoybox/tests/test_bqmtran.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import numpy.linalg as la

from ssmtoybox.bq.bqmtran import GaussianProcessTransform, GPQMO, BayesSardTransform
from ssmtoybox.bq.bqmtran import GaussianProcessTransform, MultiOutputGaussianProcessTransform, BayesSardTransform
from ssmtoybox.ssmod import UNGMTransition, Pendulum2DTransition, CoordinatedTurnTransition, ReentryVehicle2DTransition
from ssmtoybox.utils import GaussRV

Expand Down Expand Up @@ -117,7 +117,7 @@ def test_weights_rbf(self):
dim_in, dim_out = 1, 1
khyp = np.array([[1, 3]])
phyp = {'kappa': 0.0, 'alpha': 1.0}
tf = GPQMO(dim_in, dim_out, khyp, point_par=phyp)
tf = MultiOutputGaussianProcessTransform(dim_in, dim_out, khyp, point_par=phyp)
wm, wc, wcc = tf.wm, tf.Wc, tf.Wcc
self.assertTrue(np.allclose(wc, wc.swapaxes(0, 1).swapaxes(2, 3)), "Covariance weight matrix not symmetric.")

Expand All @@ -127,7 +127,7 @@ def test_weights_rbf(self):
[1, 2, 2, 2, 2],
[1, 3, 3, 3, 3]])
phyp = {'kappa': 0.0, 'alpha': 1.0}
tf = GPQMO(dim_in, dim_out, khyp, point_par=phyp)
tf = MultiOutputGaussianProcessTransform(dim_in, dim_out, khyp, point_par=phyp)
wm, wc, wcc = tf.wm, tf.Wc, tf.Wcc
self.assertTrue(np.allclose(wc, wc.swapaxes(0, 1).swapaxes(2, 3)), "Covariance weight matrix not symmetric.")

Expand All @@ -136,7 +136,7 @@ def test_apply(self):
f = dyn.dyn_eval
dim_in, dim_out = dyn.dim_in, dyn.dim_state
ker_par = np.hstack((np.ones((dim_out, 1)), 3*np.ones((dim_out, dim_in))))
tf = GPQMO(dim_in, dim_out, ker_par)
tf = MultiOutputGaussianProcessTransform(dim_in, dim_out, ker_par)
mean, cov = np.zeros(dim_in, ), np.eye(dim_in)
tmean, tcov, tccov = tf.apply(f, mean, cov, np.atleast_1d(1.0))
print("Transformed moments\nmean: {}\ncov: {}\nccov: {}".format(tmean, tcov, tccov))
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_single_vs_multi_output(self):

# multi-output GPQ
ker_par_mo = np.hstack((np.ones((dim_out, 1)), 25 * np.ones((dim_out, dim_in))))
tf_mo = GPQMO(dim_in, dim_out, ker_par_mo)
tf_mo = MultiOutputGaussianProcessTransform(dim_in, dim_out, ker_par_mo)

# transformed moments
# FIXME: transformed covariances different
Expand All @@ -195,7 +195,7 @@ def test_optimize_1D(self):
dim_in, dim_out = dyn.dim_in, dyn.dim_state

par0 = 1 + np.random.rand(dim_out, dim_in + 1)
tf = GPQMO(dim_in, dim_out, par0)
tf = MultiOutputGaussianProcessTransform(dim_in, dim_out, par0)

# use sampled system state trajectory to create training data
fy = np.zeros((dim_out, steps))
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_optimize(self):

# par0 = np.hstack((np.ones((dim_out, 1)), 5*np.ones((dim_out, dim_in+1))))
par0 = 10*np.ones((dim_out, dim_in+1))
tf = GPQMO(dim_in, dim_out, par0)
tf = MultiOutputGaussianProcessTransform(dim_in, dim_out, par0)

# use sampled system state trajectory to create training data
fy = np.zeros((dim_out, steps))
Expand Down

0 comments on commit c266ad6

Please sign in to comment.