예제 #1
0
    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)
        n, = nat_params if nat_params is not None else cls.standardToNat(
            *params)

        d = np.vstack(
            [Dirichlet.log_partitionGradient(nat_params=(_n, )) for _n in n])
        return (d, ) if split == False else ((d, ), (0, ))
예제 #2
0
    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, = params if params is not None else cls.natToStandard(
         *nat_params)
     last_dim = alpha.shape[-1]
     return sum([
         Dirichlet.log_partition(params=(a, ))
         for a in alpha.reshape((-1, last_dim))
     ])
 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_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)
        alpha, = nat_params if nat_params is not None else cls.standardToNat(
            *params)
        last_dim = alpha.shape[-1]

        d = np.vstack([
            Dirichlet.log_partitionGradient(nat_params=(a, ))
            for a in alpha.reshape((-1, last_dim))
        ]).reshape(alpha.shape)
        return (d, ) if split == False else ((d, ), (0, ))
예제 #7
0
    def sample(cls, params=None, nat_params=None, size=1):
        # Sample from P( x | Ѳ; α )
        assert (params is None) ^ (nat_params is None)

        (alpha, ) = params if params is not None else cls.natToStandard(
            *nat_params)

        ans = np.swapaxes(
            np.array(
                [Dirichlet.sample(params=(a, ), size=size) for a in alpha]), 0,
            1)
        cls.checkShape(ans)
        return ans
    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 sample(cls, params=None, nat_params=None, size=1):
        # Sample from P( x | Ѳ; α )
        assert (params is None) ^ (nat_params is None)

        (alpha, ) = params if params is not None else cls.natToStandard(
            *nat_params)

        ans = np.empty(alpha.shape + (size, ))
        for indices in itertools.product(*[range(s)
                                           for s in alpha.shape[:-1]]):
            ans[indices] = Dirichlet.sample(params=(alpha[indices], ),
                                            size=size).T

        ans = np.rollaxis(ans, -1)

        cls.checkShape(ans)
        return ans
예제 #11
0
    def log_likelihood(cls, x, params=None, nat_params=None):
        # Compute P( x | Ѳ; α )
        assert (params is None) ^ (nat_params is None)
        (alpha, ) = params if params is not None else cls.natToStandard(
            *nat_params)
        if (isinstance(x, tuple)):
            assert len(x) == 1
            x, = x
        assert isinstance(x, np.ndarray)
        if (x.ndim == 3):
            return sum([
                TransitionDirichletPrior.log_likelihood(_x, params=(alpha, ))
                for _x in x
            ])

        assert isinstance(x, np.ndarray) and x.ndim == 2, x
        return sum([
            Dirichlet.log_likelihood(_x, params=(a, ))
            for _x, a in zip(x, alpha)
        ])
    def log_likelihood(cls, x, params=None, nat_params=None):
        # Compute P( x | Ѳ; α )
        assert (params is None) ^ (nat_params is None)
        (alpha, ) = params if params is not None else cls.natToStandard(
            *nat_params)
        if (isinstance(x, tuple)):
            assert len(x) == 1
            x, = x
        assert isinstance(x, np.ndarray)

        if (x.ndim > alpha.ndim):
            assert x.ndim == alpha.ndim + 1
            return sum([
                TensorTransitionDirichletPrior.log_likelihood(_x,
                                                              params=(alpha, ))
                for _x in x
            ])

        last_dim = alpha.shape[-1]
        return sum([
            Dirichlet.log_likelihood(_x, params=(a, ))
            for _x, a in zip(x.reshape((
                -1, last_dim)), alpha.reshape((-1, last_dim)))
        ])
예제 #13
0
 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
예제 #14
0
 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
예제 #15
0
 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, ) = params if params is not None else cls.natToStandard(
         *nat_params)
     return sum([Dirichlet.log_partition(params=(a, )) for a in alpha])