<s> import numpy as np <EOL> from . kern import Kern <EOL> from ... util . linalg import mdot <EOL> from ... util . decorators import silence_errors <EOL> from ... core . parameterization . param import Param <EOL> from paramz . transformations import Logexp <EOL> class Periodic ( Kern ) : <EOL> def __init__ ( self , input_dim , variance , lengthscale , period , n_freq , lower , upper , active_dims , name ) : <EOL> """<STR_LIT>""" <EOL> assert input_dim == <NUM_LIT:1> , "<STR_LIT>" <EOL> super ( Periodic , self ) . __init__ ( input_dim , active_dims , name ) <EOL> self . input_dim = input_dim <EOL> self . lower , self . upper = lower , upper <EOL> self . n_freq = n_freq <EOL> self . n_basis = <NUM_LIT:2> * n_freq <EOL> self . variance = Param ( '<STR_LIT>' , np . float64 ( variance ) , Logexp ( ) ) <EOL> self . lengthscale = Param ( '<STR_LIT>' , np . float64 ( lengthscale ) , Logexp ( ) ) <EOL> self . period = Param ( '<STR_LIT>' , np . float64 ( period ) , Logexp ( ) ) <EOL> self . link_parameters ( self . variance , self . lengthscale , self . period ) <EOL> def _cos ( self , alpha , omega , phase ) : <EOL> def f ( x ) : <EOL> return alpha * np . cos ( omega * x + phase ) <EOL> return f <EOL> @ silence_errors <EOL> def _cos_factorization ( self , alpha , omega , phase ) : <EOL> r1 = np . sum ( alpha * np . cos ( phase ) , axis = <NUM_LIT:1> ) [ : , None ] <EOL> r2 = np . sum ( alpha * np . sin ( phase ) , axis = <NUM_LIT:1> ) [ : , None ] <EOL> r = np . sqrt ( r1 ** <NUM_LIT:2> + r2 ** <NUM_LIT:2> ) <EOL> psi = np . where ( r1 != <NUM_LIT:0> , ( np . arctan ( r2 / r1 ) + ( r1 < <NUM_LIT:0.> ) * np . pi ) , np . arcsin ( r2 ) ) <EOL> return r , omega [ : , <NUM_LIT:0> : <NUM_LIT:1> ] , psi <EOL> @ silence_errors <EOL> def _int_computation ( self , r1 , omega1 , phi1 , r2 , omega2 , phi2 ) : <EOL> Gint1 = <NUM_LIT:1.> / ( omega1 + omega2 . T ) * ( np . sin ( ( omega1 + omega2 . T ) * self . upper + phi1 + phi2 . T ) - np . sin ( ( omega1 + omega2 . T ) * self . lower + phi1 + phi2 . T ) ) + <NUM_LIT:1.> / ( omega1 - omega2 . T ) * ( np . sin ( ( omega1 - omega2 . T ) * self . upper + phi1 - phi2 . T ) - np . sin ( ( omega1 - omega2 . T ) * self . lower + phi1 - phi2 . T ) ) <EOL> Gint2 = <NUM_LIT:1.> / ( omega1 + omega2 . T ) * ( np . sin ( ( omega1 + omega2 . T ) * self . upper + phi1 + phi2 . T ) - np . sin ( ( omega1 + omega2 . T ) * self . lower + phi1 + phi2 . T ) ) + np . cos ( phi1 - phi2 . T ) * ( self . upper - self . lower ) <EOL> Gint = np . dot ( r1 , r2 . T ) / <NUM_LIT:2> * np . where ( np . isnan ( Gint1 ) , Gint2 , Gint1 ) <EOL> return Gint <EOL> def K ( self , X , X2 = None ) : <EOL> FX = self . _cos ( self . basis_alpha [ None , : ] , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] ) ( X ) <EOL> if X2 is None : <EOL> FX2 = FX <EOL> else : <EOL> FX2 = self . _cos ( self . basis_alpha [ None , : ] , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] ) ( X2 ) <EOL> return mdot ( FX , self . Gi , FX2 . T ) <EOL> def Kdiag ( self , X ) : <EOL> return np . diag ( self . K ( X ) ) <EOL> class PeriodicExponential ( Periodic ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , input_dim = <NUM_LIT:1> , variance = <NUM_LIT:1.> , lengthscale = <NUM_LIT:1.> , period = <NUM_LIT> * np . pi , n_freq = <NUM_LIT:10> , lower = <NUM_LIT:0.> , upper = <NUM_LIT:4> * np . pi , active_dims = None , name = '<STR_LIT>' ) : <EOL> super ( PeriodicExponential , self ) . __init__ ( input_dim , variance , lengthscale , period , n_freq , lower , upper , active_dims , name ) <EOL> def parameters_changed ( self ) : <EOL> self . a = [ <NUM_LIT:1.> / self . lengthscale , <NUM_LIT:1.> ] <EOL> self . b = [ <NUM_LIT:1> ] <EOL> self . basis_alpha = np . ones ( ( self . n_basis , ) ) <EOL> self . basis_omega = ( <NUM_LIT:2> * np . pi * np . arange ( <NUM_LIT:1> , self . n_freq + <NUM_LIT:1> ) / self . period ) . repeat ( <NUM_LIT:2> ) <EOL> self . basis_phi = np . zeros ( self . n_freq * <NUM_LIT:2> ) <EOL> self . basis_phi [ : : <NUM_LIT:2> ] = - np . pi / <NUM_LIT:2> <EOL> self . G = self . Gram_matrix ( ) <EOL> self . Gi = np . linalg . inv ( self . G ) <EOL> def Gram_matrix ( self ) : <EOL> La = np . column_stack ( ( self . a [ <NUM_LIT:0> ] * np . ones ( ( self . n_basis , <NUM_LIT:1> ) ) , self . a [ <NUM_LIT:1> ] * self . basis_omega ) ) <EOL> Lo = np . column_stack ( ( self . basis_omega , self . basis_omega ) ) <EOL> Lp = np . column_stack ( ( self . basis_phi , self . basis_phi + np . pi / <NUM_LIT:2> ) ) <EOL> r , omega , phi = self . _cos_factorization ( La , Lo , Lp ) <EOL> Gint = self . _int_computation ( r , omega , phi , r , omega , phi ) <EOL> Flower = np . array ( self . _cos ( self . basis_alpha , self . basis_omega , self . basis_phi ) ( self . lower ) ) [ : , None ] <EOL> return ( self . lengthscale / ( <NUM_LIT:2> * self . variance ) * Gint + <NUM_LIT:1.> / self . variance * np . dot ( Flower , Flower . T ) ) <EOL> @ silence_errors <EOL> def update_gradients_full ( self , dL_dK , X , X2 = None ) : <EOL> """<STR_LIT>""" <EOL> if X2 is None : X2 = X <EOL> FX = self . _cos ( self . basis_alpha [ None , : ] , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] ) ( X ) <EOL> FX2 = self . _cos ( self . basis_alpha [ None , : ] , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] ) ( X2 ) <EOL> La = np . column_stack ( ( self . a [ <NUM_LIT:0> ] * np . ones ( ( self . n_basis , <NUM_LIT:1> ) ) , self . a [ <NUM_LIT:1> ] * self . basis_omega ) ) <EOL> Lo = np . column_stack ( ( self . basis_omega , self . basis_omega ) ) <EOL> Lp = np . column_stack ( ( self . basis_phi , self . basis_phi + np . pi / <NUM_LIT:2> ) ) <EOL> r , omega , phi = self . _cos_factorization ( La , Lo , Lp ) <EOL> Gint = self . _int_computation ( r , omega , phi , r , omega , phi ) <EOL> Flower = np . array ( self . _cos ( self . basis_alpha , self . basis_omega , self . basis_phi ) ( self . lower ) ) [ : , None ] <EOL> dK_dvar = <NUM_LIT:1.> / self . variance * mdot ( FX , self . Gi , FX2 . T ) <EOL> da_dlen = [ - <NUM_LIT:1.> / self . lengthscale ** <NUM_LIT:2> , <NUM_LIT:0.> ] <EOL> dLa_dlen = np . column_stack ( ( da_dlen [ <NUM_LIT:0> ] * np . ones ( ( self . n_basis , <NUM_LIT:1> ) ) , da_dlen [ <NUM_LIT:1> ] * self . basis_omega ) ) <EOL> r1 , omega1 , phi1 = self . _cos_factorization ( dLa_dlen , Lo , Lp ) <EOL> dGint_dlen = self . _int_computation ( r1 , omega1 , phi1 , r , omega , phi ) <EOL> dGint_dlen = dGint_dlen + dGint_dlen . T <EOL> dG_dlen = <NUM_LIT:1.> / <NUM_LIT:2> * Gint + self . lengthscale / <NUM_LIT:2> * dGint_dlen <EOL> dK_dlen = - mdot ( FX , self . Gi , dG_dlen / self . variance , self . Gi , FX2 . T ) <EOL> dFX_dper = self . _cos ( - self . basis_alpha [ None , : ] * self . basis_omega [ None , : ] / self . period * X , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] + np . pi / <NUM_LIT:2> ) ( X ) <EOL> dFX2_dper = self . _cos ( - self . basis_alpha [ None , : ] * self . basis_omega [ None , : ] / self . period * X2 , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] + np . pi / <NUM_LIT:2> ) ( X2 ) <EOL> dLa_dper = np . column_stack ( ( - self . a [ <NUM_LIT:0> ] * self . basis_omega / self . period , - self . a [ <NUM_LIT:1> ] * self . basis_omega ** <NUM_LIT:2> / self . period ) ) <EOL> dLp_dper = np . column_stack ( ( self . basis_phi + np . pi / <NUM_LIT:2> , self . basis_phi + np . pi ) ) <EOL> r1 , omega1 , phi1 = self . _cos_factorization ( dLa_dper , Lo , dLp_dper ) <EOL> IPPprim1 = self . upper * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + <NUM_LIT:1.> / ( omega - omega1 . T ) * np . cos ( ( omega - omega1 . T ) * self . upper + phi - phi1 . T - np . pi / <NUM_LIT:2> ) ) <EOL> IPPprim1 -= self . lower * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + <NUM_LIT:1.> / ( omega - omega1 . T ) * np . cos ( ( omega - omega1 . T ) * self . lower + phi - phi1 . T - np . pi / <NUM_LIT:2> ) ) <EOL> IPPprim2 = self . upper * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + self . upper * np . cos ( phi - phi1 . T ) ) <EOL> IPPprim2 -= self . lower * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + self . lower * np . cos ( phi - phi1 . T ) ) <EOL> IPPprim = np . where ( np . logical_or ( np . isnan ( IPPprim1 ) , np . isinf ( IPPprim1 ) ) , IPPprim2 , IPPprim1 ) <EOL> IPPint1 = <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / ( omega - omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega - omega1 . T ) * self . upper + phi - phi1 . T - np . pi ) <EOL> IPPint1 -= <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / ( omega - omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega - omega1 . T ) * self . lower + phi - phi1 . T - np . pi ) <EOL> IPPint2 = <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / <NUM_LIT:2> * self . upper ** <NUM_LIT:2> * np . cos ( phi - phi1 . T ) <EOL> IPPint2 -= <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / <NUM_LIT:2> * self . lower ** <NUM_LIT:2> * np . cos ( phi - phi1 . T ) <EOL> IPPint = np . where ( np . isnan ( IPPint1 ) , IPPint2 , IPPint1 ) <EOL> dLa_dper2 = np . column_stack ( ( - self . a [ <NUM_LIT:1> ] * self . basis_omega / self . period ) ) <EOL> dLp_dper2 = np . column_stack ( ( self . basis_phi + np . pi / <NUM_LIT:2> ) ) <EOL> r2 , omega2 , phi2 = dLa_dper2 . T , Lo [ : , <NUM_LIT:0> : <NUM_LIT:1> ] , dLp_dper2 . T <EOL> dGint_dper = np . dot ( r , r1 . T ) / <NUM_LIT:2> * ( IPPprim - IPPint ) + self . _int_computation ( r2 , omega2 , phi2 , r , omega , phi ) <EOL> dGint_dper = dGint_dper + dGint_dper . T <EOL> dFlower_dper = np . array ( self . _cos ( - self . lower * self . basis_alpha * self . basis_omega / self . period , self . basis_omega , self . basis_phi + np . pi / <NUM_LIT:2> ) ( self . lower ) ) [ : , None ] <EOL> dG_dper = <NUM_LIT:1.> / self . variance * ( self . lengthscale / <NUM_LIT:2> * dGint_dper + self . b [ <NUM_LIT:0> ] * ( np . dot ( dFlower_dper , Flower . T ) + np . dot ( Flower , dFlower_dper . T ) ) ) <EOL> dK_dper = mdot ( dFX_dper , self . Gi , FX2 . T ) - mdot ( FX , self . Gi , dG_dper , self . Gi , FX2 . T ) + mdot ( FX , self . Gi , dFX2_dper . T ) <EOL> self . variance . gradient = np . sum ( dK_dvar * dL_dK ) <EOL> self . lengthscale . gradient = np . sum ( dK_dlen * dL_dK ) <EOL> self . period . gradient = np . sum ( dK_dper * dL_dK ) <EOL> class PeriodicMatern32 ( Periodic ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , input_dim = <NUM_LIT:1> , variance = <NUM_LIT:1.> , lengthscale = <NUM_LIT:1.> , period = <NUM_LIT> * np . pi , n_freq = <NUM_LIT:10> , lower = <NUM_LIT:0.> , upper = <NUM_LIT:4> * np . pi , active_dims = None , name = '<STR_LIT>' ) : <EOL> super ( PeriodicMatern32 , self ) . __init__ ( input_dim , variance , lengthscale , period , n_freq , lower , upper , active_dims , name ) <EOL> def parameters_changed ( self ) : <EOL> self . a = [ <NUM_LIT> / self . lengthscale ** <NUM_LIT:2> , <NUM_LIT:2> * np . sqrt ( <NUM_LIT:3> ) / self . lengthscale , <NUM_LIT:1.> ] <EOL> self . b = [ <NUM_LIT:1> , self . lengthscale ** <NUM_LIT:2> / <NUM_LIT:3> ] <EOL> self . basis_alpha = np . ones ( ( self . n_basis , ) ) <EOL> self . basis_omega = ( <NUM_LIT:2> * np . pi * np . arange ( <NUM_LIT:1> , self . n_freq + <NUM_LIT:1> ) / self . period ) . repeat ( <NUM_LIT:2> ) <EOL> self . basis_phi = np . zeros ( self . n_freq * <NUM_LIT:2> ) <EOL> self . basis_phi [ : : <NUM_LIT:2> ] = - np . pi / <NUM_LIT:2> <EOL> self . G = self . Gram_matrix ( ) <EOL> self . Gi = np . linalg . inv ( self . G ) <EOL> def Gram_matrix ( self ) : <EOL> La = np . column_stack ( ( self . a [ <NUM_LIT:0> ] * np . ones ( ( self . n_basis , <NUM_LIT:1> ) ) , self . a [ <NUM_LIT:1> ] * self . basis_omega , self . a [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:2> ) ) <EOL> Lo = np . column_stack ( ( self . basis_omega , self . basis_omega , self . basis_omega ) ) <EOL> Lp = np . column_stack ( ( self . basis_phi , self . basis_phi + np . pi / <NUM_LIT:2> , self . basis_phi + np . pi ) ) <EOL> r , omega , phi = self . _cos_factorization ( La , Lo , Lp ) <EOL> Gint = self . _int_computation ( r , omega , phi , r , omega , phi ) <EOL> Flower = np . array ( self . _cos ( self . basis_alpha , self . basis_omega , self . basis_phi ) ( self . lower ) ) [ : , None ] <EOL> F1lower = np . array ( self . _cos ( self . basis_alpha * self . basis_omega , self . basis_omega , self . basis_phi + np . pi / <NUM_LIT:2> ) ( self . lower ) ) [ : , None ] <EOL> return ( self . lengthscale ** <NUM_LIT:3> / ( <NUM_LIT:12> * np . sqrt ( <NUM_LIT:3> ) * self . variance ) * Gint + <NUM_LIT:1.> / self . variance * np . dot ( Flower , Flower . T ) + self . lengthscale ** <NUM_LIT:2> / ( <NUM_LIT> * self . variance ) * np . dot ( F1lower , F1lower . T ) ) <EOL> @ silence_errors <EOL> def update_gradients_full ( self , dL_dK , X , X2 ) : <EOL> """<STR_LIT>""" <EOL> if X2 is None : X2 = X <EOL> FX = self . _cos ( self . basis_alpha [ None , : ] , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] ) ( X ) <EOL> FX2 = self . _cos ( self . basis_alpha [ None , : ] , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] ) ( X2 ) <EOL> La = np . column_stack ( ( self . a [ <NUM_LIT:0> ] * np . ones ( ( self . n_basis , <NUM_LIT:1> ) ) , self . a [ <NUM_LIT:1> ] * self . basis_omega , self . a [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:2> ) ) <EOL> Lo = np . column_stack ( ( self . basis_omega , self . basis_omega , self . basis_omega ) ) <EOL> Lp = np . column_stack ( ( self . basis_phi , self . basis_phi + np . pi / <NUM_LIT:2> , self . basis_phi + np . pi ) ) <EOL> r , omega , phi = self . _cos_factorization ( La , Lo , Lp ) <EOL> Gint = self . _int_computation ( r , omega , phi , r , omega , phi ) <EOL> Flower = np . array ( self . _cos ( self . basis_alpha , self . basis_omega , self . basis_phi ) ( self . lower ) ) [ : , None ] <EOL> F1lower = np . array ( self . _cos ( self . basis_alpha * self . basis_omega , self . basis_omega , self . basis_phi + np . pi / <NUM_LIT:2> ) ( self . lower ) ) [ : , None ] <EOL> dK_dvar = <NUM_LIT:1.> / self . variance * mdot ( FX , self . Gi , FX2 . T ) <EOL> da_dlen = [ - <NUM_LIT:6> / self . lengthscale ** <NUM_LIT:3> , - <NUM_LIT:2> * np . sqrt ( <NUM_LIT:3> ) / self . lengthscale ** <NUM_LIT:2> , <NUM_LIT:0.> ] <EOL> db_dlen = [ <NUM_LIT:0.> , <NUM_LIT:2> * self . lengthscale / <NUM_LIT> ] <EOL> dLa_dlen = np . column_stack ( ( da_dlen [ <NUM_LIT:0> ] * np . ones ( ( self . n_basis , <NUM_LIT:1> ) ) , da_dlen [ <NUM_LIT:1> ] * self . basis_omega , da_dlen [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:2> ) ) <EOL> r1 , omega1 , phi1 = self . _cos_factorization ( dLa_dlen , Lo , Lp ) <EOL> dGint_dlen = self . _int_computation ( r1 , omega1 , phi1 , r , omega , phi ) <EOL> dGint_dlen = dGint_dlen + dGint_dlen . T <EOL> dG_dlen = self . lengthscale ** <NUM_LIT:2> / ( <NUM_LIT:4> * np . sqrt ( <NUM_LIT:3> ) ) * Gint + self . lengthscale ** <NUM_LIT:3> / ( <NUM_LIT:12> * np . sqrt ( <NUM_LIT:3> ) ) * dGint_dlen + db_dlen [ <NUM_LIT:0> ] * np . dot ( Flower , Flower . T ) + db_dlen [ <NUM_LIT:1> ] * np . dot ( F1lower , F1lower . T ) <EOL> dK_dlen = - mdot ( FX , self . Gi , dG_dlen / self . variance , self . Gi , FX2 . T ) <EOL> dFX_dper = self . _cos ( - self . basis_alpha [ None , : ] * self . basis_omega [ None , : ] / self . period * X , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] + np . pi / <NUM_LIT:2> ) ( X ) <EOL> dFX2_dper = self . _cos ( - self . basis_alpha [ None , : ] * self . basis_omega [ None , : ] / self . period * X2 , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] + np . pi / <NUM_LIT:2> ) ( X2 ) <EOL> dLa_dper = np . column_stack ( ( - self . a [ <NUM_LIT:0> ] * self . basis_omega / self . period , - self . a [ <NUM_LIT:1> ] * self . basis_omega ** <NUM_LIT:2> / self . period , - self . a [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:3> / self . period ) ) <EOL> dLp_dper = np . column_stack ( ( self . basis_phi + np . pi / <NUM_LIT:2> , self . basis_phi + np . pi , self . basis_phi + np . pi * <NUM_LIT:3> / <NUM_LIT:2> ) ) <EOL> r1 , omega1 , phi1 = self . _cos_factorization ( dLa_dper , Lo , dLp_dper ) <EOL> IPPprim1 = self . upper * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + <NUM_LIT:1.> / ( omega - omega1 . T ) * np . cos ( ( omega - omega1 . T ) * self . upper + phi - phi1 . T - np . pi / <NUM_LIT:2> ) ) <EOL> IPPprim1 -= self . lower * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + <NUM_LIT:1.> / ( omega - omega1 . T ) * np . cos ( ( omega - omega1 . T ) * self . lower + phi - phi1 . T - np . pi / <NUM_LIT:2> ) ) <EOL> IPPprim2 = self . upper * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + self . upper * np . cos ( phi - phi1 . T ) ) <EOL> IPPprim2 -= self . lower * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + self . lower * np . cos ( phi - phi1 . T ) ) <EOL> IPPprim = np . where ( np . isnan ( IPPprim1 ) , IPPprim2 , IPPprim1 ) <EOL> IPPint1 = <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / ( omega - omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega - omega1 . T ) * self . upper + phi - phi1 . T - np . pi ) <EOL> IPPint1 -= <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / ( omega - omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega - omega1 . T ) * self . lower + phi - phi1 . T - np . pi ) <EOL> IPPint2 = <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / <NUM_LIT:2> * self . upper ** <NUM_LIT:2> * np . cos ( phi - phi1 . T ) <EOL> IPPint2 -= <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / <NUM_LIT:2> * self . lower ** <NUM_LIT:2> * np . cos ( phi - phi1 . T ) <EOL> IPPint = np . where ( np . isnan ( IPPint1 ) , IPPint2 , IPPint1 ) <EOL> dLa_dper2 = np . column_stack ( ( - self . a [ <NUM_LIT:1> ] * self . basis_omega / self . period , - <NUM_LIT:2> * self . a [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:2> / self . period ) ) <EOL> dLp_dper2 = np . column_stack ( ( self . basis_phi + np . pi / <NUM_LIT:2> , self . basis_phi + np . pi ) ) <EOL> r2 , omega2 , phi2 = self . _cos_factorization ( dLa_dper2 , Lo [ : , <NUM_LIT:0> : <NUM_LIT:2> ] , dLp_dper2 ) <EOL> dGint_dper = np . dot ( r , r1 . T ) / <NUM_LIT:2> * ( IPPprim - IPPint ) + self . _int_computation ( r2 , omega2 , phi2 , r , omega , phi ) <EOL> dGint_dper = dGint_dper + dGint_dper . T <EOL> dFlower_dper = np . array ( self . _cos ( - self . lower * self . basis_alpha * self . basis_omega / self . period , self . basis_omega , self . basis_phi + np . pi / <NUM_LIT:2> ) ( self . lower ) ) [ : , None ] <EOL> dF1lower_dper = np . array ( self . _cos ( - self . lower * self . basis_alpha * self . basis_omega ** <NUM_LIT:2> / self . period , self . basis_omega , self . basis_phi + np . pi ) ( self . lower ) + self . _cos ( - self . basis_alpha * self . basis_omega / self . period , self . basis_omega , self . basis_phi + np . pi / <NUM_LIT:2> ) ( self . lower ) ) [ : , None ] <EOL> dG_dper = <NUM_LIT:1.> / self . variance * ( self . lengthscale ** <NUM_LIT:3> / ( <NUM_LIT:12> * np . sqrt ( <NUM_LIT:3> ) ) * dGint_dper + self . b [ <NUM_LIT:0> ] * ( np . dot ( dFlower_dper , Flower . T ) + np . dot ( Flower , dFlower_dper . T ) ) + self . b [ <NUM_LIT:1> ] * ( np . dot ( dF1lower_dper , F1lower . T ) + np . dot ( F1lower , dF1lower_dper . T ) ) ) <EOL> dK_dper = mdot ( dFX_dper , self . Gi , FX2 . T ) - mdot ( FX , self . Gi , dG_dper , self . Gi , FX2 . T ) + mdot ( FX , self . Gi , dFX2_dper . T ) <EOL> self . variance . gradient = np . sum ( dK_dvar * dL_dK ) <EOL> self . lengthscale . gradient = np . sum ( dK_dlen * dL_dK ) <EOL> self . period . gradient = np . sum ( dK_dper * dL_dK ) <EOL> class PeriodicMatern52 ( Periodic ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , input_dim = <NUM_LIT:1> , variance = <NUM_LIT:1.> , lengthscale = <NUM_LIT:1.> , period = <NUM_LIT> * np . pi , n_freq = <NUM_LIT:10> , lower = <NUM_LIT:0.> , upper = <NUM_LIT:4> * np . pi , active_dims = None , name = '<STR_LIT>' ) : <EOL> super ( PeriodicMatern52 , self ) . __init__ ( input_dim , variance , lengthscale , period , n_freq , lower , upper , active_dims , name ) <EOL> def parameters_changed ( self ) : <EOL> self . a = [ <NUM_LIT:5> * np . sqrt ( <NUM_LIT:5> ) / self . lengthscale ** <NUM_LIT:3> , <NUM_LIT> / self . lengthscale ** <NUM_LIT:2> , <NUM_LIT:3> * np . sqrt ( <NUM_LIT:5> ) / self . lengthscale , <NUM_LIT:1.> ] <EOL> self . b = [ <NUM_LIT> / <NUM_LIT:8> , <NUM_LIT:9> * self . lengthscale ** <NUM_LIT:4> / <NUM_LIT> , <NUM_LIT:3> * self . lengthscale ** <NUM_LIT:2> / <NUM_LIT> , <NUM_LIT:3> * self . lengthscale ** <NUM_LIT:2> / ( <NUM_LIT:5> * <NUM_LIT> ) , <NUM_LIT:3> * self . lengthscale ** <NUM_LIT:2> / ( <NUM_LIT:5> * <NUM_LIT> ) ] <EOL> self . basis_alpha = np . ones ( ( <NUM_LIT:2> * self . n_freq , ) ) <EOL> self . basis_omega = ( <NUM_LIT:2> * np . pi * np . arange ( <NUM_LIT:1> , self . n_freq + <NUM_LIT:1> ) / self . period ) . repeat ( <NUM_LIT:2> ) <EOL> self . basis_phi = np . zeros ( self . n_freq * <NUM_LIT:2> ) <EOL> self . basis_phi [ : : <NUM_LIT:2> ] = - np . pi / <NUM_LIT:2> <EOL> self . G = self . Gram_matrix ( ) <EOL> self . Gi = np . linalg . inv ( self . G ) <EOL> def Gram_matrix ( self ) : <EOL> La = np . column_stack ( ( self . a [ <NUM_LIT:0> ] * np . ones ( ( self . n_basis , <NUM_LIT:1> ) ) , self . a [ <NUM_LIT:1> ] * self . basis_omega , self . a [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:2> , self . a [ <NUM_LIT:3> ] * self . basis_omega ** <NUM_LIT:3> ) ) <EOL> Lo = np . column_stack ( ( self . basis_omega , self . basis_omega , self . basis_omega , self . basis_omega ) ) <EOL> Lp = np . column_stack ( ( self . basis_phi , self . basis_phi + np . pi / <NUM_LIT:2> , self . basis_phi + np . pi , self . basis_phi + np . pi * <NUM_LIT:3> / <NUM_LIT:2> ) ) <EOL> r , omega , phi = self . _cos_factorization ( La , Lo , Lp ) <EOL> Gint = self . _int_computation ( r , omega , phi , r , omega , phi ) <EOL> Flower = np . array ( self . _cos ( self . basis_alpha , self . basis_omega , self . basis_phi ) ( self . lower ) ) [ : , None ] <EOL> F1lower = np . array ( self . _cos ( self . basis_alpha * self . basis_omega , self . basis_omega , self . basis_phi + np . pi / <NUM_LIT:2> ) ( self . lower ) ) [ : , None ] <EOL> F2lower = np . array ( self . _cos ( self . basis_alpha * self . basis_omega ** <NUM_LIT:2> , self . basis_omega , self . basis_phi + np . pi ) ( self . lower ) ) [ : , None ] <EOL> lower_terms = self . b [ <NUM_LIT:0> ] * np . dot ( Flower , Flower . T ) + self . b [ <NUM_LIT:1> ] * np . dot ( F2lower , F2lower . T ) + self . b [ <NUM_LIT:2> ] * np . dot ( F1lower , F1lower . T ) + self . b [ <NUM_LIT:3> ] * np . dot ( F2lower , Flower . T ) + self . b [ <NUM_LIT:4> ] * np . dot ( Flower , F2lower . T ) <EOL> return ( <NUM_LIT:3> * self . lengthscale ** <NUM_LIT:5> / ( <NUM_LIT> * np . sqrt ( <NUM_LIT:5> ) * self . variance ) * Gint + <NUM_LIT:1.> / self . variance * lower_terms ) <EOL> @ silence_errors <EOL> def update_gradients_full ( self , dL_dK , X , X2 = None ) : <EOL> if X2 is None : X2 = X <EOL> FX = self . _cos ( self . basis_alpha [ None , : ] , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] ) ( X ) <EOL> FX2 = self . _cos ( self . basis_alpha [ None , : ] , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] ) ( X2 ) <EOL> La = np . column_stack ( ( self . a [ <NUM_LIT:0> ] * np . ones ( ( self . n_basis , <NUM_LIT:1> ) ) , self . a [ <NUM_LIT:1> ] * self . basis_omega , self . a [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:2> , self . a [ <NUM_LIT:3> ] * self . basis_omega ** <NUM_LIT:3> ) ) <EOL> Lo = np . column_stack ( ( self . basis_omega , self . basis_omega , self . basis_omega , self . basis_omega ) ) <EOL> Lp = np . column_stack ( ( self . basis_phi , self . basis_phi + np . pi / <NUM_LIT:2> , self . basis_phi + np . pi , self . basis_phi + np . pi * <NUM_LIT:3> / <NUM_LIT:2> ) ) <EOL> r , omega , phi = self . _cos_factorization ( La , Lo , Lp ) <EOL> Gint = self . _int_computation ( r , omega , phi , r , omega , phi ) <EOL> Flower = np . array ( self . _cos ( self . basis_alpha , self . basis_omega , self . basis_phi ) ( self . lower ) ) [ : , None ] <EOL> F1lower = np . array ( self . _cos ( self . basis_alpha * self . basis_omega , self . basis_omega , self . basis_phi + np . pi / <NUM_LIT:2> ) ( self . lower ) ) [ : , None ] <EOL> F2lower = np . array ( self . _cos ( self . basis_alpha * self . basis_omega ** <NUM_LIT:2> , self . basis_omega , self . basis_phi + np . pi ) ( self . lower ) ) [ : , None ] <EOL> dK_dvar = <NUM_LIT:1.> / self . variance * mdot ( FX , self . Gi , FX2 . T ) <EOL> da_dlen = [ - <NUM_LIT:3> * self . a [ <NUM_LIT:0> ] / self . lengthscale , - <NUM_LIT:2> * self . a [ <NUM_LIT:1> ] / self . lengthscale , - self . a [ <NUM_LIT:2> ] / self . lengthscale , <NUM_LIT:0.> ] <EOL> db_dlen = [ <NUM_LIT:0.> , <NUM_LIT:4> * self . b [ <NUM_LIT:1> ] / self . lengthscale , <NUM_LIT:2> * self . b [ <NUM_LIT:2> ] / self . lengthscale , <NUM_LIT:2> * self . b [ <NUM_LIT:3> ] / self . lengthscale , <NUM_LIT:2> * self . b [ <NUM_LIT:4> ] / self . lengthscale ] <EOL> dLa_dlen = np . column_stack ( ( da_dlen [ <NUM_LIT:0> ] * np . ones ( ( self . n_basis , <NUM_LIT:1> ) ) , da_dlen [ <NUM_LIT:1> ] * self . basis_omega , da_dlen [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:2> , da_dlen [ <NUM_LIT:3> ] * self . basis_omega ** <NUM_LIT:3> ) ) <EOL> r1 , omega1 , phi1 = self . _cos_factorization ( dLa_dlen , Lo , Lp ) <EOL> dGint_dlen = self . _int_computation ( r1 , omega1 , phi1 , r , omega , phi ) <EOL> dGint_dlen = dGint_dlen + dGint_dlen . T <EOL> dlower_terms_dlen = db_dlen [ <NUM_LIT:0> ] * np . dot ( Flower , Flower . T ) + db_dlen [ <NUM_LIT:1> ] * np . dot ( F2lower , F2lower . T ) + db_dlen [ <NUM_LIT:2> ] * np . dot ( F1lower , F1lower . T ) + db_dlen [ <NUM_LIT:3> ] * np . dot ( F2lower , Flower . T ) + db_dlen [ <NUM_LIT:4> ] * np . dot ( Flower , F2lower . T ) <EOL> dG_dlen = <NUM_LIT:15> * self . lengthscale ** <NUM_LIT:4> / ( <NUM_LIT> * np . sqrt ( <NUM_LIT:5> ) ) * Gint + <NUM_LIT:3> * self . lengthscale ** <NUM_LIT:5> / ( <NUM_LIT> * np . sqrt ( <NUM_LIT:5> ) ) * dGint_dlen + dlower_terms_dlen <EOL> dK_dlen = - mdot ( FX , self . Gi , dG_dlen / self . variance , self . Gi , FX2 . T ) <EOL> dFX_dper = self . _cos ( - self . basis_alpha [ None , : ] * self . basis_omega [ None , : ] / self . period * X , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] + np . pi / <NUM_LIT:2> ) ( X ) <EOL> dFX2_dper = self . _cos ( - self . basis_alpha [ None , : ] * self . basis_omega [ None , : ] / self . period * X2 , self . basis_omega [ None , : ] , self . basis_phi [ None , : ] + np . pi / <NUM_LIT:2> ) ( X2 ) <EOL> dLa_dper = np . column_stack ( ( - self . a [ <NUM_LIT:0> ] * self . basis_omega / self . period , - self . a [ <NUM_LIT:1> ] * self . basis_omega ** <NUM_LIT:2> / self . period , - self . a [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:3> / self . period , - self . a [ <NUM_LIT:3> ] * self . basis_omega ** <NUM_LIT:4> / self . period ) ) <EOL> dLp_dper = np . column_stack ( ( self . basis_phi + np . pi / <NUM_LIT:2> , self . basis_phi + np . pi , self . basis_phi + np . pi * <NUM_LIT:3> / <NUM_LIT:2> , self . basis_phi ) ) <EOL> r1 , omega1 , phi1 = self . _cos_factorization ( dLa_dper , Lo , dLp_dper ) <EOL> IPPprim1 = self . upper * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + <NUM_LIT:1.> / ( omega - omega1 . T ) * np . cos ( ( omega - omega1 . T ) * self . upper + phi - phi1 . T - np . pi / <NUM_LIT:2> ) ) <EOL> IPPprim1 -= self . lower * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + <NUM_LIT:1.> / ( omega - omega1 . T ) * np . cos ( ( omega - omega1 . T ) * self . lower + phi - phi1 . T - np . pi / <NUM_LIT:2> ) ) <EOL> IPPprim2 = self . upper * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + self . upper * np . cos ( phi - phi1 . T ) ) <EOL> IPPprim2 -= self . lower * ( <NUM_LIT:1.> / ( omega + omega1 . T ) * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi / <NUM_LIT:2> ) + self . lower * np . cos ( phi - phi1 . T ) ) <EOL> IPPprim = np . where ( np . isnan ( IPPprim1 ) , IPPprim2 , IPPprim1 ) <EOL> IPPint1 = <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / ( omega - omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega - omega1 . T ) * self . upper + phi - phi1 . T - np . pi ) <EOL> IPPint1 -= <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / ( omega - omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega - omega1 . T ) * self . lower + phi - phi1 . T - np . pi ) <EOL> IPPint2 = <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . upper + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / <NUM_LIT:2> * self . upper ** <NUM_LIT:2> * np . cos ( phi - phi1 . T ) <EOL> IPPint2 -= <NUM_LIT:1.> / ( omega + omega1 . T ) ** <NUM_LIT:2> * np . cos ( ( omega + omega1 . T ) * self . lower + phi + phi1 . T - np . pi ) + <NUM_LIT:1.> / <NUM_LIT:2> * self . lower ** <NUM_LIT:2> * np . cos ( phi - phi1 . T ) <EOL> IPPint = np . where ( np . isnan ( IPPint1 ) , IPPint2 , IPPint1 ) <EOL> dLa_dper2 = np . column_stack ( ( - self . a [ <NUM_LIT:1> ] * self . basis_omega / self . period , - <NUM_LIT:2> * self . a [ <NUM_LIT:2> ] * self . basis_omega ** <NUM_LIT:2> / self . period , - <NUM_LIT:3> * self . a [ <NUM_LIT:3> ] * self . basis_omega ** <NUM_LIT:3> / self . period ) ) <EOL> dLp_dper2 = np . column_stack ( ( self . basis_phi + np . pi / <NUM_LIT:2> , self . basis_phi + np . pi , self . basis_phi + np . pi * <NUM_LIT:3> / <NUM_LIT:2> ) ) <EOL> r2 , omega2 , phi2 = self . _cos_factorization ( dLa_dper2 , Lo [ : , <NUM_LIT:0> : <NUM_LIT:2> ] , dLp_dper2 ) <EOL> dGint_dper = np . dot ( r , r1 . T ) / <NUM_LIT:2> * ( IPPprim - IPPint ) + self . _int_computation ( r2 , omega2 , phi2 , r , omega , phi ) <EOL> dGint_dper = dGint_dper + dGint_dper . T <EOL> dFlower_dper = np . array ( self . _cos ( - self . lower * self . basis_alpha * self . basis_omega / self . period , self . basis_omega , self . basis_phi + np . pi / <NUM_LIT:2> ) ( self . lower ) ) [ : , None ] <EOL> dF1lower_dper = np . array ( self . _cos ( - self . lower * self . basis_alpha * self . basis_omega ** <NUM_LIT:2> / self . period , self . basis_omega , self . basis_phi + np . pi ) ( self . lower ) + self . _cos ( - self . basis_alpha * self . basis_omega / self . period , self . basis_omega , self . basis_phi + np . pi / <NUM_LIT:2> ) ( self . lower ) ) [ : , None ] <EOL> dF2lower_dper = np . array ( self . _cos ( - self . lower * self . basis_alpha * self . basis_omega ** <NUM_LIT:3> / self . period , self . basis_omega , self . basis_phi + np . pi * <NUM_LIT:3> / <NUM_LIT:2> ) ( self . lower ) + self . _cos ( - <NUM_LIT:2> * self . basis_alpha * self . basis_omega ** <NUM_LIT:2> / self . period , self . basis_omega , self . basis_phi + np . pi ) ( self . lower ) ) [ : , None ] <EOL> dlower_terms_dper = self . b [ <NUM_LIT:0> ] * ( np . dot ( dFlower_dper , Flower . T ) + np . dot ( Flower . T , dFlower_dper ) ) <EOL> dlower_terms_dper += self . b [ <NUM_LIT:1> ] * ( np . dot ( dF2lower_dper , F2lower . T ) + np . dot ( F2lower , dF2lower_dper . T ) ) - <NUM_LIT:4> * self . b [ <NUM_LIT:1> ] / self . period * np . dot ( F2lower , F2lower . T ) <EOL> dlower_terms_dper += self . b [ <NUM_LIT:2> ] * ( np . dot ( dF1lower_dper , F1lower . T ) + np . dot ( F1lower , dF1lower_dper . T ) ) - <NUM_LIT:2> * self . b [ <NUM_LIT:2> ] / self . period * np . dot ( F1lower , F1lower . T ) <EOL> dlower_terms_dper += self . b [ <NUM_LIT:3> ] * ( np . dot ( dF2lower_dper , Flower . T ) + np . dot ( F2lower , dFlower_dper . T ) ) - <NUM_LIT:2> * self . b [ <NUM_LIT:3> ] / self . period * np . dot ( F2lower , Flower . T ) <EOL> dlower_terms_dper += self . b [ <NUM_LIT:4> ] * ( np . dot ( dFlower_dper , F2lower . T ) + np . dot ( Flower , dF2lower_dper . T ) ) - <NUM_LIT:2> * self . b [ <NUM_LIT:4> ] / self . period * np . dot ( Flower , F2lower . T ) <EOL> dG_dper = <NUM_LIT:1.> / self . variance * ( <NUM_LIT:3> * self . lengthscale ** <NUM_LIT:5> / ( <NUM_LIT> * np . sqrt ( <NUM_LIT:5> ) ) * dGint_dper + <NUM_LIT:0.5> * dlower_terms_dper ) <EOL> dK_dper = mdot ( dFX_dper , self . Gi , FX2 . T ) - mdot ( FX , self . Gi , dG_dper , self . Gi , FX2 . T ) + mdot ( FX , self . Gi , dFX2_dper . T ) <EOL> self . variance . gradient = np . sum ( dK_dvar * dL_dK ) <EOL> self . lengthscale . gradient = np . sum ( dK_dlen * dL_dK ) <EOL> self . period . gradient = np . sum ( dK_dper * dL_dK ) </s>
