def confint_noncentrality(f_stat, df1, df2, alpha=0.05, alternative="two-sided"): """confidence interval for noncentality parameter in F-test This does not yet handle non-negativity constraint on nc. Currently only two-sided alternative is supported. Notes ----- The algorithm inverts the cdf of the noncentral F distribution with respect to the noncentrality parameters. See Steiger 2004 and references cited in it. References ---------- Steiger, James H. 2004. “Beyond the F Test: Effect Size Confidence Intervals and Tests of Close Fit in the Analysis of Variance and Contrast Analysis.” Psychological Methods 9 (2): 164–82. https://doi.org/10.1037/1082-989X.9.2.164. See Also -------- `confint_effectsize_oneway` """ if alternative in ["two-sided", "2s", "ts"]: alpha1s = alpha / 2 ci = ncfdtrinc(df1, df2, [1 - alpha1s, alpha1s], f_stat) else: raise NotImplementedError return ci
def _noncentrality_f(f_stat, df1, df2, alpha=0.05): """noncentrality parameter for f statistic `nc` is zero-truncated umvue Parameters ---------- fstat : float f-statistic, for example from a hypothesis test df : int or float Degrees of freedom alpha : float in (0, 1) Significance level for the confidence interval, covarage is 1 - alpha. Returns ------- HolderTuple The main attributes are - ``nc`` : estimate of noncentrality parameter - ``confint`` : lower and upper bound of confidence interval for `nc`` Other attributes are estimates for nc by different methods. References ---------- .. [1] Kubokawa, T., C.P. Robert, and A.K.Md.E. Saleh. 1993. “Estimation of Noncentrality Parameters.” Canadian Journal of Statistics 21 (1): 45–57. https://doi.org/10.2307/3315657. """ alpha_half = alpha / 2 x_s = f_stat * df1 / df2 nc_umvue = (df2 - 2) * x_s - df1 nc = np.maximum(nc_umvue, 0) nc_krs = np.maximum(nc_umvue, x_s * 2 * (df2 - 1) / (df1 + 2)) nc_median = special.ncfdtrinc(df1, df2, 0.5, f_stat) ci = special.ncfdtrinc(df1, df2, [1 - alpha_half, alpha_half], f_stat) res = Holder(nc=nc, confint=ci, nc_umvue=nc_umvue, nc_krs=nc_krs, nc_median=nc_median, name="Noncentrality for F-distributed random variable" ) return res
def _compute_ncp_f(df1, df2, power=1 / 3): """ Uses the inverse function of the non-central F distribution with regard to NCP to recover the NCP corresponding to a given level of power for a given F test. :param df1: Numerator degrees of freedom :param df2: Denominator degrees of freedom :param power: Desired level of power :return: """ critval = f._ppf(.95, df1, df2) return ncfdtrinc(df1, df2, 1 - power, critval)
def confint_noncentrality(f_stat, df, alpha=0.05, alternative="two-sided"): """ Confidence interval for noncentrality parameter in F-test This does not yet handle non-negativity constraint on nc. Currently only two-sided alternative is supported. Parameters ---------- f_stat : float df : tuple degrees of freedom ``df = (df1, df2)`` where - df1 : numerator degrees of freedom, number of constraints - df2 : denominator degrees of freedom, df_resid alpha : float, default 0.05 alternative : {"two-sided"} Other alternatives have not been implements. Returns ------- float The end point of the confidence interval. Notes ----- The algorithm inverts the cdf of the noncentral F distribution with respect to the noncentrality parameters. See Steiger 2004 and references cited in it. References ---------- .. [1] Steiger, James H. 2004. “Beyond the F Test: Effect Size Confidence Intervals and Tests of Close Fit in the Analysis of Variance and Contrast Analysis.” Psychological Methods 9 (2): 164–82. https://doi.org/10.1037/1082-989X.9.2.164. See Also -------- confint_effectsize_oneway """ df1, df2 = df if alternative in ["two-sided", "2s", "ts"]: alpha1s = alpha / 2 ci = ncfdtrinc(df1, df2, [1 - alpha1s, alpha1s], f_stat) else: raise NotImplementedError return ci
def _calc_bound(self, is_multirep, alphatest, alpha, lower_tail_prob, upper_tail_prob, prob, noncentrality, cl_type, dfe1, dfe2, dfh, fcrit, omega, tolerance, **kwargs): """Calculate power bounds """ df1_unirep = None if 'df1_unirep' in kwargs.keys(): df1_unirep = kwargs['df1_unirep'] fmethod = Constants.FMETHOD_MISSING if alpha > tolerance: if cl_type == Constants.CLTYPE_DESIRED_KNOWN: if is_multirep: chi = chi2.ppf(lower_tail_prob, dfe1) noncentrality = (chi / dfe1) * omega else: chi = chi2.ppf(lower_tail_prob, dfh) noncentrality = (chi / dfh) * omega elif cl_type == Constants.CLTYPE_DESIRED_ESTIMATE: f_a = omega / dfh bound = finv(upper_tail_prob, dfh, dfe1) if f_a <= bound: noncentrality = 0 else: noncentrality = special.ncfdtrinc(dfh, dfe1, upper_tail_prob, f_a) if is_multirep: prob, fmethod = probf(fcrit, dfh, dfe2, noncentrality) else: prob, fmethod = probf(fcrit, df1_unirep, dfe2, noncentrality) if fmethod == Constants.FMETHOD_NORMAL_LR and prob == 1: power_bound = alphatest else: power_bound = 1 - prob power = Power() power.power = power_bound power.fmethod = fmethod power.noncentrality_parameter = noncentrality return power