跳转至

pystatpower.proportion.single.superiority

Functions:

Name Description
solve_power

Calculate the statistical power for a superiority test of one proportion.

solve_size

Estimate the required sample size for a superiority test of one proportion.

solve_proportion

Estimate the required proportion under the alternative hypothesis for a superiority test of one proportion.

solve_null_proportion

Estimate the required proportion under the null hypothesis for a superiority test of one proportion.

solve_superiority_proportion

Estimate the required superiority proportion for a superiority test of one proportion.

solve_margin

Estimate the required margin for a superiority test for one proportion.

solve_power

solve_power(
    *,
    proportion: float,
    null_proportion: float | None = None,
    margin: float | None = None,
    superiority_proportion: float | None = None,
    size: int,
    alternative: Literal["greater", "less"],
    alpha: float = 0.025,
    method: Literal["z-p0", "z-phat"],
    continuity_correction: bool = False,
) -> float

Calculate the statistical power for a superiority test of one proportion.

Parameters:

Name Type Description Default
proportion float

Proportion under the alternative hypothesis.

required
null_proportion float | None

Proportion under the null hypothesis.

Ignored if superiority_proportion is specified; otherwise, required alongside margin.

None
margin float | None

The superiority margin.

Ignored if superiority_proportion is specified; otherwise, required alongside null_proportion.

Regardless of whether alternative is specified as 'greater' or 'less', you can always specify this parameter to be positive or negative as you prefer. Internally, the value of margin will be converted before actual calculation.

  • If alternative is 'greater', the actual margin used internally is abs(margin).
  • If alternative is 'less', the actual margin used internally is -abs(margin).
None
superiority_proportion float | None

The superiority proportion.

Required if either null_proportion or margin is omitted.

None
size int

Sample size.

required
alternative Literal['greater', 'less']

Type of the alternative hypothesis:

  • If alternative is 'greater', the alternative hypothesis is \(p - p_0 > \delta \ (\delta > 0)\)
  • If alternative is 'less', the alternative hypothesis is \(p - p_0 < \delta \ (\delta < 0)\)
required
alpha float

Significance level.

The superiority test is a one-sided test, and 0.025 is a commonly used significance level.

0.025
method Literal['z-p0', 'z-phat']

The method used to construct the test statistic.

  • 'z-p0': Standard normal distribution (large sample approximation), using p0 to calculate the variance.
  • 'z-phat': Standard normal distribution (large sample approximation), using phat to calculate the variance.
required
continuity_correction bool

Whether to apply the continuity correction.

False

Returns:

Name Type Description
float float

The statistical power of the test.

Raises:

Type Description
ValueError

If superiority_proportion is omitted, and either null_proportion or margin is missing.

solve_size

solve_size(
    *,
    proportion: float,
    null_proportion: float | None = None,
    margin: float | None = None,
    superiority_proportion: float | None = None,
    alternative: Literal["greater", "less"],
    alpha: float = 0.025,
    power: float = 0.8,
    method: Literal["z-p0", "z-phat"],
    continuity_correction: bool = False,
) -> int

Estimate the required sample size for a superiority test of one proportion.

Parameters:

Name Type Description Default
proportion float

Proportion under the alternative hypothesis.

required
null_proportion float | None

Proportion under the null hypothesis.

Ignored if superiority_proportion is specified; otherwise, required alongside margin.

None
margin float | None

The superiority margin.

Ignored if superiority_proportion is specified; otherwise, required alongside null_proportion.

Regardless of whether alternative is specified as 'greater' or 'less', you can always specify this parameter to be positive or negative as you prefer. Internally, the value of margin will be converted before actual calculation.

  • If alternative is 'greater', the actual margin used internally is abs(margin).
  • If alternative is 'less', the actual margin used internally is -abs(margin).
None
superiority_proportion float | None

The superiority proportion.

Required if either null_proportion or margin is omitted.

None
alternative Literal['greater', 'less']

Type of the alternative hypothesis:

  • If alternative is 'greater', the alternative hypothesis is \(p - p_0 > \delta \ (\delta > 0)\)
  • If alternative is 'less', the alternative hypothesis is \(p - p_0 < \delta \ (\delta < 0)\)
required
alpha float

Significance level.

The superiority test is a one-sided test, and 0.025 is a commonly used significance level.

0.025
power float

Expected statistical power.

0.8 is a commonly used statistical power.

0.8
method Literal['z-p0', 'z-phat']

The method used to construct the test statistic.

  • 'z-p0': Standard normal distribution (large sample approximation), using p0 to calculate the variance.
  • 'z-phat': Standard normal distribution (large sample approximation), using phat to calculate the variance.
required
continuity_correction bool

Whether to apply the continuity correction.

False

Returns:

Name Type Description
int int

The required sample size.

Raises:

Type Description
ValueError

If superiority_proportion is omitted, and either null_proportion or margin is missing.

solve_proportion

solve_proportion(
    *,
    null_proportion: float | None = None,
    margin: float | None = None,
    superiority_proportion: float | None = None,
    size: int,
    alternative: Literal["greater", "less"],
    alpha: float = 0.025,
    power: float = 0.8,
    method: Literal["z-p0", "z-phat"],
    continuity_correction: bool = False,
) -> float

Estimate the required proportion under the alternative hypothesis for a superiority test of one proportion.

Parameters:

Name Type Description Default
null_proportion float | None

Proportion under the null hypothesis.

Ignored if superiority_proportion is specified; otherwise, required alongside margin.

None
margin float | None

The superiority margin.

Ignored if superiority_proportion is specified; otherwise, required alongside null_proportion.

Regardless of whether alternative is specified as 'greater' or 'less', you can always specify this parameter to be positive or negative as you prefer. Internally, the value of margin will be converted before actual calculation.

  • If alternative is 'greater', the actual margin used internally is abs(margin).
  • If alternative is 'less', the actual margin used internally is -abs(margin).
None
superiority_proportion float | None

The superiority proportion.

Required if either null_proportion or margin is omitted.

None
size int

Sample size.

required
alternative Literal['greater', 'less']

Type of the alternative hypothesis:

  • If alternative is 'greater', the alternative hypothesis is \(p - p_0 > \delta \ (\delta > 0)\)
  • If alternative is 'less', the alternative hypothesis is \(p - p_0 < \delta \ (\delta < 0)\)
required
alpha float

Significance level.

The superiority test is a one-sided test, and 0.025 is a commonly used significance level.

0.025
power float

Expected statistical power.

0.8 is a commonly used statistical power.

0.8
method Literal['z-p0', 'z-phat']

The method used to construct the test statistic.

  • 'z-p0': Standard normal distribution (large sample approximation), using p0 to calculate the variance.
  • 'z-phat': Standard normal distribution (large sample approximation), using phat to calculate the variance.
required
continuity_correction bool

Whether to apply the continuity correction.

False

Returns:

Name Type Description
float float

The required proportion under the alternative hypothesis.

Raises:

Type Description
ValueError

If superiority_proportion is omitted, and either null_proportion or margin is missing.

Notes

The value range of the alternative hypothesis proportion \(p\) is determined by the null hypothesis proportion \(p_0\) and the non-inferiority margin \(\delta\).

If alternative is 'greater', that is, higher proportions are better, we have:

\[ \begin{cases} p > p_0 + \delta \\ 0 < p < 1 \\ \delta > 0 \end{cases} \ \Rightarrow p_0 + \delta < p < 1 \]

If alternative is 'less', that is, higher proportions are worse, we have:

\[ \begin{cases} p < p_0 + \delta \\ 0 < p < 1 \\ \delta < 0 \end{cases} \ \Rightarrow 0 < p < p_0 + \delta \]

solve_null_proportion

solve_null_proportion(
    *,
    proportion: float,
    margin: float,
    size: int,
    alternative: Literal["greater", "less"],
    alpha: float = 0.025,
    power: float = 0.8,
    method: Literal["z-p0", "z-phat"],
    continuity_correction: bool = False,
) -> float

Estimate the required proportion under the null hypothesis for a superiority test of one proportion.

Parameters:

Name Type Description Default
proportion float

Proportion under the alternative hypothesis.

required
margin float

The superiority margin.

Regardless of whether alternative is specified as 'greater' or 'less', you can always specify this parameter to be positive or negative as you prefer. Internally, the value of margin will be converted before actual calculation.

  • If alternative is 'greater', the actual margin used internally is abs(margin).
  • If alternative is 'less', the actual margin used internally is -abs(margin).
required
size int

Sample size.

required
alternative Literal['greater', 'less']

Type of the alternative hypothesis:

  • If alternative is 'greater', the alternative hypothesis is \(p - p_0 > \delta \ (\delta > 0)\)
  • If alternative is 'less', the alternative hypothesis is \(p - p_0 < \delta \ (\delta < 0)\)
required
alpha float

Significance level.

The superiority test is a one-sided test, and 0.025 is a commonly used significance level.

0.025
power float

Expected statistical power.

0.8 is a commonly used statistical power.

0.8
method Literal['z-p0', 'z-phat']

The method used to construct the test statistic.

  • 'z-p0': Standard normal distribution (large sample approximation), using p0 to calculate the variance.
  • 'z-phat': Standard normal distribution (large sample approximation), using phat to calculate the variance.
required
continuity_correction bool

Whether to apply the continuity correction.

False

Returns:

Name Type Description
float float

The required proportion under the null hypothesis.

Notes

The value range of the null hypothesis proportion \(p_0\) is determined by the alternative hypothesis proportion \(p\) and the non-inferiority margin \(\delta\).

If alternative is 'greater', that is, higher proportions are better, we have:

\[ \begin{cases} p_0 < p - \delta \\ 0 < p_0 < 1 \\ 0 < p_0 + \delta < 1 \\ \delta > 0 \end{cases} \ \Rightarrow 0 < p_0 < \operatorname{min}(p - \delta, 1 - \delta) \]

If alternative is 'less', that is, higher proportions are worse, we have:

\[ \begin{cases} p_0 > p - \delta \\ 0 < p_0 < 1 \\ 0 < p_0 + \delta < 1 \\ \delta < 0 \end{cases} \ \Rightarrow \operatorname{max}(p - \delta, -\delta) < p_0 < 1 \]

solve_superiority_proportion

solve_superiority_proportion(
    *,
    proportion: float,
    size: int,
    alternative: Literal["greater", "less"],
    alpha: float = 0.025,
    power: float = 0.8,
    method: Literal["z-p0", "z-phat"],
    continuity_correction: bool = False,
) -> float

Estimate the required superiority proportion for a superiority test of one proportion.

Parameters:

Name Type Description Default
proportion float

Proportion under the alternative hypothesis.

required
size int

Sample size.

required
alternative Literal['greater', 'less']

Type of the alternative hypothesis:

  • If alternative is 'greater', the alternative hypothesis is \(p - p_0 > \delta \ (\delta > 0)\)
  • If alternative is 'less', the alternative hypothesis is \(p - p_0 < \delta \ (\delta < 0)\)
required
alpha float

Significance level.

The superiority test is a one-sided test, and 0.025 is a commonly used significance level.

0.025
power float

Expected statistical power.

0.8 is a commonly used statistical power.

0.8
method Literal['z-p0', 'z-phat']

The method used to construct the test statistic.

  • 'z-p0': Standard normal distribution (large sample approximation), using p0 to calculate the variance.
  • 'z-phat': Standard normal distribution (large sample approximation), using phat to calculate the variance.
required
continuity_correction bool

Whether to apply the continuity correction.

False

Returns:

Name Type Description
float float

The required superiority proportion.

Notes

The value range of the superiority hypothesis proportion \(p_{\text{sup}}\) is determined by the alternative hypothesis proportion \(p\).

If alternative is 'greater', that is, higher proportions are better, we have:

\[ \begin{cases} p_{\text{sup}} < p \\ 0 < p_{\text{sup}} < 1 \\ \end{cases} \ \Rightarrow 0 < p_{\text{sup}} < p \]

If alternative is 'less', that is, higher proportions are worse, we have:

\[ \begin{cases} p_{\text{sup}} > p \\ 0 < p_{\text{sup}} < 1 \\ \end{cases} \ \Rightarrow p < p_{\text{sup}} < 1 \]

solve_margin

solve_margin(
    *,
    proportion: float,
    null_proportion: float,
    size: int,
    alternative: Literal["greater", "less"],
    alpha: float = 0.025,
    power: float = 0.8,
    method: Literal["z-p0", "z-phat"],
    continuity_correction: bool = False,
) -> float

Estimate the required margin for a superiority test for one proportion.

Parameters:

Name Type Description Default
proportion float

Proportion under the alternative hypothesis.

required
null_proportion float

Proportion under the null hypothesis.

required
size int

Sample size.

required
alternative Literal['greater', 'less']

Type of the alternative hypothesis:

  • If alternative is 'greater', the alternative hypothesis is \(p - p_0 > \delta \ (\delta > 0)\)
  • If alternative is 'less', the alternative hypothesis is \(p - p_0 < \delta \ (\delta < 0)\)
required
alpha float

Significance level.

The superiority test is a one-sided test, and 0.025 is a commonly used significance level.

0.025
power float

Expected statistical power.

0.8 is a commonly used statistical power.

0.8
method Literal['z-p0', 'z-phat']

The method used to construct the test statistic.

  • 'z-p0': Standard normal distribution (large sample approximation), using p0 to calculate the variance.
  • 'z-phat': Standard normal distribution (large sample approximation), using phat to calculate the variance.
required
continuity_correction bool

Whether to apply the continuity correction.

False

Returns:

Name Type Description
float float

The required superiority margin.

Notes

The value range of the null hypothesis proportion \(p_0\) is determined by the alternative hypothesis proportion \(p\) and the non-inferiority margin \(\delta\).

If alternative is 'greater', that is, higher proportions are better, we have:

\[ \begin{cases} \delta < p - p_0 \\ 0 < p_0 + \delta < 1 \\ \delta > 0 \end{cases} \ \Rightarrow 0 < \delta < p - p_0 \]

If alternative is 'less', that is, higher proportions are worse, we have:

\[ \begin{cases} \delta > p - p_0 \\ 0 < p_0 + \delta < 1 \\ \delta < 0 \end{cases} \ \Rightarrow p - p_0 < \delta < 0 \]

To handle cases where the superiority margin is zero, the program computes the margin indirectly. It first calls solve_superiority_proportion to determine the superiority proportion \(p_{\text{sup}}\), and then calculates the margin \(\delta\) using the following formula:

\[ \delta = p_{\text{sup}} - p_0 \]