Esempio n. 1
0
    def statistics(self, x, y):
        if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
            idx = np.logical_and(~np.isnan(x).any(axis=1),
                                 ~np.isnan(y).any(axis=1))
            x, y = x[idx], y[idx]

            if self.affine:
                x = np.hstack((x, np.ones((x.shape[0], 1))))

            contract = 'nd,nl->dl'

            yxT = np.einsum(contract, y, x, optimize=True)
            xxT = np.einsum(contract, x, x, optimize=True)
            yyT = np.einsum(contract, y, y, optimize=True)
            n = y.shape[0]

            yxTk = np.array([yxT for _ in range(self.size)])
            xxTk = np.array([xxT for _ in range(self.size)])
            yyTk = np.array([yyT for _ in range(self.size)])
            nk = np.array([n for _ in range(self.size)])

            return Stats([yxTk, xxTk, yyTk, nk])
        else:
            stats = list(map(self.statistics, x, y))
            return reduce(add, stats)
Esempio n. 2
0
 def std_to_nat(self, params):
     params_list = list(zip(*params))
     natparams_list = [
         dist.std_to_nat(par) for dist, par in zip(self.dists, params_list)
     ]
     natparams_stack = Stats(
         map(partial(np.stack, axis=0), zip(*natparams_list)))
     return natparams_stack
Esempio n. 3
0
    def weighted_statistics(self, data, weights):
        if isinstance(data, np.ndarray):
            idx = ~np.isnan(data).any(axis=0)
            data, weights = data[idx], weights[idx]

            n = 0.5 * np.sum(weights)
            xx = -0.5 * np.einsum('nd,n,nd->d', data, weights, data)

            return Stats([n, xx])
        else:
            stats = list(map(self.weighted_statistics, data, weights))
            return reduce(add, stats)
Esempio n. 4
0
    def statistics(self, data):
        if isinstance(data, np.ndarray):
            idx = ~np.isnan(data).any(axis=0)
            data = data[idx]

            n = 0.5 * data.shape[0]
            xx = -0.5 * np.einsum('nd,nd->d', data, data)

            return Stats([n, xx])
        else:
            stats = list(map(self.statistics, data))
            return reduce(add, stats)
Esempio n. 5
0
    def weighted_statistics(self, data, weights):
        if isinstance(data, np.ndarray):
            idx = ~np.isnan(data).any(axis=1)
            data, weights = data[idx], weights[idx]

            x = np.einsum('n,nd->d', weights, data)
            n = np.sum(weights)
            xx = np.einsum('nd,n,nd->d', data, weights, data)
            nd = np.broadcast_to(np.sum(weights), (self.dim, ))

            return Stats([x, nd, nd, xx])
        else:
            stats = list(map(self.weighted_statistics, data, weights))
            return reduce(add, stats)
Esempio n. 6
0
    def statistics(self, data):
        if isinstance(data, np.ndarray):
            idx = ~np.isnan(data).any(axis=1)
            data = data[idx]

            x = np.sum(data, axis=0)
            n = data.shape[0]
            xx = np.einsum('nd,nd->d', data, data)
            nd = np.broadcast_to(data.shape[0], (self.dim, ))

            return Stats([x, nd, nd, xx])
        else:
            stats = list(map(self.statistics, data))
            return reduce(add, stats)
Esempio n. 7
0
    def weighted_statistics(self, data, weights):
        if isinstance(data, np.ndarray):
            idx = ~np.isnan(data).any(axis=1)
            data, weights = data[idx], weights[idx]

            xk = np.einsum('nk,nd->kd', weights, data)
            xxk = np.einsum('nd,nk,nd->kd', data, weights, data)
            ndk = np.broadcast_to(np.sum(weights, axis=0, keepdims=True),
                                  (self.size, self.dim))

            return Stats([xk, ndk, ndk, xxk])
        else:
            stats = list(map(self.weighted_statistics, data, weights))
            return reduce(add, stats)
Esempio n. 8
0
    def weighted_statistics(self, data, weights):
        if isinstance(data, np.ndarray):
            idx = ~np.isnan(data).any(axis=1)
            data, weights = data[idx], weights[idx]

            c0, c1 = 'nk,nd->kd', 'nd,nk,nl->kdl'

            xk = np.einsum(c0, weights, data, optimize=True)
            xxTk = np.einsum(c1, data, weights, data, optimize=True)
            nk = np.sum(weights, axis=0)

            return Stats([xk, nk, xxTk, nk])
        else:
            stats = list(map(self.weighted_statistics, data, weights))
            return reduce(add, stats)
Esempio n. 9
0
    def statistics(self, data):
        if isinstance(data, np.ndarray):
            idx = ~np.isnan(data).any(axis=1)
            data = data[idx]

            c0, c1 = 'nd->d', 'nd,nl->dl'

            x = np.einsum(c0, data, optimize=True)
            xxT = np.einsum(c1, data, data, optimize=True)
            n = data.shape[0]

            return Stats([x, n, xxT, n])
        else:
            stats = list(map(self.statistics, data))
            return reduce(add, stats)
Esempio n. 10
0
    def weighted_statistics(self, x, y, weights):
        if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
            idx = np.logical_and(~np.isnan(x).any(axis=1),
                                 ~np.isnan(y).any(axis=0))
            x, y, weights = x[idx], y[idx], weights[idx]

            if self.affine:
                x = np.hstack((x, np.ones((x.shape[0], 1))))

            xxT = - 0.5 * np.einsum('nd,n,nl->dl', x, weights, x, optimize=True)
            yxT = np.einsum('n,n,nl->l', y, weights, x, optimize=True)

            return Stats([yxT, xxT])
        else:
            stats = list(map(self.weighted_statistics, x, y, weights))
            return reduce(add, stats)
Esempio n. 11
0
    def std_to_nat(self, params):
        # stats = [A.T @ V,
        #          -0.5 * A.T @ V @ A,
        #          -0.5 * V,
        #          0.5 * log_det(V)]
        #
        # nats = [M @ K,
        #         K,
        #         psi^-1 + M @ K @ M.T,
        #         nu - d - 1. + l]

        a = params[0] @ params[1]
        b = params[1]
        c = np.linalg.inv(params[2]) + params[0] @ params[1] @ params[0].T
        d = params[3] - self.row_dim - 1. + self.column_dim
        return Stats([a, b, c, d])
Esempio n. 12
0
    def std_to_nat(params):
        # stats = [mu * lmbda_diag,
        #          -0.5 * lmbda_diag * mu * mu,
        #          0.5 * log(lmbda_diag),
        #          -0.5 * lmbda_diag]
        #
        # nats = [kappa * m,
        #         kappa,
        #         2. * alpha - 1.,
        #         2. * beta + kappa * m * m]

        a = params[1] * params[0]
        b = params[1]
        c = 2. * params[2] - 1.
        d = 2. * params[3] + params[1] * params[0]**2
        return Stats([a, b, c, d])
Esempio n. 13
0
    def std_to_nat(params):
        # stats = [A.T * V_diag,
        #          -0.5 * A.T @ A,
        #          0.5 * log(V_diag),
        #          -0.5 * V_diag]
        #
        # nats = [M @ K,
        #         K,
        #         2. * alpha - 1.,
        #         2. * beta + M @ K @ M.T]

        a = params[0] @ params[1]
        b = params[1]
        c = 2. * params[2] - 1.
        d = 2. * params[3] + np.einsum('dl,lm,dm->d', params[0], params[1],
                                       params[0])
        return Stats([a, b, c, d])
Esempio n. 14
0
    def std_to_nat(params):
        # stats = [mu.T @ lmbda,
        #          -0.5 * lmbda @ (mu @ mu.T),
        #          -0.5 * lmbda,
        #          0.5 * logdet(lmbda)]
        #
        # nats = [kappa * m,
        #         kappa,
        #         psi^-1 + kappa * (m @ m.T),
        #         nu - d]

        a = params[1] * params[0]
        b = params[1]
        c = np.linalg.inv(
            params[2]) + params[1] * np.outer(params[0], params[0])
        d = params[3] - params[2].shape[0]
        return Stats([a, b, c, d])
Esempio n. 15
0
    def weighted_statistics(self, x, y, weights):
        if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
            idx = np.logical_and(~np.isnan(x).any(axis=1),
                                 ~np.isnan(y).any(axis=0))
            x, y, weights = x[idx], y[idx], weights[idx]

            if self.affine:
                x = np.hstack((x, np.ones((x.shape[0], 1))))

            n = 0.5 * np.sum(weights)
            yy = - 0.5 * np.sum(weights * y * y)
            yxTa = np.einsum('n,n,nl,l->', y, weights, x, self.W, optimize=True)
            xTaaTx = - 0.5 * np.einsum('l,nl,n,nd,d->', self.W, x, weights, x, self.W, optimize=True)

            return Stats([n, yy + yxTa + xTaaTx])
        else:
            stats = list(map(self.weighted_statistics, x, y, weights))
            return reduce(add, stats)
Esempio n. 16
0
    def weighted_statistics(self, x, y, weights):
        if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
            idx = np.logical_and(~np.isnan(x).any(axis=1),
                                 ~np.isnan(y).any(axis=1))
            x, y, weights = x[idx], y[idx], weights[idx]

            if self.affine:
                x = np.hstack((x, np.ones((x.shape[0], 1))))

            xxT = np.einsum('nd,n,nl->dl', x, weights, x, optimize=True)
            yxT = np.einsum('nd,n,nl->dl', y, weights, x, optimize=True)
            yy = np.einsum('nd,n,nd->d', y, weights, y, optimize=True)
            nd = np.broadcast_to(np.sum(weights), (self.output_dim, ))

            return Stats([yxT, xxT, nd, yy])
        else:
            stats = list(map(self.weighted_statistics, x, y, weights))
            return reduce(add, stats)
Esempio n. 17
0
    def statistics(self, data):
        if isinstance(data, np.ndarray):
            idx = ~np.isnan(data).any(axis=1)
            data = data[idx]

            c0, c1 = 'nd->d', 'nd,nd->d'

            x = np.einsum(c0, data, optimize=True)
            xx = np.einsum(c1, data, data, optimize=True)
            nd = np.broadcast_to(data.shape[0], (self.dim, ))

            xk = np.array([x for _ in range(self.size)])
            xxk = np.array([xx for _ in range(self.size)])
            ndk = np.array([nd for _ in range(self.size)])

            return Stats([xk, ndk, ndk, xxk])
        else:
            stats = list(map(self.statistics, data))
            return reduce(add, stats)
Esempio n. 18
0
    def statistics(self, data):
        if isinstance(data, np.ndarray):
            idx = ~np.isnan(data).any(axis=1)
            data = data[idx]

            c0, c1 = 'nd->d', 'nd,nl->dl'

            x = np.einsum(c0, data, optimize=True)
            xxT = np.einsum(c1, data, data, optimize=True)
            n = data.shape[0]

            xk = np.array([x for _ in range(self.size)])
            xxTk = np.array([xxT for _ in range(self.size)])
            nk = np.array([n for _ in range(self.size)])

            return Stats([xk, nk, xxTk, nk])
        else:
            stats = list(map(self.statistics, data))
            return reduce(add, stats)
Esempio n. 19
0
    def weighted_statistics(self, x, y, weights):
        if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
            idx = np.logical_and(~np.isnan(x).any(axis=1),
                                 ~np.isnan(y).any(axis=1))
            x, y, weights = x[idx], y[idx], weights[idx]

            if self.affine:
                x = np.hstack((x, np.ones((x.shape[0], 1))))

            contract = 'nd,nk,nl->kdl'

            yxTk = np.einsum(contract, y, weights, x, optimize=True)
            xxTk = np.einsum(contract, x, weights, x, optimize=True)
            yyTk = np.einsum(contract, y, weights, y, optimize=True)
            nk = np.sum(weights, axis=0)

            return Stats([yxTk, xxTk, yyTk, nk])
        else:
            stats = list(map(self.weighted_statistics, x, y, weights))
            return reduce(add, stats)
Esempio n. 20
0
    def statistics(self, x, y):
        if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
            idx = np.logical_and(~np.isnan(x).any(axis=1),
                                 ~np.isnan(y).any(axis=1))
            x, y = x[idx], y[idx]

            if self.affine:
                x = np.hstack((x, np.ones((x.shape[0], 1))))

            xxT = np.einsum('nd,nl->dl', x, x, optimize=True)
            yxT = np.einsum('nd,nl->dl', y, x, optimize=True)
            yy = np.einsum('nd,nd->d', y, y, optimize=True)
            nd = np.broadcast_to(y.shape[0], (self.output_dim, ))

            xxTk = np.array([xxT for _ in range(self.size)])
            yxTk = np.array([yxT for _ in range(self.size)])
            yyk = np.array([yy for _ in range(self.size)])
            ndk = np.array([nd for _ in range(self.size)])

            return Stats([yxTk, xxTk, ndk, yyk])
        else:
            stats = list(map(self.statistics, x, y))
            return reduce(add, stats)
Esempio n. 21
0
    def estep(self, ems, act=None):
        if isinstance(ems, np.ndarray) \
                and isinstance(act, np.ndarray):

            smooth_mean, smooth_covar, gain, log_lik =\
                self.kalman_smoother(ems, act)

            nb_steps = ems.shape[0]

            # currently only for full covariances
            Ex = smooth_mean  # E[x{n}]
            ExxpT = np.zeros_like(gain)  # E[x_{n} x_{n-1}^T]
            for t in range(nb_steps - 1):
                ExxpT[t] = smooth_covar[t + 1] @ gain[t].T\
                           + np.outer(smooth_mean[t + 1], smooth_mean[t])

            ExxT = np.zeros_like(smooth_covar)  # E[x_{n} x_{n}^T]
            for t in range(nb_steps):
                ExxT[t] = smooth_covar[t] + np.outer(smooth_mean[t],
                                                     smooth_mean[t])

            # init_ltn_stats
            x, xxT = Ex[0], ExxT[0]
            init_ltn_stats = Stats([x, 1., xxT, 1.])

            # ltn_stats
            xxT = np.zeros((nb_steps - 1, self.ltn_dim + 1, self.ltn_dim + 1))
            for t in range(nb_steps - 1):
                xxT[t] = np.block([[ExxT[t], Ex[t][:, np.newaxis]],
                                   [Ex[t][np.newaxis, :],
                                    np.ones((1, ))]])

            yxT = np.zeros((nb_steps - 1, self.ltn_dim, self.ltn_dim + 1))
            for t in range(nb_steps - 1):
                yxT[t] = np.hstack((ExxpT[t], Ex[t + 1][:, np.newaxis]))

            yyT = ExxT[1:]

            ltn_stats = Stats([
                np.sum(yxT, axis=0),
                np.sum(xxT, axis=0),
                np.sum(yyT, axis=0), yyT.shape[0]
            ])

            # ems_stats
            xxT = np.zeros((nb_steps, self.ltn_dim + 1, self.ltn_dim + 1))
            for t in range(nb_steps):
                xxT[t] = np.block([[ExxT[t], Ex[t][:, np.newaxis]],
                                   [Ex[t][np.newaxis, :],
                                    np.ones((1, ))]])

            x = np.hstack((Ex, np.ones((Ex.shape[0], 1))))
            yxT = np.einsum('nd,nl->ndl', ems, x)
            yyT = np.einsum('nd,nl->ndl', ems, ems)

            ems_stats = Stats([
                np.sum(yxT, axis=0),
                np.sum(xxT, axis=0),
                np.sum(yyT, axis=0), yyT.shape[0]
            ])

            return init_ltn_stats, ltn_stats, ems_stats, log_lik
        else:

            def inner(ems, act):
                return self.estep.__wrapped__(self, ems, act)

            result = map(inner, ems, act)
            init_ltn_stats, ltn_stats, ems_stats, log_lik = list(
                map(list, zip(*result)))
            stats = tuple([
                reduce(add, init_ltn_stats),
                reduce(add, ltn_stats),
                reduce(add, ems_stats)
            ])
            return stats, np.sum(np.hstack(log_lik))
Esempio n. 22
0
File: gamma.py Progetto: hanyas/sds
 def std_to_nat(params):
     alphas = params[0] - 1
     betas = -params[1]
     return Stats([alphas, betas])
Esempio n. 23
0
 def std_to_nat(params):
     psi = - 0.5 * np.linalg.inv(params[0])
     nu = 0.5 * (params[1] - psi.shape[0] - 1)
     return Stats([psi, nu])
Esempio n. 24
0
 def std_to_nat(params):
     a = params[1] * params[0]
     b = -0.5 * params[1]
     return Stats([a, b])