跳转至

pystatpower.models.proportion.single.ci

Functions:

Name Description
solve_distance

Calculate the confidence interval width for one proportion or the distance from the proportion to the confidence bound.

solve_size

Estimate the required sample size, given either a desired confidence interval width or a desired distance from the proportion to the confidence bound.

solve_proportion

Estimate the required proportion, given either a desired confidence interval width or a desired distance from the proportion to the confidence bound.

solve_distance

solve_distance(
    *,
    proportion: float,
    size: int,
    conf_level: float = 0.95,
    interval_type: Literal[
        "two-sided", "lower one-sided", "upper one-sided"
    ] = "two-sided",
    method: Literal[
        "clopper-pearson", "wald", "wilson"
    ] = "clopper-pearson",
    continuity_correction: bool = False,
) -> float

Calculate the confidence interval width for one proportion or the distance from the proportion to the confidence bound.

Parameters:

Name Type Description Default
proportion float

Proportion.

required
size int

Sample size.

required
conf_level float

Confidence level.

0.95
interval_type Literal['two-sided', 'lower one-sided', 'upper one-sided']

Type of the confidence interval.

  • two-sided: Two-sided confidence interval \((L, U)\).
  • lower one-sided: Lower one-sided confidence interval \((L, +\infty)\).
  • upper one-sided: Upper one-sided confidence interval \((-\infty, U)\).
'two-sided'
method Literal['clopper-pearson', 'wald', 'wilson']

Method used in calculation of the confidence interval.

'clopper-pearson'
continuity_correction bool

Whether or not to apply Yate's continuity correction, only valid for method='wald' or method='wilson'.

False

Returns:

Type Description
float

The confidence interval width.

Raises:

Type Description
ValueError

If method='wald' or method='wilson', and continuity_correction=None.

solve_size

solve_size(
    *,
    proportion: float,
    distance: float,
    conf_level: float = 0.95,
    interval_type: Literal[
        "two-sided", "lower one-sided", "upper one-sided"
    ] = "two-sided",
    method: Literal[
        "clopper-pearson", "wald", "wilson"
    ] = "clopper-pearson",
    continuity_correction: bool = False,
) -> int

Estimate the required sample size, given either a desired confidence interval width or a desired distance from the proportion to the confidence bound.

Parameters:

Name Type Description Default
proportion float

Proportion.

required
distance float
  • if interval_type='two-sided', provide confidence interval width.
  • if interval_type='lower one-sided' or interval_type='upper one-sided', provide the distance from the proportion to the confidence bound.
required
conf_level float

Confidence level.

0.95
interval_type Literal['two-sided', 'lower one-sided', 'upper one-sided']

Type of the confidence interval.

  • two-sided: Two-sided confidence interval \((L, U)\).
  • lower one-sided: Lower one-sided confidence interval \((L, +\infty)\).
  • upper one-sided: Upper one-sided confidence interval \((-\infty, U)\).
'two-sided'
method Literal['clopper-pearson', 'wald', 'wilson']

Method used in calculation of the confidence interval.

'clopper-pearson'
continuity_correction bool | None

Whether or not to apply Yate's continuity correction, only valid for method='wald' or method='wilson'.

False

Returns:

Type Description
int

The required sample size.

Raises:

Type Description
ValueError

If method='wald' or method='wilson', and continuity_correction=None.

solve_proportion

solve_proportion(
    *,
    size: int,
    distance: float,
    conf_level: float = 0.95,
    interval_type: Literal[
        "two-sided", "lower one-sided", "upper one-sided"
    ] = "two-sided",
    method: Literal[
        "clopper-pearson", "wald", "wilson"
    ] = "clopper-pearson",
    continuity_correction: bool = False,
    search_direction: Literal["below", "above"] = "above",
) -> float

Estimate the required proportion, given either a desired confidence interval width or a desired distance from the proportion to the confidence bound.

Parameters:

Name Type Description Default
size int

Sample size.

required
distance float
  • if interval_type='two-sided', provide confidence interval width.
  • if interval_type='lower one-sided' or interval_type='upper one-sided', provide the distance from the proportion to the confidence bound.
required
conf_level float

Confidence level.

0.95
interval_type Literal['two-sided', 'lower one-sided', 'upper one-sided']

Type of the confidence interval.

  • two-sided: Two-sided confidence interval \((L, U)\).
  • lower one-sided: Lower one-sided confidence interval \((L, +\infty)\).
  • upper one-sided: Upper one-sided confidence interval \((-\infty, U)\).
'two-sided'
method Literal['clopper-pearson', 'wald', 'wilson']

Method used in calculation of the confidence interval.

'clopper-pearson'
continuity_correction bool | None

Whether or not to apply Yate's continuity correction, only valid for method='wald' or method='wilson'.

False
search_direction Literal['below', 'above']

Direction of the search.

  • 'below': search for the proportion in range (0, 0.5]
  • 'above': search for the proportion in range [0.5, 1)
'above'

Returns:

Type Description
float

The required proportion.