<s> '''<STR_LIT>''' <EOL> import unittest <EOL> import numpy as np , GPy <EOL> from GPy . core . parameterization . variational import NormalPosterior <EOL> class Test ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> self . N = <NUM_LIT:20> <EOL> self . N_new = <NUM_LIT:50> <EOL> self . D = <NUM_LIT:1> <EOL> self . X = np . random . uniform ( - <NUM_LIT> , <NUM_LIT> , ( self . N , <NUM_LIT:1> ) ) <EOL> self . Y = np . sin ( self . X ) + np . random . randn ( self . N , self . D ) * <NUM_LIT> <EOL> self . X_new = np . random . uniform ( - <NUM_LIT> , <NUM_LIT> , ( self . N_new , <NUM_LIT:1> ) ) <EOL> def test_setxy_bgplvm ( self ) : <EOL> k = GPy . kern . RBF ( <NUM_LIT:1> ) <EOL> m = GPy . models . BayesianGPLVM ( self . Y , <NUM_LIT:1> , kernel = k ) <EOL> mu , var = m . predict ( m . X ) <EOL> X = m . X . copy ( ) <EOL> Xnew = NormalPosterior ( m . X . mean [ : <NUM_LIT:10> ] . copy ( ) , m . X . variance [ : <NUM_LIT:10> ] . copy ( ) ) <EOL> m . set_XY ( Xnew , m . Y [ : <NUM_LIT:10> ] ) <EOL> assert ( m . checkgrad ( ) ) <EOL> m . set_XY ( X , self . Y ) <EOL> mu2 , var2 = m . predict ( m . X ) <EOL> np . testing . assert_allclose ( mu , mu2 ) <EOL> np . testing . assert_allclose ( var , var2 ) <EOL> def test_setxy_gplvm ( self ) : <EOL> k = GPy . kern . RBF ( <NUM_LIT:1> ) <EOL> m = GPy . models . GPLVM ( self . Y , <NUM_LIT:1> , kernel = k ) <EOL> mu , var = m . predict ( m . X ) <EOL> X = m . X . copy ( ) <EOL> Xnew = X [ : <NUM_LIT:10> ] . copy ( ) <EOL> m . set_XY ( Xnew , m . Y [ : <NUM_LIT:10> ] ) <EOL> assert ( m . checkgrad ( ) ) <EOL> m . set_XY ( X , self . Y ) <EOL> mu2 , var2 = m . predict ( m . X ) <EOL> np . testing . assert_allclose ( mu , mu2 ) <EOL> np . testing . assert_allclose ( var , var2 ) <EOL> def test_setxy_gp ( self ) : <EOL> k = GPy . kern . RBF ( <NUM_LIT:1> ) <EOL> m = GPy . models . GPRegression ( self . X , self . Y , kernel = k ) <EOL> mu , var = m . predict ( m . X ) <EOL> X = m . X . copy ( ) <EOL> m . set_XY ( m . X [ : <NUM_LIT:10> ] , m . Y [ : <NUM_LIT:10> ] ) <EOL> assert ( m . checkgrad ( ) ) <EOL> m . set_XY ( X , self . Y ) <EOL> mu2 , var2 = m . predict ( m . X ) <EOL> np . testing . assert_allclose ( mu , mu2 ) <EOL> np . testing . assert_allclose ( var , var2 ) <EOL> def test_mean_function ( self ) : <EOL> from GPy . core . parameterization . param import Param <EOL> from GPy . core . mapping import Mapping <EOL> class Parabola ( Mapping ) : <EOL> def __init__ ( self , variance , degree = <NUM_LIT:2> , name = '<STR_LIT>' ) : <EOL> super ( Parabola , self ) . __init__ ( <NUM_LIT:1> , <NUM_LIT:1> , name ) <EOL> self . variance = Param ( '<STR_LIT>' , np . ones ( degree + <NUM_LIT:1> ) * variance ) <EOL> self . degree = degree <EOL> self . link_parameter ( self . variance ) <EOL> def f ( self , X ) : <EOL> p = self . variance [ <NUM_LIT:0> ] * np . ones ( X . shape ) <EOL> for i in range ( <NUM_LIT:1> , self . degree + <NUM_LIT:1> ) : <EOL> p += self . variance [ i ] * X ** ( i ) <EOL> return p <EOL> def gradients_X ( self , dL_dF , X ) : <EOL> grad = np . zeros ( X . shape ) <EOL> for i in range ( <NUM_LIT:1> , self . degree + <NUM_LIT:1> ) : <EOL> grad += ( i ) * self . variance [ i ] * X ** ( i - <NUM_LIT:1> ) <EOL> return grad <EOL> def update_gradients ( self , dL_dF , X ) : <EOL> for i in range ( self . degree + <NUM_LIT:1> ) : <EOL> self . variance . gradient [ i ] = ( dL_dF * X ** ( i ) ) . sum ( <NUM_LIT:0> ) <EOL> X = np . linspace ( - <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:100> ) [ : , None ] <EOL> k = GPy . kern . RBF ( <NUM_LIT:1> ) <EOL> k . randomize ( ) <EOL> p = Parabola ( <NUM_LIT> ) <EOL> p . randomize ( ) <EOL> Y = p . f ( X ) + np . random . multivariate_normal ( np . zeros ( X . shape [ <NUM_LIT:0> ] ) , k . K ( X ) + np . eye ( X . shape [ <NUM_LIT:0> ] ) * <NUM_LIT> ) [ : , None ] + np . random . normal ( <NUM_LIT:0> , <NUM_LIT> , ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> m = GPy . models . GPRegression ( X , Y , mean_function = p ) <EOL> m . randomize ( ) <EOL> assert ( m . checkgrad ( ) ) <EOL> _ = m . predict ( m . X ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
