def generate(cls, measurements=4, T=5, D_latent=3, D_obs=2, size=1): initialDist = Dirichlet.generate(D=D_latent) transDist = TransitionDirichletPrior.generate(D_in=D_latent, D_out=D_latent) emissionDist = TransitionDirichletPrior.generate(D_in=D_latent, D_out=D_obs) dummy = cls(initialDist, transDist, emissionDist) return dummy.isample(measurements=measurements, T=T, size=size)
def log_partition(cls, x=None, params=None, nat_params=None, split=False): # Compute A( Ѳ ) - log( h( x ) ) assert (params is None) ^ (nat_params is None) alpha_0, alpha_pi, alpha_L = params if params is not None else cls.natToStandard( *nat_params) A1 = Dirichlet.log_partition(params=(alpha_0, ), split=split) A2 = TransitionDirichletPrior.log_partition(params=(alpha_pi, ), split=split) A3 = TransitionDirichletPrior.log_partition(params=(alpha_L, ), split=split) return A1 + A2 + A3
def log_partitionGradient(cls, params=None, nat_params=None, split=False): # Derivative w.r.t. natural params. Also the expected sufficient stat assert (params is None) ^ (nat_params is None) n1, n2, n3 = nat_params if nat_params is not None else cls.standardToNat( *params) d1 = Dirichlet.log_partitionGradient(nat_params=(n1, ))[0] d2 = TransitionDirichletPrior.log_partitionGradient( nat_params=(n2, ))[0] d3 = TransitionDirichletPrior.log_partitionGradient( nat_params=(n3, ))[0] return (d1, d2, d3) if split == False else ((d1, d2, d3), (0, ))
def log_likelihood(cls, x, params=None, nat_params=None): # Compute P( x | Ѳ; α ) assert (params is None) ^ (nat_params is None) alpha_0, alpha_pi, alpha_L = params if params is not None else cls.natToStandard( *nat_params) pi_0, pi, L = x ans = Dirichlet.log_likelihood(pi_0, params=(alpha_0, )) ans += TransitionDirichletPrior.log_likelihood(pi, params=(alpha_pi, )) ans += TransitionDirichletPrior.log_likelihood(L, params=(alpha_L, )) return ans
def sample(cls, params=None, nat_params=None, size=1): # Sample from P( x | Ѳ; α ) assert (params is None) ^ (nat_params is None) alpha_0, alpha_pi, alpha_L = params if params is not None else cls.natToStandard( *nat_params) pi_0 = Dirichlet.sample(params=(alpha_0, ), size=size) pi = TransitionDirichletPrior.sample(params=(alpha_pi, ), size=size) L = TransitionDirichletPrior.sample(params=(alpha_L, ), size=size) ans = (pi_0, pi, L) cls.checkShape(ans) return ans
def natToStandard(cls, n1, n2, n3): alpha_0, = Dirichlet.natToStandard(n1) alpha_pi, = TransitionDirichletPrior.natToStandard(n2) alpha_L, = TransitionDirichletPrior.natToStandard(n3) return alpha_0, alpha_pi, alpha_L
def standardToNat(cls, alpha_0, alpha_pi, alpha_L): n1, = Dirichlet.standardToNat(alpha_0) n2, = TransitionDirichletPrior.standardToNat(alpha_pi) n3, = TransitionDirichletPrior.standardToNat(alpha_L) return n1, n2, n3