Esempio n. 1
0
    def sVector_derivs(self, r, v, mu):
        """
        checked
        typed
        """
        e = self.eVector(r, v, mu)
        emag = np.linalg.norm(e)
        eUnit = e / emag
        n = self.nVector(r, v, mu)
        nmag = np.linalg.norm(n)
        nUnit = n / nmag
        rootTerm = np.sqrt(1.0 - (1.0 / emag**2))

        deVectordx = self.eVector_derivs(r, v, mu)
        deUnitdx = np.dot(self.mathUtil.unit_vector_deriv(e), deVectordx)
        dOneByemagdx = (-1.0 / emag**2) * np.dot(eUnit, deVectordx)

        dnVectordx = self.nVector_derivs(r, v, mu)
        dnUnitdx = np.dot(self.mathUtil.unit_vector_deriv(n), dnVectordx)

        drootTermdx = 0.5 * (1.0 - emag**(-2))**(-0.5) * 2.0 * emag**(
            -3) * np.dot(eUnit, deVectordx)

        term1 = np.outer(eUnit, dOneByemagdx)
        term2 = (1.0 / emag) * deUnitdx
        term3 = np.outer(nUnit, drootTermdx)
        term4 = rootTerm * dnUnitdx
        dsVectordx = -term1 - term2 + term3 + term4
        return dsVectordx
def pylds_E_step(lds, data):
    T = data.shape[0]
    mu_init, sigma_init, A, sigma_states, C, sigma_obs = lds
    normalizer, smoothed_mus, smoothed_sigmas, E_xtp1_xtT = \
        _E_step(mu_init, sigma_init, A, sigma_states, C, sigma_obs, data)

    EyyT = data.T.dot(data)
    EyxT = data.T.dot(smoothed_mus)
    ExxT = smoothed_sigmas.sum(0) + smoothed_mus.T.dot(smoothed_mus)

    E_xt_xtT = \
        ExxT - (smoothed_sigmas[-1]
                + np.outer(smoothed_mus[-1],smoothed_mus[-1]))
    E_xtp1_xtp1T = \
        ExxT - (smoothed_sigmas[0]
                + np.outer(smoothed_mus[0], smoothed_mus[0]))
    E_xtp1_xtT = E_xtp1_xtT.sum(0)

    E_x1_x1T = smoothed_sigmas[0] + np.outer(smoothed_mus[0], smoothed_mus[0])
    E_x1 = smoothed_mus[0]

    E_init_stats = E_x1_x1T, E_x1, 1.
    E_pairwise_stats = E_xt_xtT, E_xtp1_xtT.T, E_xtp1_xtp1T, T - 1
    E_node_stats = ExxT, EyxT.T, EyyT, T

    return E_init_stats, E_pairwise_stats, E_node_stats
def wishart_updates(x, mu, mu2, e_z, prior_mu, prior_inv_wishart_scale, prior_wishart_dof, kappa):
    k_approx = np.shape(mu)[0]

    prior_mu_tile = np.tile(prior_mu, (k_approx, 1))
    cross_term1 = np.dot(mu.T, prior_mu_tile)
    prior_normal_term = kappa * (np.sum(mu2, axis = 0)\
                                - cross_term1 - cross_term1.T \
                                + k_approx * np.outer(prior_mu, prior_mu))

    predictive = np.dot(e_z, mu)
    outer_mu = np.array([np.outer(mu[i,:], mu[i,:]) for i in range(k_approx)])
    z_sum = np.sum(e_z, axis = 0)

    cross_term2 = np.dot(x.T, predictive)
    data_lh_term = np.dot(x.T, x) - cross_term2 - cross_term2.T\
                    + np.einsum('kij, k -> ij', outer_mu, z_sum)

    inv_scale_update = prior_inv_wishart_scale + prior_normal_term + data_lh_term
    scale_update = np.linalg.inv(inv_scale_update)

    dof_update = prior_wishart_dof + np.shape(x)[0] + k_approx

    # there's some numerical issue in which the update is not exactly symmetric
    if np.any(np.abs(scale_update - scale_update.T) >= 1e-10):
        print('wishart scale not symmetric?')
        print(scale_update - scale_update.T)

    scale_update = 0.5 * (scale_update + scale_update.T)

    return scale_update, np.array([dof_update])
Esempio n. 4
0
    def eVector_derivs(self, r, v, mu):
        """
        checked
        """
        drdx = self.drdx(r)
        dvdx = self.dvdx(v)
        drvdx = np.dot(r, dvdx) + np.dot(v, drdx)
        drvvdx = np.outer(v, drvdx) + np.dot(np.dot(r, v), dvdx)

        rmag = np.linalg.norm(r)
        drmagdr = self.mathUtil.column_vector_norm2_deriv(r)
        dOneByRmagdx = (-1.0 / rmag**2) * np.dot(drmagdr, drdx)

        vmag = np.linalg.norm(v)
        dvmagdv = self.mathUtil.column_vector_norm2_deriv(v)
        dvmag2dx = 2.0 * vmag * np.dot(dvmagdv, dvdx)

        term1 = np.outer(
            r, (dvmag2dx - mu * dOneByRmagdx)) + (vmag**2 - mu / rmag) * drdx

        term2 = drvvdx
        deVectordxUnscaled = term1 - term2
        # final scaling
        deVectordx = (1.0 / mu) * deVectordxUnscaled
        return deVectordx
Esempio n. 5
0
def lunch_estimator(model):
  '''
  Huber sandwich estimator
  '''
  X = model.training_data.X.copy()
  Y = model.training_data.Y.copy()
  N = X.shape[0]
  D = model.params.get_free().shape[0]

  gradTgrad = np.zeros((D,D))
  grad_sum = np.zeros(D)
  hess_sum = np.zeros((D,D))
  w0 = model.params.get_free().copy()

  for n in range(N):
    model.training_data.X = X[n].reshape(1,-1)
    model.training_data.Y = Y[n].reshape(1,-1)
    grad_n = autograd.jacobian(model.eval_objective)(model.params.get_free())
    model.params.set_free(w0)
    gradTgrad += np.outer(grad_n, grad_n)
    grad_sum += grad_n
    hess_sum += autograd.hessian(model.eval_objective)(model.params.get_free())
    model.params.set_free(w0)
    
  est_fisher = np.linalg.inv(hess_sum / N)
  est_score_score = (gradTgrad - np.outer(grad_sum, grad_sum)) / N
  model.training_data.X = X
  model.training_data.Y = Y
  return est_fisher.dot(est_score_score).dot(est_fisher)
Esempio n. 6
0
    def plot_points(self, params, iter, gradient):
        phisim = np.linspace((-math.pi) / 2., (math.pi / 2.))
        thetasim = np.linspace(0, 2 * np.pi)
        print params
        pointscoord = np.full((3, 6), 0.0)

        for i in range(6):

            pointscoord[0, i] = params[i]
            pointscoord[1, i] = params[i + 1]
            pointscoord[2, i] = params[i + 2]

        x = np.outer(np.sin(thetasim), np.cos(phisim))
        y = np.outer(np.sin(thetasim), np.sin(phisim))
        z = np.outer(np.cos(thetasim), np.ones_like(phisim))
        fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
        ax.plot_wireframe(sph.radius * x,
                          sph.radius * y,
                          sph.radius * z,
                          color='g')
        ax.scatter(pointscoord[:3, 0],
                   pointscoord[:3, 1],
                   pointscoord[:3, 2],
                   c='r')
        ax.scatter(pointscoord[:3, 3],
                   pointscoord[:3, 4],
                   pointscoord[:3, 5],
                   c='r')
        plt.show()
Esempio n. 7
0
    def update_hessian_inverse(self, x, obj, p, state_aux):
        method = self.setting.step_method
        a, Hinv = state_aux
        n = x.size
        s = a * p
        y = obj.gradient(x + a * p) - obj.gradient(x)

        if method == 'bfgs':
            ssT = np.outer(s, s)
            ysT = np.outer(y, s)
            yTs = np.dot(y, s)
            C = np.eye(n) - ysT / yTs
            Hinv_new = np.dot(C.T, np.dot(Hinv, C)) + ssT / yTs
        elif method == 'dfp':
            Hinv_y = np.dot(Hinv, y)
            y_Hinv_y = np.dot(y, Hinv_y)
            ssT = np.outer(s, s)
            yTs = np.dot(y, s)
            Hinv_new = Hinv - np.outer(Hinv_y, Hinv_y) / y_Hinv_y + ssT / yTs
        elif method == 'sr1':
            Hinv_y = np.dot(Hinv, y)
            s_minus_Hinv_y = s - Hinv_y
            denominator = np.dot(s_minus_Hinv_y, y)
            if np.abs(denominator) > self.setting.sr1_skip_tol * la.norm(
                    y) * la.norm(s_minus_Hinv_y):
                Hinv_new = Hinv + np.outer(s_minus_Hinv_y,
                                           s_minus_Hinv_y) / denominator
            else:  # skipping rule to avoid huge search directions under denominator collapse
                Hinv_new = np.copy(Hinv)
        else:
            raise ValueError('Invalid step method!')
        return Hinv_new
Esempio n. 8
0
def rank_2_H_update(H, s, y, d):
    Hy = np.matmul(H, y)
    temp = np.outer(s - Hy, d)
    ddT = np.outer(d, d)
    dTy = np.inner(d, y)
    return H + (temp + np.transpose(temp)) / dTy - np.inner(
        y, s - Hy) * ddT / (dTy**2)
Esempio n. 9
0
    def dPositionVectordh(self, x, mu):
        """
        derivatives of position vector wrt angular momentum vector
        typed
        """
        TA = x[5]
        cTA = np.cos(TA)
        sTA = np.sin(TA)
        h = self.hVector(x)
        e = self.eVector(x, mu)
        hMag = self.hMag(x)
        eMag = self.eMag(x, mu)
        eUnit = e / eMag
        crossProduct = np.cross(h, e)
        crossProductMag = np.linalg.norm(crossProduct)

        factor = 1.0 / (mu * (1.0 + eMag * cTA))

        term1 = cTA * np.outer(eUnit, 2.0 * h)
        #term2 = sTA * np.dot((1.0 / crossProductMag) * (np.identity(3) - (1.0 / crossProductMag**2) * np.outer(crossProduct, crossProduct)), -self.mathUtil.crossmat(e))

        term2 = (sTA / crossProductMag) * (
            2. * np.outer(crossProduct, h) + hMag**2 * np.dot(
                (-np.identity(3) + (1. / crossProductMag**2) * np.outer(
                    crossProduct, crossProduct)), self.mathUtil.crossmat(e)))
        drdh = factor * (term1 + term2)
        return drdh
Esempio n. 10
0
    def dVelocityVectordh(self, x, mu):
        """
        derivatives of Velocity vector wrt angular momentum vector
        typed
        """
        TA = x[5]
        sTA = np.sin(TA)
        cTA = np.cos(TA)
        h = self.hVector(x)
        e = self.eVector(x, mu)
        eCross = self.mathUtil.crossmat(e)
        hMag = np.linalg.norm(h)
        eMag = np.linalg.norm(e)
        eUnit = e / eMag
        crossProduct = np.cross(h, e)
        crossProductMag = np.linalg.norm(crossProduct)

        factor1 = 1.0 / hMag**3
        term1 = factor1 * (-np.outer(
            sTA * eUnit - ((eMag + cTA) / crossProductMag) * crossProduct, h))
        factor2 = (-(eMag + cTA)) / (hMag * crossProductMag)
        term2 = factor2 * (-eCross + (1.0 / crossProductMag**2) * np.dot(
            np.outer(crossProduct, crossProduct), eCross))

        dvdh = -mu * (term1 + term2)
        return dvdh
Esempio n. 11
0
    def dVelocityVectorde(self, x, mu):
        """
        derivatives of Velocity vector wrt eccentricity vector
        typed
        """
        TA = x[5]
        sTA = np.sin(TA)
        cTA = np.cos(TA)
        h = self.hVector(x)
        e = self.eVector(x, mu)
        hCross = self.mathUtil.crossmat(h)
        hMag = np.linalg.norm(h)
        eMag = np.linalg.norm(e)
        crossProduct = np.cross(h, e)
        crossProductMag = np.linalg.norm(crossProduct)

        term1 = sTA * (1.0 / eMag) * (np.identity(3) -
                                      (1.0 / eMag**2) * np.outer(e, e))
        term21 = np.outer((1.0 / crossProductMag) * crossProduct,
                          (1.0 / eMag) * e)
        term22 = (eMag + cTA) * (1.0 / crossProductMag) * (
            hCross - (1.0 / crossProductMag**2) *
            np.dot(np.outer(crossProduct, crossProduct), hCross))
        term2 = -(term21 + term22)

        factor = -mu / hMag

        dvde = factor * (term1 + term2)
        return dvde
Esempio n. 12
0
def pylds_E_step(lds, data):
    T = data.shape[0]
    mu_init, sigma_init, A, sigma_states, C, sigma_obs = lds
    normalizer, smoothed_mus, smoothed_sigmas, E_xtp1_xtT = \
        _E_step(mu_init, sigma_init, A, sigma_states, C, sigma_obs, data)

    EyyT = data.T.dot(data)
    EyxT = data.T.dot(smoothed_mus)
    ExxT = smoothed_sigmas.sum(0) + smoothed_mus.T.dot(smoothed_mus)

    E_xt_xtT = \
        ExxT - (smoothed_sigmas[-1]
                + np.outer(smoothed_mus[-1],smoothed_mus[-1]))
    E_xtp1_xtp1T = \
        ExxT - (smoothed_sigmas[0]
                + np.outer(smoothed_mus[0], smoothed_mus[0]))
    E_xtp1_xtT = E_xtp1_xtT.sum(0)

    E_x1_x1T = smoothed_sigmas[0] + np.outer(smoothed_mus[0], smoothed_mus[0])
    E_x1 = smoothed_mus[0]

    E_init_stats = E_x1_x1T, E_x1, 1.
    E_pairwise_stats = E_xt_xtT, E_xtp1_xtT.T, E_xtp1_xtp1T, T-1
    E_node_stats = ExxT, EyxT.T, EyyT, T

    return E_init_stats, E_pairwise_stats, E_node_stats
Esempio n. 13
0
def hypergeom_mat(N, n):
    K = np.outer(np.ones(n + 1), np.arange(N + 1))
    k = np.outer(np.arange(n + 1), np.ones(N + 1))
    ret = log_binom(K, k)
    ret = ret + ret[::-1, ::-1]
    ret = ret - log_binom(N, n)
    return np.exp(ret)
Esempio n. 14
0
    def step3(L, Sigma, mu, mun):
        temp2 = np.dot(-J12.T, Sigma)
        Sigma_21 = solve_posdef_from_cholesky(L, temp2)

        ExnxT = Sigma_21 + np.outer(mun, mu)
        ExxT = Sigma + np.outer(mu, mu)

        return mu, ExxT, ExnxT
Esempio n. 15
0
def rank_2_H_update(H,s,y,d):
    Hy = np.matmul(H,y)
    temp = np.outer(s-Hy,d)
    ddT = np.outer(d,d)
    dTy = np.inner(d,y)

    if dTy**2 == 0:
        raise ZeroDivisionError

    return H + (temp + np.transpose(temp))/dTy - np.inner(y,s-Hy)*ddT/(dTy**2)
def costL1(K):
    loss = 0.
    Ker = np.dot(K, K.T)
    for q in S:
        i, j, k = q
        Mt = (2. * np.outer(X[i], X[j]) - 2. * np.outer(X[i], X[k]) -
              np.outer(X[j], X[j]) + np.outer(X[k], X[k]))
        loss = loss + np.log(1. + np.exp(-np.trace(np.dot(Mt, Ker))))
    # add L1 penalty
    loss = loss / len(S) + lam1 * norm1(Ker.flatten())
    return loss
Esempio n. 17
0
def test_expected_multivariate_normal_logpdf_simple(D=10):
    # Test single datapoint log pdf
    x = npr.randn(D)
    mu = npr.randn(D)
    L = npr.randn(D, D)
    Sigma = np.dot(L, L.T)

    # Check that when the covariance is zero we get the regular mvn pdf
    xxT = np.outer(x, x)
    mumuT = np.outer(mu, mu)
    ll1 = expected_multivariate_normal_logpdf(x, xxT, mu, mumuT, Sigma)
    ll2 = multivariate_normal_logpdf(x, mu, Sigma)
    assert np.allclose(ll1, ll2)
Esempio n. 18
0
 def compute_operator_from_data(self, datain, cdata, dataout):
     # for i in range(len(cdata)-1):
     self.counter += 1
     fk = np.hstack((psix(datain), np.dot(psiu(datain), cdata)))
     fkpo = np.hstack((psix(dataout), np.dot(psiu(dataout), cdata)))
     self.G = self.G + (np.outer(fk, fk) - self.G) / self.counter
     self.A = self.A + (np.outer(fk, fkpo) - self.A) / self.counter
     # self.G = self.G + np.outer(fk, fk)
     # self.A = self.A + np.outer(fk, fkpo)
     self.K = np.linalg.pinv(self.G).dot(self.A)
     Kcont = np.real(logm(self.K, disp=False)[0] / self.sampling_time)
     self.Kx = Kcont.T[0:NUM_STATE_OBS_, 0:NUM_STATE_OBS_]
     self.Ku = Kcont.T[0:NUM_STATE_OBS_, NUM_STATE_OBS_:NUM_OBS_]
    def _kernel(self, sigma, X, basis):
        if basis is None:
            basis = X

        n_x, d = X.shape
        n_y, d = basis.shape
        #dist = -2*np.matmul(np.multiply(X, 1./sigma),basis.T) + np.outer(np.matmul(np.square(X), (1./sigma).T), np.ones([1, n_y])) + np.outer( np.ones([n_x,1]),np.matmul(np.square(basis),(1./sigma).T))
        dist = -2 * np.matmul(np.multiply(X, sigma), basis.T) + np.outer(
            np.reshape(np.matmul(np.square(X), sigma.T), [-1, 1]),
            np.ones([1, n_y])) + np.outer(
                np.ones([n_x, 1]),
                np.reshape(np.matmul(np.square(basis), sigma.T), [1, -1]))
        return np.exp(-dist)
Esempio n. 20
0
 def _square_dist(self, X, basis=None):
     if basis is None:
         n, d = X.shape
         dist = np.matmul(X, X.T)
         diag_dist = np.outer(np.diagonal(dist), np.ones([1, n]))
         dist = diag_dist + diag_dist.T - 2 * dist
     else:
         n_x, d = X.shape
         n_y, d = basis.shape
         dist = -2 * np.matmul(X, basis.T) + np.outer(
             np.sum(np.square(X), axis=1), np.ones([1, n_y])) + np.outer(
                 np.ones([n_x, 1]), np.sum(np.square(basis), axis=1))
     return dist
Esempio n. 21
0
def alleged_BFGS(n, f, dfdx,x_start, TEMP_B0, BFGS_alpha):
    # Initialize delta_xq and gamma
    delta_xq = np.zeros((2, 1))
    gamma = np.zeros((2, 1))
    part1 = np.zeros((2, 2))
    part2 = np.zeros((2, 2))
    part3 = np.zeros((2, 2))
    part4 = np.zeros((2, 2))
    part5 = np.zeros((2, 2))
    part6 = np.zeros((2, 1))
    part7 = np.zeros((1, 1))
    part8 = np.zeros((2, 2))
    part9 = np.zeros((2, 2))
    # Initialize xq
    xq = np.empty((n + 1, 2)) 
    xq[0] = x_start
    # Initialize gradient storage
    g = np.zeros((n + 1, 2))
    g[0] = dfdx(xq[0])
    # Initialize hessian storage
    h = np.zeros((n + 1, 2, 2))
    h[0] = TEMP_B0
    for i in range(n):

        search_dirn = np.linalg.solve(h[i], g[i])
        # Compute search direction and magnitude (dx)
        #  with dx = -alpha * inv(h) * grad
        delta_xq = -np.dot(BFGS_alpha[i], np.linalg.solve(h[i], g[i]))
        # delta_xq = - np.linalg.solve(h[i], g[i])

        xq[i + 1] = xq[i] + delta_xq

        # Get gradient update for next step
        g[i + 1] = dfdx(xq[i + 1])

        # Get hessian update for next step
        gamma = g[i + 1] - g[i]
        part1 = np.outer(gamma, gamma)
        part2 = np.outer(gamma, delta_xq)
        part3 = np.dot(np.linalg.pinv(part2), part1)

        part4 = np.outer(delta_xq, delta_xq)
        part5 = np.dot(h[i], part4)
        part6 = np.dot(part5, h[i])
        part7 = np.dot(delta_xq, h[i])
        part8 = np.dot(part7, delta_xq)
        part9 = np.dot(part6, 1 / part8)

        h[i + 1] = h[i] + part3 - part9

    return xq
Esempio n. 22
0
    def dPositionVectorde(self, x, mu):
        """
        derivatives of position vector wrt eccentricity vector
        typed
        """
        TA = x[5]
        cTA = np.cos(TA)
        sTA = np.sin(TA)
        h = self.hVector(x)
        e = self.eVector(x, mu)
        hMag = self.hMag(x)
        eMag = self.eMag(x, mu)
        eUnit = e / eMag
        crossProduct = np.cross(h, e)
        crossProductMag = np.linalg.norm(crossProduct)
        crossProductUnit = crossProduct / crossProductMag
        factor = hMag**2 / mu
        #term1_old = np.outer((-cTA / ((1.0 + eMag * cTA)**2)) * eUnit, eUnit * cTA + crossProduct / crossProductMag * sTA)

        term1 = (-cTA / (1. + eMag * cTA)**2) * np.outer(
            cTA * eUnit + sTA * crossProductUnit, eUnit)

        factor2 = 1.0 / (1.0 + eMag * cTA)
        #term2_old = factor2 * ((cTA / eMag) * (np.identity(3) - (1.0 / eMag**2) * np.outer(e, e)) + (sTA / crossProductMag) * np.dot((np.identity(3) - (1.0 / crossProductMag**2) * np.outer(crossProduct, crossProduct)), self.mathUtil.crossmat(h)))

        term2 = factor2 * (
            (cTA / eMag) * (np.identity(3) - (1. / eMag**2) * np.outer(e, e)) +
            (sTA / crossProductMag) * np.dot(
                (np.identity(3) - (1. / crossProductMag**2) * np.outer(
                    crossProduct, crossProduct)), self.mathUtil.crossmat(h)))

        #term2 = term2_old
        # print(term1_old)
        # print("\n")
        # print(term1)
        # print("\n")
        # print("\n")
        # print("\n")
        # print("\n")
        # print(term2_old)
        # print("\n")
        # print(term2)
        # print("\n")
        # print(term3)
        # print("\n")

        drde = factor * (term1 + term2)
        return drde
Esempio n. 23
0
    def _sample_mu_k_Sigma_k(self, k):
        """Draw posterior updates for `mu_k` and `Sigma_k`.
        """
        W_k = self.W[self.Z == k]
        N_k = W_k.shape[0]

        if N_k > 0:
            W_k_bar = W_k.mean(axis=0)
            diff = W_k - W_k_bar
            mu_post = (self.prior_obs * self.mu0) + (N_k * W_k_bar)
            mu_post /= (N_k + self.prior_obs)
            SSE = np.dot(diff.T, diff)
            prior_diff = W_k_bar - self.mu0
            SSE_prior = np.outer(prior_diff.T, prior_diff)
            nu_post = self.nu0 + N_k
            lambda_post = self.prior_obs + N_k
            Psi_post = self.Psi0 + SSE
            Psi_post += ((self.prior_obs * N_k) / lambda_post) * SSE_prior

            self.Sigma[k] = invwishart.rvs(nu_post, Psi_post)
            cov = self.Sigma[k] / lambda_post
            self.mu[k] = self.rng.multivariate_normal(mu_post, cov)
        else:
            self.Sigma[k] = invwishart.rvs(self.nu0, self.Psi0)
            cov = self.Sigma[k] / self.prior_obs
            self.mu[k] = self.rng.multivariate_normal(self.mu0, cov)
Esempio n. 24
0
    def _posterior_mvn_t(self, W_k, W_star_m):
        """Calculates the multivariate-t likelihood for joining new cluster.
        """
        if W_k.shape[0] > 0:
            W_bar = W_k.mean(axis=0)
            diff = W_k - W_bar
            SSE = np.dot(diff.T, diff)
            N_k = W_k.shape[0]
            prior_diff = W_bar - self.mu0
            SSE_prior = np.outer(prior_diff.T, prior_diff)
        else:
            W_bar = 0.
            SSE = 0.
            N_k = 0.
            SSE_prior = 0.

        mu_posterior = (self.prior_obs * self.mu0) + (N_k * W_bar)
        mu_posterior /= (N_k + self.prior_obs)
        nu_posterior = self.nu0 + N_k
        lambda_posterior = self.prior_obs + N_k
        psi_posterior = self.Psi0 + SSE
        psi_posterior += ((self.prior_obs * N_k) /
                          (self.prior_obs + N_k)) * SSE_prior
        psi_posterior *= (lambda_posterior +
                          1.) / (lambda_posterior *
                                 (nu_posterior - self.D + 1.))
        df_posterior = (nu_posterior - self.D + 1.)
        return multivariate_t.logpdf(W_star_m, mu_posterior, psi_posterior,
                                     df_posterior)
Esempio n. 25
0
def angle_axis_rotation_matrix(angle, axis, axis_already_normalized=False):
    # Gives the rotation matrix from an angle and an axis.
    # An implmentation of https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
    # Inputs:
    #   * angle: can be one angle or a vector (1d ndarray) of angles. Given in radians.
    #   * axis: a 1d numpy array of length 3 (x,y,z). Represents the angle.
    #   * axis_already_normalized: boolean, skips normalization for speed if you flag this true.
    # Outputs:
    #   * If angle is a scalar, returns a 3x3 rotation matrix.
    #   * If angle is a vector, returns a 3x3xN rotation matrix.
    if not axis_already_normalized:
        axis = axis / np.linalg.norm(axis)

    sintheta = np.sin(angle)
    costheta = np.cos(angle)
    cpm = np.array(
        [[0, -axis[2], axis[1]],
         [axis[2], 0, -axis[0]],
         [-axis[1], axis[0], 0]]
    )  # The cross product matrix of the rotation axis vector
    outer_axis = np.outer(axis, axis)

    angle = np.array(angle)  # make sure angle is a ndarray
    if len(angle.shape) == 0:  # is a scalar
        rot_matrix = costheta * np.eye(3) + sintheta * cpm + (1 - costheta) * outer_axis
        return rot_matrix
    else:  # angle is assumed to be a 1d ndarray
        rot_matrix = costheta * np.expand_dims(np.eye(3), 2) + sintheta * np.expand_dims(cpm, 2) + (
                1 - costheta) * np.expand_dims(outer_axis, 2)
        return rot_matrix
Esempio n. 26
0
def prediction_grad(x, y, W, V, b, c):
    """
    Function to compute gradients for 1 hidden layer neural network
    """
    l1_out = np.dot(W, x) + b
    l1_act = np.tanh(l1_out)
    final_out = np.dot(V, l1_act) + c

    ex_fv = np.exp(final_out)
    ex_sum = np.sum(np.exp(final_out))
    gt_calc = ex_fv / ex_sum

    init_unit = np.zeros(len(c))
    init_unit[y] = 1

    der_l1_act = 1 - np.square(l1_act)  #Check this part

    dldf = -init_unit.reshape(len(c), 1) + gt_calc
    dldc = dldf
    dldv = np.dot(dldf, l1_act.T)

    dldb = der_l1_act * (np.dot(V.T, dldf))

    dldw = np.outer(dldb, x.T)

    return dldw, dldv, dldb, dldc
    def predict_cumulative_hazard(self, X, times=None, ancillary_X=None):
        """
        Return the cumulative hazard rate of subjects in X at time points.

        Parameters
        ----------
        X: numpy array or DataFrame
            a (n,d) covariate numpy array or DataFrame. If a DataFrame, columns
            can be in any order. If a numpy array, columns must be in the
            same order as the training data.
        times: iterable, optional
            an iterable of increasing times to predict the cumulative hazard at. Default
            is the set of all durations (observed and unobserved). Uses a linear interpolation if
            points in time are not in the index.
        ancillary_X: numpy array or DataFrame, optional
            a (n,d) covariate numpy array or DataFrame. If a DataFrame, columns
            can be in any order. If a numpy array, columns must be in the
            same order as the training data.

        Returns
        -------
        cumulative_hazard_ : DataFrame
            the cumulative hazard of individuals over the timeline
        """
        times = coalesce(times, self.timeline, np.unique(self.durations))
        alpha_, beta_ = self._prep_inputs_for_prediction_and_return_scores(X, ancillary_X)
        return pd.DataFrame(np.log1p(np.outer(times, 1 / alpha_) ** beta_), columns=_get_index(X), index=times)
Esempio n. 28
0
def generalized_outer_product(mat):
    if len(mat.shape) == 1:
        return np.outer(mat, mat)
    elif len(mat.shape) == 2:
        return np.einsum('ij,ik->ijk', mat, mat)
    else:
        raise ArithmeticError
Esempio n. 29
0
def rfnorm2mat(normal):
    """ Matrix to reflect in plane through origin, orthogonal to `normal`

    Parameters
    ----------
    normal : array-like, shape (3,)
       vector normal to plane of reflection

    Returns
    -------
    mat : array shape (3,3)

    Notes
    -----
    http://en.wikipedia.org/wiki/Reflection_(mathematics)

    The reflection of a vector `v` in a plane normal to vector `a` is:

    .. math::

       \\mathrm{Ref}_a(v) = v - 2\\frac{v\\cdot a}{a\\cdot a}a

    The entries of the corresponding orthogonal transformation matrix
    `R` are given by:

    .. math::

       R_{ij} = I_{ij} - 2\\frac{a_i a_j}{\|a\|^2}

    where $I$ is the identity matrix.
    """
    normal = np.asarray(normal, dtype=np.float)
    norm2 = (normal**2).sum()
    M = np.eye(3)
    return M - 2.0 * np.outer(normal, normal) / norm2
def generalized_outer_product(mat):
    if len(mat.shape) == 1:
        return np.outer(mat, mat)
    elif len(mat.shape) == 2:
        return np.einsum('ij,ik->ijk', mat, mat)
    else:
        raise ArithmeticError
Esempio n. 31
0
    def predict_cumulative_hazard(self, X, times=None, ancillary_X=None):
        """
        Return the cumulative hazard rate of subjects in X at time points.

        Parameters
        ----------
        X: numpy array or DataFrame
            a (n,d) covariate numpy array or DataFrame. If a DataFrame, columns
            can be in any order. If a numpy array, columns must be in the
            same order as the training data.
        times: iterable, optional
            an iterable of increasing times to predict the cumulative hazard at. Default
            is the set of all durations (observed and unobserved). Uses a linear interpolation if
            points in time are not in the index.
        ancillary_X: numpy array or DataFrame, optional
            a (n,d) covariate numpy array or DataFrame. If a DataFrame, columns
            can be in any order. If a numpy array, columns must be in the
            same order as the training data.

        Returns
        -------
        cumulative_hazard_ : DataFrame
            the cumulative hazard of individuals over the timeline
        """
        times = coalesce(times, self.timeline, np.unique(self.durations))
        alpha_, beta_ = self._prep_inputs_for_prediction_and_return_scores(X, ancillary_X)
        return pd.DataFrame(np.log1p(np.outer(times, 1 / alpha_) ** beta_), columns=_get_index(X), index=times)
Esempio n. 32
0
    def rVector_derivs(self, x):
        """
        bplane R unit vector derivatives
        typed
        """

        s = self.sVector(x)
        t = self.tVector(x)
        crossProduct = np.cross(s, t)
        crossProductMag = np.linalg.norm(crossProduct)
        dcrossProductds, dcrossProductdt = self.mathUtil.d_crossproduct(s, t)
        dsdx = self.sVector_derivs(x)
        dtdx = self.tVector_derivs(x)
        dRdxNotUnit = np.dot(dcrossProductds, dsdx) + np.dot(
            dcrossProductdt, dtdx)
        dOneByCrossProductMagdx = (-1.0 / crossProductMag**3) * np.dot(
            crossProduct, dRdxNotUnit)
        dRdx = (1.0 / crossProductMag) * dRdxNotUnit + np.outer(
            crossProduct, dOneByCrossProductMagdx)

        #term1 = (1.0 / crossProductMag) * dRdxNotUnit
        #term2 = np.outer(crossProduct, dOneByCrossProductMagdx)
        #print('***')
        #print('dRdx')
        #print('crossProduct: ', crossProduct)
        #print('dRdxNotUnit column1: ', dRdxNotUnit[0:3,1])
        #print(crossProduct[0]*dRdxNotUnit[0,1], crossProduct[1]*dRdxNotUnit[1,1])
        #print(crossProduct[2], dOneByCrossProductMagdx[1])
        #print(dRdxNotUnit[2,1], term2[2,1])
        #print('***')
        return dRdx
Esempio n. 33
0
    def tVector_derivs(self, x):
        """
        derivatives of the T unit vector
        typed
        """
        # choose the reference vector here
        k = self.mathUtil.k_unit()
        referenceVector = k
        dreferenceVectordx = np.zeros(
            (3,
             6))  # for k reference vector (or any constant reference vector)

        s = self.sVector(x)
        crossProduct = np.cross(s, referenceVector)
        crossProductMag = np.linalg.norm(crossProduct)
        dcrossProductds, dcrossProductdreferenceVector = self.mathUtil.d_crossproduct(
            s, referenceVector)

        dsdx = self.sVector_derivs(x)
        dtdxNotUnit = np.dot(dcrossProductds, dsdx) + np.dot(
            dcrossProductdreferenceVector, dreferenceVectordx)
        dOneByCrossProductMagdx = (-1.0 / crossProductMag**3) * np.dot(
            crossProduct, dtdxNotUnit)
        dtdx = (1.0 / crossProductMag) * dtdxNotUnit + np.outer(
            crossProduct, dOneByCrossProductMagdx)

        return dtdx
Esempio n. 34
0
def test_jacobian_higher_order():
    fun = lambda x: np.sin(np.outer(x, x)) + np.cos(np.dot(x, x))

    jacobian(fun)(npr.randn(3)).shape == (3, 3, 3)
    jacobian(jacobian(fun))(npr.randn(3)).shape == (3, 3, 3, 3)
    jacobian(jacobian(jacobian(fun)))(npr.randn(3)).shape == (3, 3, 3, 3, 3)

    check_grads(lambda x: np.sum(jacobian(fun)(x)), npr.randn(3))
    check_grads(lambda x: np.sum(jacobian(jacobian(fun))(x)), npr.randn(3))
Esempio n. 35
0
def diff(r, R, u):
    """ return Jacobian of exp(r_hat) * u
    R = exp(r) <- caller will already have this, so pass it in """
    # derivation: http://arxiv.org/abs/1312.0788
    uhat = hat(u)
    rnorm = np.dot(r, r)
    if rnorm == 0:
        return -uhat
    return -np.dot(np.dot(R, uhat), (
        np.outer(r, r) + np.dot(R.T - np.eye(3), hat(r)))) / rnorm
Esempio n. 36
0
def natural_rts_backward_step(next_smooth, next_pred, filtered, pair_param):
    # p = "predicted", f = "filtered", s = "smoothed", n = "next"
    (Jns, hns, mun), (Jnp, hnp), (Jf, hf) = next_smooth, next_pred, filtered
    J11, J12, J22, _ = pair_param

    # convert from natural parameter to the usual J definitions
    Jns, Jnp, Jf, J11, J12, J22 = -2*Jns, -2*Jnp, -2*Jf, -2*J11, -J12, -2*J22

    J11, J12, J22 = Jf + J11, J12, Jns - Jnp + J22
    L = np.linalg.cholesky(J22)
    temp = solve_triangular(L, J12.T)
    Js = J11 - np.dot(temp.T, temp)
    hs = hf - np.dot(temp.T, solve_triangular(L, hns - hnp))

    mu, sigma = info_to_mean((Js, hs))
    ExnxT = -solve_posdef_from_cholesky(L, np.dot(J12.T, sigma)) + np.outer(mun, mu)

    ExxT = sigma + np.outer(mu, mu)

    return -1./2*Js, hs, (mu, ExxT, ExnxT)
Esempio n. 37
0
def diff(r, R, u):
    """ return Jacobian of exp(r_hat) * u
    R = exp(r) <- caller will already have this, so pass it in """
    # derivation: http://arxiv.org/abs/1312.0788
    # THIS IS WRONG! which even a rudimentary test will prove
    # for now use cv2.Rodrigues
    uhat = hat(u)
    rnorm = np.dot(r, r)
    if rnorm == 0:
        return -uhat
    return -np.dot(np.dot(R, uhat), (
        np.outer(r, r) + np.dot(R.T - np.eye(3), hat(r)))) / rnorm
Esempio n. 38
0
def pylds_E_step_inhomog(lds, data):
    T = data.shape[0]
    mu_init, sigma_init, A, sigma_states, C, sigma_obs = lds
    normalizer, smoothed_mus, smoothed_sigmas, E_xtp1_xtT = \
        _E_step(mu_init, sigma_init, A, sigma_states, C, sigma_obs, data)

    EyyT = np.einsum('ti,tj->tij', data, data)
    EyxT = np.einsum('ti,tj->tij', data, smoothed_mus)
    ExxT = smoothed_sigmas + np.einsum('ti,tj->tij', smoothed_mus, smoothed_mus)

    E_xt_xtT = ExxT[:-1]
    E_xtp1_xtp1T = ExxT[1:]
    E_xtp1_xtT = E_xtp1_xtT

    E_x1_x1T = smoothed_sigmas[0] + np.outer(smoothed_mus[0], smoothed_mus[0])
    E_x1 = smoothed_mus[0]

    E_init_stats = E_x1_x1T, E_x1, 1.
    E_pairwise_stats = E_xt_xtT.sum(0), E_xtp1_xtT.sum(0).T, E_xtp1_xtp1T.sum(0), T-1
    E_node_stats = ExxT, np.transpose(EyxT, (0, 2, 1)), EyyT, np.ones(T)

    return E_init_stats, E_pairwise_stats, E_node_stats
Esempio n. 39
0
def covgrad(x, mean, cov):
    # I think once we have Cholesky we can make this nicer.
    solved = np.linalg.solve(cov, x - mean)
    return lower_half(np.linalg.inv(cov) - np.outer(solved, solved))
Esempio n. 40
0
 def fun( x, y): return to_scalar(np.outer(x, y))
 def dfun(x, y): return to_scalar(grad(fun)(x, y))
Esempio n. 41
0
 def unit(filtered_message):
     J, h = filtered_message
     mu, Sigma = natural_to_mean(filtered_message)
     ExxT = Sigma + np.outer(mu, mu)
     return make_tuple(J, h, mu), [(mu, ExxT, 0.)]
Esempio n. 42
0
def generalized_outer_product(x):
    if np.ndim(x) == 1:
        return np.outer(x, x)
    return np.matmul(x, np.swapaxes(x, -1, -2))