fips.aggregate.mean#

fips.aggregate.mean(space, by, blocks=None, dropna=False)[source]#

Return the operator that averages the labels of space by group.

Parameters:
  • space (Space) – The labels to average: the observations’ or the state’s.

  • by (Any) – The groups, as sum() takes them.

  • blocks (str | Sequence[str] | None) – The blocks to average, each on its own; every block by default. A block not named is passed through, one row for each of its labels (left out with dropna).

  • dropna (bool) – Leave out the labels a key cannot place and the blocks not named in blocks, as sum() does; a mean is over the labels kept.

Returns:

\(W\), sparse, with one row per group (1/n for each of its n labels) and space as its columns, its rows labeled as sum() labels them. \(W S W^T\) divides the variance of independent errors by n.

Return type:

Operator

Raises:

See also

sum

The operator that sums by group.

fips.Problem.aggregate

The problem of aggregated observations.

Examples

Hourly observations at two sites, to daily means at each:

>>> import numpy as np
>>> import pandas as pd
>>> import fips
>>> labels = pd.MultiIndex.from_product(
...     [["A", "B"], pd.date_range("2020-01-01", periods=4, freq="12h")],
...     names=["site", "obs_time"],
... )
>>> obs = fips.Vector.from_series(
...     pd.Series(np.arange(8.0), index=labels), block="co2"
... )
>>> W = fips.aggregate.mean(
...     obs.space, by=["site", pd.Grouper(level="obs_time", freq="1D")]
... )
>>> W.rows.select("co2")
MultiIndex([('A', '2020-01-01'),
            ('A', '2020-01-02'),
            ('B', '2020-01-01'),
            ('B', '2020-01-02')],
           names=['site', 'obs_time'])
>>> (W @ obs).values
array([0.5, 2.5, 4.5, 6.5])