Ref Stat Pstdev
## Python statistics.pstdev() Method
The `statistics.pstdev()` method is a built-in function in Python's `statistics` module. It is used to calculate the **population standard deviation** of a given dataset.
Standard deviation is a statistical measure that quantifies the amount of variation or dispersion in a set of values. A low standard deviation indicates that the data points tend to be close to the mean (average) of the set, while a high standard deviation indicates that the data points are spread out over a wider range of values.
---
### Syntax
```python
statistics.pstdev(data, mu=None)
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `data` | Iterable | A sequence or iterable of real-valued numbers (e.g., list, tuple, or range). |
| `mu` | Number (Optional) | The known population mean. If omitted or set to `None`, the function will automatically calculate the mean of the dataset. |
### Return Value
* **Type:** `float` (or `Decimal`/`Fraction` if the input data uses those types).
* **Description:** Returns the population standard deviation of the provided dataset.
---
### Mathematical Formula
The population standard deviation ($\sigma$) is calculated using the following formula:
$$\sigma = \sqrt{\frac{\sum (x - \mu)^2}{N}}$$
Where:
* $x$ represents each value in the dataset.
* $\mu$ is the population mean.
* $N$ is the total number of data points (population size).
> **Note on `pstdev` vs. `stdev`:**
> * Use `pstdev()` when your dataset represents the **entire population**.
> * Use `stdev()` when your dataset is a **sample** of a larger population (which uses Bessel's correction, dividing by $N-1$ instead of $N$).
---
### Code Examples
#### Example 1: Basic Usage with a List of Integers
```python
import statistics
# Dataset representing the entire population
data = [1, 2, 3, 4, 5]
# Calculate population standard deviation
pstdev_val = statistics.pstdev(data)
print(f"Population Standard Deviation: {pstdev_val}")
```
**Output:**
```text
Population Standard Deviation: 1.4142135623730951
```
*(Note: The original Chinese source text contained a minor mathematical error in its explanation, showing the sample standard deviation result of `1.5811388300841898` for `pstdev`. The correct population standard deviation for `[1, 2, 3, 4, 5]` is $\sqrt{2} \approx 1.4142135623730951$.)*
---
#### Example 2: Specifying the Population Mean (`mu`)
If you have already calculated the mean of your dataset, you can pass it to the `mu` parameter to avoid redundant calculations and improve performance.
```python
import statistics
data = [2, 4, 4, 4, 5, 5, 7, 9]
# Calculate the mean beforehand
mean_val = statistics.mean(data) # mean is 5.0
# Calculate population standard deviation by passing the pre-calculated mean
pstdev_val = statistics.pstdev(data, mu=mean_val)
print(f"Mean: {mean_val}")
print(f"Population Standard Deviation: {pstdev_val}")
```
**Output:**
```text
Mean: 5.0
Population Standard Deviation: 2.0
```
---
#### Example 3: Using Decimal and Fraction Types
The `statistics` module natively supports high-precision math using `decimal.Decimal` and `fractions.Fraction`.
```python
from decimal import Decimal
import statistics
# Using Decimal for high-precision financial or scientific data
decimal_data = [Decimal("1.5"), Decimal("2.5"), Decimal("3.5")]
pstdev_decimal = statistics.pstdev(decimal_data)
print(f"Decimal Output: {pstdev_decimal} (Type: {type(pstdev_decimal)})")
```
**Output:**
```text
Decimal Output: 0.8164965809277260327324280249 (Type: )
```
---
### Considerations & Exceptions
1. **Empty Dataset:** If `data` is empty, `statistics.pstdev()` will raise a `StatisticsError`.
2. **Single Value:** Unlike `statistics.stdev()` (which raises an error for a single value because $N-1 = 0$), `statistics.pstdev()` can accept a single-item dataset and will return `0.0`.
3. **Data Types:** The input data can contain mixed numeric types (integers, floats, Decimals, Fractions), but they must be compatible with basic arithmetic operations. Passing strings or non-numeric types will raise a `TypeError`.
YouTip