コード例 #1
0
ファイル: MCMCstats.py プロジェクト: obscode/CSPMCMC
def error_ellipse(x, y, center='mean', level=0.68):
    '''compute the position, major axis, minor axis, and position angle
   of an ellipse that represents and [level] confidence interval.'''

    if center == 'mean':
        p1 = mean(x)
        p2 = mean(y)
    else:
        p1 = median(x)
        p2 = median(y)

    covar = covarc([x, y], center)

    eigval, eigvec = linalg.eig(covar)
    eigval = map(abs, eigval)

    dchi2 = brentq(lambda x: chdtr(2, x) - level, 0, 100)

    if eigval[0] > eigval[1]:
        major = sqrt(eigval[0] * dchi2)
        minor = sqrt(eigval[1] * dchi2)
        pa = arctan2(eigvec[1, 0], eigvec[0, 0]) * 180.0 / pi
    else:
        major = sqrt(eigval[1] * dchi2)
        minor = sqrt(eigval[0] * dchi2)
        pa = arctan2(eigvec[1, 1], eigvec[0, 1]) * 180.0 / pi

    return (p1, p2, major, minor, pa)
コード例 #2
0
def neg_2_log_likelihood_ratio_CDF(l, mu, sigma):
    """Calculate the CDF of -2log(likelihood ratio) CDF.

    Parameters
    ----------
    l : float
        The :math:`l = \lambda = -2ln(\Lambda)` value to calculate the CDF at.
    mu : float
        The expectation value to test.
    sigma : float
        The standard deviation of the distribution.

    Returns
    -------
    prob : float (between 0 and 1)
        The probability :math:`CDF(l) = Prob(\lambda < l)`.
    """
    # p_lz = stats.norm.cdf(0, loc=mu, scale=sigma)
    p_lz = special.ndtr(-mu / sigma)
    p_gz = 1 - p_lz

    l_crit = mu**2 / sigma**2
    # chi2 = stats.chi2(1)
    # nu = chi2.cdf(l_crit)
    nu = special.chdtr(1, l_crit)

    if l < l_crit:
        # p_plus = p_gz * 2 * chi2.cdf(l) / (1.0 + nu)
        p_plus = p_gz * 2 * special.chdtr(1, l) / (1.0 + nu)
    else:
        # p_plus = p_gz * (chi2.cdf(l) + nu) / (1.0 + nu)
        p_plus = p_gz * (special.chdtr(1, l) + nu) / (1.0 + nu)

    tau = 0.5 * sigma**2 / mu * (mu**2 / sigma**2 - l)
    if tau < 0:
        # p_minus = p_lz - stats.norm.cdf(tau, loc=mu, scale=sigma)
        p_minus = p_lz - special.ndtr((tau - mu) / sigma)
    else:
        p_minus = 0
    return p_plus + p_minus
コード例 #3
0
    def fit_for_A(self, sep, del_z_bar, N, b_g_rel, c_rel, n, Cov_mat,
                  covar_type, nbins):
        self.c_rel = c_rel
        self.get_quad_coeff_for_alpha(b_g_rel, n)
        A_step, dA = np.linspace(0., 10., 199, endpoint=True, retstep=True)
        prob_step = np.asarray([
            np.exp(-0.5 * self.chi_sqrd(A_i, del_z_bar, sep, Cov_mat))
            for A_i in A_step
        ])
        prob_step *= 1. / simp_integral(dA, prob_step)
        exp_A = simp_integral(dA, prob_step * A_step)
        var_A = simp_integral(dA, prob_step * np.power(A_step, 2)) - np.power(
            exp_A, 2)

        plt.plot(A_step, prob_step)
        plt.axvline(x=exp_A, ymin=0, ymax=1, color='k')
        plt.axvline(x=exp_A - np.sqrt(var_A),
                    ymin=0,
                    ymax=1,
                    color='k',
                    linestyle='--')
        plt.axvline(x=exp_A + np.sqrt(var_A),
                    ymin=0,
                    ymax=1,
                    color='k',
                    linestyle='--')
        plt.xlabel('A')
        plt.ylabel('Probability of fit')
        plt.savefig('auto/probability/Prob_A_{0}_{1}.png'.format(
            n, covar_type))
        plt.close()

        CP_A = chdtr(nbins - 1, self.chi_sqrd(0, del_z_bar, sep, Cov_mat))
        signif_A = np.sqrt(2) * erfinv(CP_A)
        j = (np.where(
            np.array([
                simp_integral(dA, prob_step[:i])
                for i in range(3, A_step.size + 1, 2)
            ]) > 0.95)[0][0] + 1) * 2
        diff = 0.95 - simp_integral(dA, prob_step[:j - 1])
        if 0.5 * dA * (prob_step[j - 2] + prob_step[j - 1]) > diff:
            A_95 = A_step[j - 2] + 0.5 * dA
        else:
            A_95 = A_step[j - 1] + 0.5 * dA

        print("   Best fit A: {0}".format(exp_A))
        print("   error bars: {0}".format(np.sqrt(var_A)))
        print("   significance: {0}".format(signif_A))
        print("   Chi^2 of fit: {0}".format(
            self.chi_sqrd(exp_A, del_z_bar, sep, Cov_mat)))
        print("   95% confidence: {0}".format(A_95))
        return exp_A, var_A
コード例 #4
0
ファイル: chi_square.py プロジェクト: jayden11/pytools
    def cdf(self, x):
        """
        Computes the cumulative distribution function of the
        distribution at the point(s) x. The cdf is defined as follows:
            F(x|k) = gammainc(k/2, x/2) / gamma(k/2)

        Where gammainc and gamma are the incomplete gamma and gamma functions,
        respectively.

        Parameters
        ----------
        x: array, dtype=float, shape=(m x n)
            The value(s) at which the user would like the cdf evaluated.
            If an array is passed in, the cdf is evaluated at every point
            in the array and an array of the same size is returned.

        Returns
        -------
        cdf: array, dtype=float, shape=(m x n)
            The cdf at each point in x.
        """
        cdf = chdtr(self.k, x)

        return cdf
コード例 #5
0
ファイル: chi_square.py プロジェクト: spencerlyon2/pytools
    def cdf(self, x):
        """
        Computes the cumulative distribution function of the
        distribution at the point(s) x. The cdf is defined as follows:
            F(x|k) = gammainc(k/2, x/2) / gamma(k/2)

        Where gammainc and gamma are the incomplete gamma and gamma functions,
        respectively.

        Parameters
        ----------
        x: array, dtype=float, shape=(m x n)
            The value(s) at which the user would like the cdf evaluated.
            If an array is passed in, the cdf is evaluated at every point
            in the array and an array of the same size is returned.

        Returns
        -------
        cdf: array, dtype=float, shape=(m x n)
            The cdf at each point in x.
        """
        cdf = chdtr(self.k, x)

        return cdf
コード例 #6
0
ファイル: core.py プロジェクト: Timothy-Edward-Kendon/pywafo
def _cdfchi2(x, df):
    return special.chdtr(df, x)
コード例 #7
0
    def fit_for_A(self, seps, del_z_bar, N, b_g_rel, c_rel, n, Cov_mat,
                  covar_type, nbins):
        # This is to isolate the n for debugging
        i, j = np.indices(Cov_mat.shape)
        mod_matrix = np.zeros(Cov_mat.shape)
        mod_matrix[np.logical_and(i == j, i >= nbins * (n - 1))] = 1
        Cov_mat_I = np.matrix(mod_matrix) * Cov_mat.I * np.matrix(mod_matrix)
        # end debugging block
        self.c_rel = c_rel
        self.get_quad_coeff_for_alpha(b_g_rel, n)
        del_z_bar_flat = del_z_bar.flatten()
        print(del_z_bar_flat)
        A_step, dA = np.linspace(0.,
                                 10.**(2 - n / 2.),
                                 199,
                                 endpoint=True,
                                 retstep=True)
        prob_step = np.asarray([
            np.exp(-0.5 *
                   self.chi_sqrd(A_i, del_z_bar_flat, seps, n, Cov_mat_I))
            for A_i in A_step
        ])
        prob_step *= 1. / simp_integral(dA, prob_step)
        exp_A = simp_integral(dA, prob_step * A_step)
        var_A = simp_integral(dA, prob_step * np.power(A_step, 2)) - np.power(
            exp_A, 2)
        # find max probability A
        bi = np.array([0, 10**(2. - n / 2.)])
        for i in range(20):
            left_chi = self.chi_sqrd(bi[0], del_z_bar_flat, seps, n, Cov_mat)
            right_chi = self.chi_sqrd(bi[1], del_z_bar_flat, seps, n, Cov_mat)
            if left_chi < right_chi:
                bi[1] = np.average(bi)
            else:
                bi[0] = np.average(bi)
        A_min_chi = np.average(bi)

        plt.plot(A_step, prob_step)
        plt.axvline(x=exp_A, ymin=0, ymax=1, color='k')
        plt.axvline(x=exp_A - np.sqrt(var_A),
                    ymin=0,
                    ymax=1,
                    color='k',
                    linestyle='--')
        plt.axvline(x=exp_A + np.sqrt(var_A),
                    ymin=0,
                    ymax=1,
                    color='k',
                    linestyle='--')
        plt.axvline(x=A_min_chi, ymin=0, ymax=1, color='r')
        plt.xlabel('A')
        plt.ylabel('Probability of fit')
        plt.savefig('auto/probability/combined/Prob_A_{0}_{1}.png'.format(
            n, covar_type))
        plt.close()

        CP_A = chdtr(nbins * n - 1,
                     self.chi_sqrd(0, del_z_bar_flat, seps, n, Cov_mat))
        signif_A = np.sqrt(2) * erfinv(CP_A)
        j = (np.where(
            np.array([
                simp_integral(dA, prob_step[:i])
                for i in range(3, A_step.size + 1, 2)
            ]) > 0.95)[0][0] + 1) * 2
        diff = 0.95 - simp_integral(dA, prob_step[:j - 1])
        if 0.5 * dA * (prob_step[j - 2] + prob_step[j - 1]) > diff:
            A_95 = A_step[j - 2] + 0.5 * dA
        else:
            A_95 = A_step[j - 1] + 0.5 * dA

        print("   Best fit A: {0}".format(exp_A))
        print("   A with max Prob: {0}".format(A_min_chi))
        print("   error bars: {0}".format(np.sqrt(var_A)))
        print("   significance: {0}".format(signif_A))
        print("   Chi^2 of fit: {0}".format(
            self.chi_sqrd(exp_A, del_z_bar_flat, seps, n, Cov_mat)))
        print("   95% confidence: {0}".format(A_95))
        return exp_A, var_A
コード例 #8
0
ファイル: core.py プロジェクト: morbult/pywafo
def _cdfchi2(x, df):
    return special.chdtr(df, x)  # pylint: disable=no-member
コード例 #9
0
ファイル: core.py プロジェクト: mikemt/pywafo
def _cdfchi2(x, df):
    return special.chdtr(df, x)