Beispiel #1
0
def test_pos_recipr():
    X = np.array([2, 1, -1, 0], dtype=np.int8)
    eX = np.array([0.5, 1, 0, 0])
    Y = positive_reciprocal(X)
    assert_array_almost_equal, Y, eX
    assert Y.dtype.type == np.float64
    X2 = X.reshape((2, 2))
    Y2 = positive_reciprocal(X2)
    assert_array_almost_equal, Y2, eX.reshape((2, 2))
    # check that lists have arrived
    XL = [0, 1, -1]
    assert_array_almost_equal, positive_reciprocal(XL), [0, 1, 0]
    # scalars
    assert positive_reciprocal(-1) == 0
    assert positive_reciprocal(0) == 0
    assert positive_reciprocal(2) == 0.5
Beispiel #2
0
    def normalized_residuals(self):
        """Residuals, normalized to have unit length.

        See [1]_ and [2]_.

        Notes
        -----
        Is this supposed to return "stanardized residuals,"
        residuals standardized
        to have mean zero and approximately unit variance?

        d_i = e_i / sqrt(MS_E)

        Where MS_E = SSE / (n - k)

        References
        ----------
        .. [1] Montgomery and Peck 3.2.1 p. 68

        .. [2] Davidson and MacKinnon 15.2 p 662

        """
        return self.residuals * positive_reciprocal(np.sqrt(self.dispersion))
Beispiel #3
0
    def Fcontrast(self, matrix, dispersion=None, invcov=None):
        """ Compute an Fcontrast for a contrast matrix `matrix`.

        Here, `matrix` M is assumed to be non-singular. More precisely

        .. math::

            M pX pX' M'

        is assumed invertible. Here, :math:`pX` is the generalized inverse of
        the design matrix of the model.
        There can be problems in non-OLS models where
        the rank of the covariance of the noise is not full.

        See the contrast module to see how to specify contrasts.
        In particular, the matrices from these contrasts will always be
        non-singular in the sense above.

        Parameters
        ----------
        matrix : 1D array-like
            Contrast matrix.

        dispersion : None or float, optional
            If None, use ``self.dispersion``.

        invcov : None or array, optional
            Known inverse of variance covariance matrix.
            If None, calculate this matrix.

        Returns
        -------
        f_res : ``FContrastResults`` instance
            with attributes F, df_den, df_num

        Notes
        -----
        For F contrasts, we now specify an effect and covariance.

        """
        matrix = np.asarray(matrix)
        # 1D vectors assumed to be row vector
        if matrix.ndim == 1:
            matrix = matrix[None]
        if matrix.shape[1] != self.theta.shape[0]:
            raise ValueError("F contrasts should have shape[1] P=%d, "
                             "but this has shape[1] %d" %
                             (self.theta.shape[0], matrix.shape[1]))
        ctheta = np.dot(matrix, self.theta)
        if matrix.ndim == 1:
            matrix = matrix.reshape((1, matrix.shape[0]))
        if dispersion is None:
            dispersion = self.dispersion
        q = matrix.shape[0]
        if invcov is None:
            invcov = inv(self.vcov(matrix=matrix, dispersion=1.0))
        F = (np.add.reduce(np.dot(invcov, ctheta) * ctheta, 0) *
             positive_reciprocal((q * dispersion)))
        F = np.squeeze(F)
        return FContrastResults(effect=ctheta,
                                covariance=self.vcov(
                                    matrix=matrix,
                                    dispersion=dispersion[np.newaxis]),
                                F=F,
                                df_den=self.df_residuals,
                                df_num=invcov.shape[0])