#NEWLINE# def test_cost_gradient( self ) : #NEWLINE# #INDENT# '\n        Test the gradient of the rotation for MC with time-varying dynamics\n        ' #NEWLINE# np . random . seed ( 42 ) #NEWLINE# def check( D , N , K , mu = None , Lambda = None , rho = None ) : #NEWLINE# #INDENT# if ( mu is None ) : #NEWLINE# #INDENT# mu = np . zeros ( D ) #NEWLINE# #UNINDENT# if ( Lambda is None ) : #NEWLINE# #INDENT# Lambda = np . identity ( D ) #NEWLINE# #UNINDENT# if ( rho is None ) : #NEWLINE# #INDENT# rho = np . ones ( D ) #NEWLINE# #UNINDENT# V = ( np . identity ( D ) + np . ones ( ( D , D ) ) ) #NEWLINE# B = GaussianARD ( 3 , 5 , shape = ( D , K ) , plates = ( 1 , D ) ) #NEWLINE# S = GaussianARD ( 2 , 4 , shape = ( K , ) , plates = ( N , 1 ) ) #NEWLINE# A = SumMultiply ( 'dk,k->d' , B , S ) #NEWLINE# X = GaussianMarkovChain ( mu , Lambda , A , rho , n = ( N + 1 ) , initialize = False ) #NEWLINE# Y = Gaussian ( X , V , initialize = False ) #NEWLINE# Y . observe ( np . random . randn ( ( N + 1 ) , D ) ) #NEWLINE# X . update ( ) #NEWLINE# B . update ( ) #NEWLINE# S . update ( ) #NEWLINE# try : #NEWLINE# #INDENT# mu . update ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# Lambda . update ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# rho . update ( ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# rotB = RotateGaussianARD ( B , axis = ( - 2 ) ) #NEWLINE# rotX = RotateVaryingMarkovChain ( X , B , S , rotB ) #NEWLINE# rotX . setup ( ) #NEWLINE# R = np . random . randn ( D , D ) #NEWLINE# def cost( r ) : #NEWLINE# #INDENT# ( b , dr ) = rotX . bound ( np . reshape ( r , np . shape ( R ) ) ) #NEWLINE# return ( b , np . ravel ( dr ) ) #NEWLINE# #UNINDENT# err = optimize . check_gradient ( cost , np . ravel ( R ) , verbose = False ) [ 1 ] #NEWLINE# self . assertAllClose ( err , 0 , atol = 1e-06 , msg = 'Gradient incorrect' ) #NEWLINE# return #NEWLINE# #UNINDENT# self . _run_checks ( check ) #NEWLINE# pass