Exemplo n.º 1
0
def chi2ind_sample_size(starting_n,
                        effect_size,
                        dof,
                        power=0.8,
                        sig_level=0.05):
    """
    Tested against reference in NCSS PASS manual: 
    https://ncss-wpengine.netdna-ssl.com/wp-content/themes/ncss/pdf/Procedures/PASS/Chi-Square_Tests.pdf
    (Page 7)
    """

    n = starting_n  # initialisation
    sim_power = 0

    while sim_power < power:

        n += 1

        ncp = (effect_size**2) * n

        chi2_null = scs.chi2(df=dof, loc=0, scale=1)
        chi2_alt = scs.ncx2(df=dof, nc=ncp)

        cv = chi2_null.ppf(1 - sig_level)
        sim_power = 1 - chi2_alt.cdf(cv)

    print('ncp: ', ncp)
    print('Critical chi2: ', cv)
    print('Actual Power: ', sim_power)

    return np.ceil(n)
Exemplo n.º 2
0
def perform_kstest(k, lamda, theta, X_0, T, simulated):
    c = (2 * k) / ((1 - np.exp(-k * T)) * theta**2)
    df = 4 * k * lamda / theta**2
    nc = 2 * c * X_0 * np.exp(-k * T)
    rv = ncx2(df, nc, scale=1 / (2 * c))

    cdf = lambda x: rv.cdf(x)
    return kstest(simulated, cdf=cdf)
Exemplo n.º 3
0
def eval_log_lik(t, vt, bet=1, mu=1, sig=1):

    c = 2 * bet / (sig**2 * (1 - np.exp(-bet * np.diff(t))))
    zt = 2 * c * vt[1:]
    nc = 2 * np.exp(-bet * np.diff(t)) * c * vt[:-1]
    df = 4 * bet * mu / sig**2

    return (ncx2(df, nc).logpdf(zt) + np.log(2 * c)).sum()
Exemplo n.º 4
0
    def pdf(self,E,Ei=0):
        """ calculates decision problem densities under H0 and H1

        Parameters
        ----------

        E  : float
            Legitimate Signal Energy
        Ei : float
            Interferer Signal Energy

        See Also
        --------

        scipy.stats.chi2
        scipy.stats.ncx2

        """

        self.E = E
        self.Ei = Ei

        # H0 : Noise only (no interferer)
        # central chi square
        if Ei==0:
            self.pH0 = st.chi2(self.order,
                               scale=self.scale)
        else:
        # H0 : Noise + interference
        # non central chi square
            self.pH0 = st.ncx2(self.order,
                               scale=self.scale,
                               nc=Ei/(self.scale))

        # H1 : Signal + interference + Noise
        # non central chi square
        self.pH1 = st.ncx2(self.order,
                           scale=self.scale,
                           nc=(E+Ei)/(self.scale))
Exemplo n.º 5
0
def construct_chisq_rv(lmb, mu=None, bill=None, dist=None, dim=None):
    msg = 'Must have mu and bill, or distance between mu and bill and number of dimensions'
    if dist is None:
        assert (mu is not None and bill is not None), msg
        delta = np.array(mu) - np.array(bill)
        nc = np.sum(delta**2)
        dof = len(mu)
    else:
        assert (dim is not None), msg
        nc = dist**2
        dof = dim
    rv = ncx2(dof, nc)
    return rv, dof, nc
Exemplo n.º 6
0
def show_probplot(k, lamda, theta, X_0, T, simulated):
    c = (2 * k) / ((1 - np.exp(-k * T)) * theta**2)
    df = 4 * k * lamda / theta**2
    nc = 2 * c * X_0 * np.exp(-k * T)
    rv = ncx2(df, nc, scale=1 / (2 * c))
    x, y = probplot(simulated, dist=rv, fit=False)
    plt.plot(x, y, "bo")
    plt.title("Probability Plot")
    plt.xlabel("Theoretical quantiles")
    plt.ylabel("Ordered Values")
    x = np.linspace(min(x[0], y[0]), max(x[-1], y[-1]), 2)
    plt.plot(x, x, "k--")
    plt.gca().set_aspect("equal")
Exemplo n.º 7
0
    def pdf(self, E, Ei=0):
        """ calculates decision problem densities under H0 and H1

        Parameters
        ----------

        E  : float
            Legitimate Signal Energy
        Ei : float
            Interferer Signal Energy

        See Also
        --------

        scipy.stats.chi2
        scipy.stats.ncx2

        """

        self.E = E
        self.Ei = Ei

        # H0 : Noise only (no interferer)
        # central chi square
        if Ei == 0:
            self.pH0 = st.chi2(self.order, scale=self.scale)
        else:
            # H0 : Noise + interference
            # non central chi square
            self.pH0 = st.ncx2(self.order,
                               scale=self.scale,
                               nc=Ei / (self.scale))

        # H1 : Signal + interference + Noise
        # non central chi square
        self.pH1 = st.ncx2(self.order,
                           scale=self.scale,
                           nc=(E + Ei) / (self.scale))
Exemplo n.º 8
0
def _sample(w, dof, loc, n=1):
    """
    Sample from 𝑋.

    Where
        𝑋 = w[0]⋅χ²(dof[0], loc[1]) + w[1]⋅χ²(dof[1], loc[1]) + ...
    """
    samples = []
    for _ in range(n):
        x = 0.0
        for i, (d, l) in enumerate(zip(dof, loc)):
            x += w[i] * ncx2(d, l).rvs()
        samples.append(x)
    return asarray(samples)
Exemplo n.º 9
0
def power_confidence_limits(preal, n=1, c=0.95):
    """Confidence limits on power, given a (theoretical) signal power.

    This is to be used when we *expect* a given power (e.g. from the pulsed
    fraction measured in previous observations) and we want to know the
    range of values the measured power could take to a given confidence level.
    Adapted from Vaughan et al. 1994, noting that, after appropriate
    normalization of the spectral stats, the distribution of powers in the PDS
    and the Z^2_n searches is always described by a noncentral chi squared
    distribution.

    Parameters
    ----------
    preal: float
        The theoretical signal-generated value of power

    Other Parameters
    ----------------
    n: int
        The number of summed powers to obtain the result. It can be multiple
        harmonics of the PDS, adjacent bins in a PDS summed to collect all the
        power in a QPO, or the n in Z^2_n
    c: float
        The confidence level (e.g. 0.95=95%)

    Returns
    -------
    pmeas: [float, float]
        The upper and lower confidence interval (a, 1-a) on the measured power

    Examples
    --------
    >>> cl = power_confidence_limits(150, c=0.84)
    >>> np.allclose(cl, [127, 176], atol=1)
    True
    """
    rv = stats.ncx2(2 * n, preal)
    return rv.ppf([1 - c, c])
Exemplo n.º 10
0
 def isf(x):
     rv = stats.ncx2(2 * n, x)
     return rv.ppf(c)
Exemplo n.º 11
0
 def ppf(x):
     rv = stats.ncx2(2 * n, x)
     return rv.ppf(1-c)
Exemplo n.º 12
0
                                            -1.8475789041433829e-22,
                                            36120900.670255348)
income_model_dict['nakagami'] = st.nakagami(0.10038339454419823,
                                            -3.0390927147076284e-22,
                                            33062195.426077582)
income_model_dict['exponweib'] = st.exponweib(-3.5157658448986489,
                                              0.44492833350419714,
                                              -15427.454196748848,
                                              2440.0278856175246)

drivingdistance_model_dict = ct.OrderedDict()
drivingdistance_model_dict['nakagami'] = st.nakagami(0.11928581143831021,
                                                     14.999999999999996,
                                                     41.404620910360876)
drivingdistance_model_dict['ncx2'] = st.ncx2(0.30254190304723211,
                                             1.1286538320791935,
                                             14.999999999999998,
                                             8.7361471573932192)
drivingdistance_model_dict['chi'] = st.chi(0.47882729877571095,
                                           14.999999999999996,
                                           44.218301183844645)
drivingdistance_model_dict['recipinvgauss'] = st.recipinvgauss(
    2447246.0546641815, 14.999999999994969, 31.072009722580802)
drivingdistance_model_dict['f'] = st.f(0.85798489720127036, 4.1904554804436929,
                                       14.99998319939356, 21.366492843433996)

drivingduration_model_dict = ct.OrderedDict()
drivingduration_model_dict['betaprime'] = st.betaprime(2.576282082814398,
                                                       9.7247974165209996,
                                                       9.1193851632305201,
                                                       261.3457987967214)
drivingduration_model_dict['exponweib'] = st.exponweib(2.6443841639764942,
Exemplo n.º 13
0
def mod_liu_corrected(q, w):
    r"""Joint significance of statistics derived from chi2-squared distributions.
    Parameters
    ----------
    q : float
        Test statistics.
    w : array_like
        Weights of the linear combination.
    Returns
    -------
    float
        Estimated p-value.

    Ref
    -------
    Liu, Huan, Yongqiang Tang, and Hao Helen Zhang. "A new chi-square
    approximation to the distribution of non-negative definite quadratic forms
    in non-central normal variables."
    Computational Statistics & Data Analysis 53.4 (2009): 853-856.
    """

    q = asarray(q, float)
    if not all(isfinite(atleast_1d(q))):
        raise ValueError("There are non-finite values in `q`.")

    w = asarray(w, float)
    if not all(isfinite(atleast_1d(w))):
        raise ValueError("There are non-finite values in `w`.")

    d = sum(w)
    w /= d

    c1 = sum(w)

    c2 = sum(w**2)

    c3 = sum(w**3)

    c4 = sum(w**4)

    s1 = c3 / (c2**(3 / 2))

    s2 = c4 / c2**2

    muQ = c1

    sigmaQ = sqrt(2 * c2)

    if s1**2 > s2:
        a = 1 / (s1 - sqrt(s1**2 - s2))

        delta = s1 * a**3 - a**2

        l = a**2 - 2 * delta
        if l < 0:
            raise RuntimeError("This term cannot be negative.")

    else:
        delta = 0
        l = 1 / (s1**2)
        a = sqrt(l)

    Q_norm = (q / d - muQ) / sigmaQ * sqrt(2 * l + 4 * delta) + (l + delta)
    Qq = atleast_1d(ncx2(df=l, nc=delta).sf(Q_norm))[0]
    return (Qq, muQ * d, sigmaQ * d, l)
Exemplo n.º 14
0
def all_dists():
    # dists param were taken from scipy.stats official
    # documentaion examples
    # Total - 89
    return {
        "alpha":
        stats.alpha(a=3.57, loc=0.0, scale=1.0),
        "anglit":
        stats.anglit(loc=0.0, scale=1.0),
        "arcsine":
        stats.arcsine(loc=0.0, scale=1.0),
        "beta":
        stats.beta(a=2.31, b=0.627, loc=0.0, scale=1.0),
        "betaprime":
        stats.betaprime(a=5, b=6, loc=0.0, scale=1.0),
        "bradford":
        stats.bradford(c=0.299, loc=0.0, scale=1.0),
        "burr":
        stats.burr(c=10.5, d=4.3, loc=0.0, scale=1.0),
        "cauchy":
        stats.cauchy(loc=0.0, scale=1.0),
        "chi":
        stats.chi(df=78, loc=0.0, scale=1.0),
        "chi2":
        stats.chi2(df=55, loc=0.0, scale=1.0),
        "cosine":
        stats.cosine(loc=0.0, scale=1.0),
        "dgamma":
        stats.dgamma(a=1.1, loc=0.0, scale=1.0),
        "dweibull":
        stats.dweibull(c=2.07, loc=0.0, scale=1.0),
        "erlang":
        stats.erlang(a=2, loc=0.0, scale=1.0),
        "expon":
        stats.expon(loc=0.0, scale=1.0),
        "exponnorm":
        stats.exponnorm(K=1.5, loc=0.0, scale=1.0),
        "exponweib":
        stats.exponweib(a=2.89, c=1.95, loc=0.0, scale=1.0),
        "exponpow":
        stats.exponpow(b=2.7, loc=0.0, scale=1.0),
        "f":
        stats.f(dfn=29, dfd=18, loc=0.0, scale=1.0),
        "fatiguelife":
        stats.fatiguelife(c=29, loc=0.0, scale=1.0),
        "fisk":
        stats.fisk(c=3.09, loc=0.0, scale=1.0),
        "foldcauchy":
        stats.foldcauchy(c=4.72, loc=0.0, scale=1.0),
        "foldnorm":
        stats.foldnorm(c=1.95, loc=0.0, scale=1.0),
        # "frechet_r": stats.frechet_r(c=1.89, loc=0.0, scale=1.0),
        # "frechet_l": stats.frechet_l(c=3.63, loc=0.0, scale=1.0),
        "genlogistic":
        stats.genlogistic(c=0.412, loc=0.0, scale=1.0),
        "genpareto":
        stats.genpareto(c=0.1, loc=0.0, scale=1.0),
        "gennorm":
        stats.gennorm(beta=1.3, loc=0.0, scale=1.0),
        "genexpon":
        stats.genexpon(a=9.13, b=16.2, c=3.28, loc=0.0, scale=1.0),
        "genextreme":
        stats.genextreme(c=-0.1, loc=0.0, scale=1.0),
        "gausshyper":
        stats.gausshyper(a=13.8, b=3.12, c=2.51, z=5.18, loc=0.0, scale=1.0),
        "gamma":
        stats.gamma(a=1.99, loc=0.0, scale=1.0),
        "gengamma":
        stats.gengamma(a=4.42, c=-3.12, loc=0.0, scale=1.0),
        "genhalflogistic":
        stats.genhalflogistic(c=0.773, loc=0.0, scale=1.0),
        "gilbrat":
        stats.gilbrat(loc=0.0, scale=1.0),
        "gompertz":
        stats.gompertz(c=0.947, loc=0.0, scale=1.0),
        "gumbel_r":
        stats.gumbel_r(loc=0.0, scale=1.0),
        "gumbel_l":
        stats.gumbel_l(loc=0.0, scale=1.0),
        "halfcauchy":
        stats.halfcauchy(loc=0.0, scale=1.0),
        "halflogistic":
        stats.halflogistic(loc=0.0, scale=1.0),
        "halfnorm":
        stats.halfnorm(loc=0.0, scale=1.0),
        "halfgennorm":
        stats.halfgennorm(beta=0.675, loc=0.0, scale=1.0),
        "hypsecant":
        stats.hypsecant(loc=0.0, scale=1.0),
        "invgamma":
        stats.invgamma(a=4.07, loc=0.0, scale=1.0),
        "invgauss":
        stats.invgauss(mu=0.145, loc=0.0, scale=1.0),
        "invweibull":
        stats.invweibull(c=10.6, loc=0.0, scale=1.0),
        "johnsonsb":
        stats.johnsonsb(a=4.32, b=3.18, loc=0.0, scale=1.0),
        "johnsonsu":
        stats.johnsonsu(a=2.55, b=2.25, loc=0.0, scale=1.0),
        "ksone":
        stats.ksone(n=1e03, loc=0.0, scale=1.0),
        "kstwobign":
        stats.kstwobign(loc=0.0, scale=1.0),
        "laplace":
        stats.laplace(loc=0.0, scale=1.0),
        "levy":
        stats.levy(loc=0.0, scale=1.0),
        "levy_l":
        stats.levy_l(loc=0.0, scale=1.0),
        "levy_stable":
        stats.levy_stable(alpha=0.357, beta=-0.675, loc=0.0, scale=1.0),
        "logistic":
        stats.logistic(loc=0.0, scale=1.0),
        "loggamma":
        stats.loggamma(c=0.414, loc=0.0, scale=1.0),
        "loglaplace":
        stats.loglaplace(c=3.25, loc=0.0, scale=1.0),
        "lognorm":
        stats.lognorm(s=0.954, loc=0.0, scale=1.0),
        "lomax":
        stats.lomax(c=1.88, loc=0.0, scale=1.0),
        "maxwell":
        stats.maxwell(loc=0.0, scale=1.0),
        "mielke":
        stats.mielke(k=10.4, s=3.6, loc=0.0, scale=1.0),
        "nakagami":
        stats.nakagami(nu=4.97, loc=0.0, scale=1.0),
        "ncx2":
        stats.ncx2(df=21, nc=1.06, loc=0.0, scale=1.0),
        "ncf":
        stats.ncf(dfn=27, dfd=27, nc=0.416, loc=0.0, scale=1.0),
        "nct":
        stats.nct(df=14, nc=0.24, loc=0.0, scale=1.0),
        "norm":
        stats.norm(loc=0.0, scale=1.0),
        "pareto":
        stats.pareto(b=2.62, loc=0.0, scale=1.0),
        "pearson3":
        stats.pearson3(skew=0.1, loc=0.0, scale=1.0),
        "powerlaw":
        stats.powerlaw(a=1.66, loc=0.0, scale=1.0),
        "powerlognorm":
        stats.powerlognorm(c=2.14, s=0.446, loc=0.0, scale=1.0),
        "powernorm":
        stats.powernorm(c=4.45, loc=0.0, scale=1.0),
        "rdist":
        stats.rdist(c=0.9, loc=0.0, scale=1.0),
        "reciprocal":
        stats.reciprocal(a=0.00623, b=1.01, loc=0.0, scale=1.0),
        "rayleigh":
        stats.rayleigh(loc=0.0, scale=1.0),
        "rice":
        stats.rice(b=0.775, loc=0.0, scale=1.0),
        "recipinvgauss":
        stats.recipinvgauss(mu=0.63, loc=0.0, scale=1.0),
        "semicircular":
        stats.semicircular(loc=0.0, scale=1.0),
        "t":
        stats.t(df=2.74, loc=0.0, scale=1.0),
        "triang":
        stats.triang(c=0.158, loc=0.0, scale=1.0),
        "truncexpon":
        stats.truncexpon(b=4.69, loc=0.0, scale=1.0),
        "truncnorm":
        stats.truncnorm(a=0.1, b=2, loc=0.0, scale=1.0),
        "tukeylambda":
        stats.tukeylambda(lam=3.13, loc=0.0, scale=1.0),
        "uniform":
        stats.uniform(loc=0.0, scale=1.0),
        "vonmises":
        stats.vonmises(kappa=3.99, loc=0.0, scale=1.0),
        "vonmises_line":
        stats.vonmises_line(kappa=3.99, loc=0.0, scale=1.0),
        "wald":
        stats.wald(loc=0.0, scale=1.0),
        "weibull_min":
        stats.weibull_min(c=1.79, loc=0.0, scale=1.0),
        "weibull_max":
        stats.weibull_max(c=2.87, loc=0.0, scale=1.0),
        "wrapcauchy":
        stats.wrapcauchy(c=0.0311, loc=0.0, scale=1.0),
    }
Exemplo n.º 15
0
df, nc = 21, 1.06
mean, var, skew, kurt = ncx2.stats(df, nc, moments='mvsk')

# Display the probability density function (``pdf``):

x = np.linspace(ncx2.ppf(0.01, df, nc), ncx2.ppf(0.99, df, nc), 100)
ax.plot(x, ncx2.pdf(x, df, nc), 'r-', lw=5, alpha=0.6, label='ncx2 pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = ncx2(df, nc)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = ncx2.ppf([0.001, 0.5, 0.999], df, nc)
np.allclose([0.001, 0.5, 0.999], ncx2.cdf(vals, df, nc))
# True

# Generate random numbers:

r = ncx2.rvs(df, nc, size=1000)

# And compare the histogram:

ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
 def __init__(self, df, ncp):
     self.dist_ = ncx2(df, ncp)