<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/stats/sample_stats.py#L415-L527) |

Computes a running mean.

Inherits From: [`AutoCompositeTensor`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/AutoCompositeTensor)

    tfp.experimental.stats.RunningMean(
        num_samples, mean
    )

In computation, samples can be provided individually or in chunks. A
"chunk" of size M implies incorporating M samples into a single expectation
computation at once, which is more efficient than one by one.

`RunningMean` is meant to serve general streaming expectations.
For a specialized version that fits streaming over MCMC samples, see
`ExpectationsReducer` in [`tfp.experimental.mcmc`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/mcmc).

<br />

<br />

<br />

| ## Args ||
|---|---|
| `num_samples` | A `Tensor` counting the number of samples accumulated so far. |
| `mean` | A `Tensor` broadcast-compatible with `num_samples` giving the current mean. |

<br />

## Methods

### `from_example`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/stats/sample_stats.py#L470-L483)

    @classmethod
    from_example(
        example
    )

Initialize an empty `RunningMean`.

<br />

<br />

<br />

| Args ||
|---|---|
| `example` | A `Tensor`. The `RunningMean` will accept samples of the same dtype and broadcast-compatible shape as the example. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `state` | `RunningMean` representing a stream of no inputs. Note that by convention, the supplied example is used only for initialization, but not counted as a sample. |

<br />

### `from_shape`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/stats/sample_stats.py#L449-L468)

    @classmethod
    from_shape(
        shape, dtype=tf.float32
    )

Initialize an empty `RunningMean`.

<br />

<br />

<br />

| Args ||
|---|---|
| `shape` | Python `Tuple` or `TensorShape` representing the shape of incoming samples. |
| `dtype` | Dtype of incoming samples and the resulting statistics. By default, the dtype is [`tf.float32`](https://www.tensorflow.org/api_docs/python/tf#float32). Any integer dtypes will be cast to corresponding floats (i.e. [`tf.int32`](https://www.tensorflow.org/api_docs/python/tf#int32) will be cast to [`tf.float32`](https://www.tensorflow.org/api_docs/python/tf#float32)), as intermediate calculations should be performing floating-point division. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `state` | `RunningMean` representing a stream of no inputs. |

<br />

### `tree_flatten`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/stats/sample_stats.py#L442-L443)

    tree_flatten()

### `tree_unflatten`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/stats/sample_stats.py#L445-L447)

    @classmethod
    tree_unflatten(
        _, tensors
    )

### `update`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/stats/sample_stats.py#L485-L522)

    update(
        new_sample, axis=None
    )

Update the `RunningMean` with a new sample.

The update formula is from Philippe Pebay (2008) \[1\] and is identical to
that used to calculate the intermediate mean in
[`tfp.experimental.stats.RunningCovariance`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/stats/RunningCovariance) and
[`tfp.experimental.stats.RunningVariance`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/stats/RunningVariance).

<br />

<br />

<br />

| Args ||
|---|---|
| `new_sample` | Incoming `Tensor` sample with shape and dtype compatible with those used to form the `RunningMean`. |
| `axis` | If chunking is desired, this is an integer that specifies the axis with chunked samples. For individual samples, set this to `None`. By default, samples are not chunked (`axis` is None). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `mean` | `RunningMean` updated to the new sample. |

<br />

#### References

\[1\]: Philippe Pebay. Formulas for Robust, One-Pass Parallel Computation of
Covariances and Arbitrary-Order Statistical Moments. *Technical Report
SAND2008-6212* , 2008.
<https://prod-ng.sandia.gov/techlib-noauth/access-control.cgi/2008/086212.pdf>