跳转至

pystatpower.proportion.single.superiority

FUNCTION DESCRIPTION
solve_power

Calculate the statistical power.

solve_size

Estimate the required sample size.

solve_proportion

Estimate the required proportion under the alternative hypothesis.

solve_null_proportion

Estimate the required proportion under the null hypothesis.

solve_superiority_proportion

Estimate the required superiority proportion.

solve_margin

Estimate the required superiority margin.

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"] = "z-phat",
    continuity_correction: bool = False,
) -> float

Calculate the statistical power.

PARAMETER DESCRIPTION
proportion

Proportion under the alternative hypothesis.

TYPE: float

null_proportion

Proportion under the null hypothesis.

Ignored if superiority_proportion is specified; otherwise, required along with margin.

TYPE: float | None DEFAULT: None

margin

The superiority margin.

Ignored if superiority_proportion is specified; otherwise, required along with null_proportion.

Tip

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

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

TYPE: float | None DEFAULT: None

superiority_proportion

The superiority proportion.

Required if either null_proportion or margin is omitted.

TYPE: float | None DEFAULT: None

size

Sample size.

TYPE: int

alternative

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)\)

TYPE: Literal['greater', 'less']

alpha

Significance level.

The superiority test is a one-sided test, with a significance level of 0.025 being commonly used.

TYPE: float DEFAULT: 0.025

method

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.

TYPE: Literal['z-p0', 'z-phat'] DEFAULT: 'z-phat'

continuity_correction

Whether to apply the continuity correction.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
float

The statistical power of the test.

RAISES 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"] = "z-phat",
    continuity_correction: bool = False,
) -> int

Estimate the required sample size.

PARAMETER DESCRIPTION
proportion

Proportion under the alternative hypothesis.

TYPE: float

null_proportion

Proportion under the null hypothesis.

Ignored if superiority_proportion is specified; otherwise, required along with margin.

TYPE: float | None DEFAULT: None

margin

The superiority margin.

Ignored if superiority_proportion is specified; otherwise, required along with null_proportion.

Tip

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

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

TYPE: float | None DEFAULT: None

superiority_proportion

The superiority proportion.

Required if either null_proportion or margin is omitted.

TYPE: float | None DEFAULT: None

alternative

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)\)

TYPE: Literal['greater', 'less']

alpha

Significance level.

The superiority test is a one-sided test, with a significance level of 0.025 being commonly used.

TYPE: float DEFAULT: 0.025

power

Expected statistical power.

0.8 is a commonly used value for statistical power.

TYPE: float DEFAULT: 0.8

method

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.

TYPE: Literal['z-p0', 'z-phat'] DEFAULT: 'z-phat'

continuity_correction

Whether to apply the continuity correction.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
int

The required sample size.

RAISES 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"] = "z-phat",
    continuity_correction: bool = False,
) -> float

Estimate the required proportion under the alternative hypothesis.

PARAMETER DESCRIPTION
null_proportion

Proportion under the null hypothesis.

Ignored if superiority_proportion is specified; otherwise, required along with margin.

TYPE: float | None DEFAULT: None

margin

The superiority margin.

Ignored if superiority_proportion is specified; otherwise, required along with null_proportion.

Tip

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

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

TYPE: float | None DEFAULT: None

superiority_proportion

The superiority proportion.

Required if either null_proportion or margin is omitted.

TYPE: float | None DEFAULT: None

size

Sample size.

TYPE: int

alternative

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)\)

TYPE: Literal['greater', 'less']

alpha

Significance level.

The superiority test is a one-sided test, with a significance level of 0.025 being commonly used.

TYPE: float DEFAULT: 0.025

power

Expected statistical power.

0.8 is a commonly used value for statistical power.

TYPE: float DEFAULT: 0.8

method

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.

TYPE: Literal['z-p0', 'z-phat'] DEFAULT: 'z-phat'

continuity_correction

Whether to apply the continuity correction.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
float

The required proportion under the alternative hypothesis.

RAISES 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"] = "z-phat",
    continuity_correction: bool = False,
) -> float

Estimate the required proportion under the null hypothesis.

PARAMETER DESCRIPTION
proportion

Proportion under the alternative hypothesis.

TYPE: float

margin

The superiority margin.

Tip

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

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

TYPE: float

size

Sample size.

TYPE: int

alternative

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)\)

TYPE: Literal['greater', 'less']

alpha

Significance level.

The superiority test is a one-sided test, with a significance level of 0.025 being commonly used.

TYPE: float DEFAULT: 0.025

power

Expected statistical power.

0.8 is a commonly used value for statistical power.

TYPE: float DEFAULT: 0.8

method

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.

TYPE: Literal['z-p0', 'z-phat'] DEFAULT: 'z-phat'

continuity_correction

Whether to apply the continuity correction.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
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"] = "z-phat",
    continuity_correction: bool = False,
) -> float

Estimate the required superiority proportion.

PARAMETER DESCRIPTION
proportion

Proportion under the alternative hypothesis.

TYPE: float

size

Sample size.

TYPE: int

alternative

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)\)

TYPE: Literal['greater', 'less']

alpha

Significance level.

The superiority test is a one-sided test, with a significance level of 0.025 being commonly used.

TYPE: float DEFAULT: 0.025

power

Expected statistical power.

0.8 is a commonly used value for statistical power.

TYPE: float DEFAULT: 0.8

method

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.

TYPE: Literal['z-p0', 'z-phat'] DEFAULT: 'z-phat'

continuity_correction

Whether to apply the continuity correction.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
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"] = "z-phat",
    continuity_correction: bool = False,
) -> float

Estimate the required superiority margin.

PARAMETER DESCRIPTION
proportion

Proportion under the alternative hypothesis.

TYPE: float

null_proportion

Proportion under the null hypothesis.

TYPE: float

size

Sample size.

TYPE: int

alternative

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)\)

TYPE: Literal['greater', 'less']

alpha

Significance level.

The superiority test is a one-sided test, with a significance level of 0.025 being commonly used.

TYPE: float DEFAULT: 0.025

power

Expected statistical power.

0.8 is a commonly used value for statistical power.

TYPE: float DEFAULT: 0.8

method

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.

TYPE: Literal['z-p0', 'z-phat'] DEFAULT: 'z-phat'

continuity_correction

Whether to apply the continuity correction.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
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 \]