## output_analysis.OnlineStatistics


Computes running sample mean and variance using Welford's algorithm.


Usage

``` python
output_analysis.OnlineStatistics(
    data=None,
    alpha=0.1,
    observer=None,
)
```


This is a robust and numerically stable approach first described in the 1960s and popularised in Donald Knuth's *The Art of Computer Programming* (Vol. 2).

The term *"online"* means each new data point is processed immediately to update statistics, without storing or reprocessing the entire dataset.

This implementation additionally supports computation of: - Confidence intervals (CIs). - Percentage deviation of CI half-widths from the mean.


## Attributes


`n: int`  
Number of data points processed so far.

`x_i: float`  
Most recent data point.

`mean: float`  
Current running mean.

`_sq: float`  
Sum of squared differences from the current mean (used for variance).

`alpha: float`  
Significance level for confidence interval calculations

`observer: list`  
Registered observers notified upon updates.


## Attributes

| Name | Description |
|----|----|
| [deviation](#deviation) | Precision of the confidence interval expressed as the percentage |
| [half_width](#half_width) | Half-width of the confidence interval. |
| [lci](#lci) | Lower bound of the confidence interval. |
| [std](#std) | Standard deviation of data. |
| [std_error](#std_error) | Standard error of the mean. |
| [uci](#uci) | Upper bound of the confidence interval. |
| [variance](#variance) | Sample variance of the data. |

------------------------------------------------------------------------


#### deviation


Precision of the confidence interval expressed as the percentage


`deviation: float`


deviation of the half width from the mean.


------------------------------------------------------------------------


#### half_width


Half-width of the confidence interval.


`half_width: float`


------------------------------------------------------------------------


#### lci


Lower bound of the confidence interval.


`lci: float`


------------------------------------------------------------------------


#### std


Standard deviation of data.


`std: float`


------------------------------------------------------------------------


#### std_error


Standard error of the mean.


`std_error: float`


------------------------------------------------------------------------


#### uci


Upper bound of the confidence interval.


`uci: float`


------------------------------------------------------------------------


#### variance


Sample variance of the data.


`variance: float`


## Methods

| Name | Description |
|----|----|
| [__init__()](#__init__) | Initialise a new OnlineStatistics object. |
| [notify()](#notify) | Notify all registered observers that an update has occurred. |
| [register_observer()](#register_observer) | Register an observer to be notified on each statistics update. |
| [update()](#update) | Update statistics with a new observation using Welford's algorithm. |

------------------------------------------------------------------------


#### \_\_init\_\_()


Initialise a new OnlineStatistics object.


Usage

``` python
__init__(data=None, alpha=0.1, observer=None)
```


##### Parameters


`data: np.ndarray | None = None`  
Initial dataset to process.

`alpha: float | None = ``0.1`  
Significance level for confidence interval calculations (CI level = 100 \* (1 - alpha) %).

`observer: ReplicationObserver | None = None`  
A user may optionally track the updates to the statistics using a [ReplicationObserver](output_analysis.ReplicationObserver.md#sim_tools.output_analysis.ReplicationObserver) (e.g. `ReplicationTabuliser`). This allows further tabular or visual analysis or saving results to file if required.


##### Raises


`ValueError`  
If `data` is provided but is not a NumPy array.


------------------------------------------------------------------------


#### notify()


Notify all registered observers that an update has occurred.


Usage

``` python
notify()
```


------------------------------------------------------------------------


#### register_observer()


Register an observer to be notified on each statistics update.


Usage

``` python
register_observer(observer)
```


##### Parameters


`observer: ReplicationObserver`  
Object implementing the observer interface.


##### Raises


`ValueError`  
If `observer` is not an instance of ReplicationObserver.


------------------------------------------------------------------------


#### update()


Update statistics with a new observation using Welford's algorithm.


Usage

``` python
update(x)
```


See Knuth. D `The Art of Computer Programming` Vol 2. 2nd ed. Page 216.


##### Parameters


`x: float`  
New observation.
