# dask.array.stats.ttest_1samp

### dask.array.stats.ttest_1samp(a, popmean, axis=0, nan_policy='propagate')

Calculate the T-test for the mean of ONE group of scores.

This docstring was copied from scipy.stats.ttest_1samp.

Some inconsistencies with the Dask version may exist.

This is a test for the null hypothesis that the expected value
(mean) of a sample of independent observations a is equal to the given
population mean, popmean.

* **Parameters:**
  **a**
  : Sample observations.

  **popmean**
  : Expected value in null hypothesis. If array_like, then its length along
    axis must equal 1, and it must otherwise be broadcastable with a.

  **axis**
  : If an int, the axis of the input along which to compute the statistic.
    The statistic of each axis-slice (e.g. row) of the input will appear in a
    corresponding element of the output.
    If `None`, the input will be raveled before computing the statistic.

  **nan_policy**
  : Defines how to handle input NaNs.
    - `propagate`: if a NaN is present in the axis slice (e.g. row) along
      which the  statistic is computed, the corresponding entry of the output
      will be NaN.
    - `omit`: NaNs will be omitted when performing the calculation.
      If insufficient data remains in the axis slice along which the
      statistic is computed, the corresponding entry of the output will be
      NaN.
    - `raise`: if a NaN is present, a `ValueError` will be raised.

  **alternative**
  : Defines the alternative hypothesis.
    The following options are available (default is ‘two-sided’):
    * ‘two-sided’: the mean of the underlying distribution of the sample
      is different than the given population mean (popmean)
    * ‘less’: the mean of the underlying distribution of the sample is
      less than the given population mean (popmean)
    * ‘greater’: the mean of the underlying distribution of the sample is
      greater than the given population mean (popmean)

  **keepdims**
  : If this is set to True, the axes which are reduced are left
    in the result as dimensions with size one. With this option,
    the result will broadcast correctly against the input array.
* **Returns:**
  **result**
  : An object with the following attributes:
    <br/>
    statistic
    : The t-statistic.
    <br/>
    pvalue
    : The p-value associated with the given alternative.
    <br/>
    df
    : The number of degrees of freedom used in calculation of the
      t-statistic; this is one less than the size of the sample
      (`a.shape[axis]`).
      <br/>
      #### Versionadded
      Added in version 1.10.0.
    <br/>
    The object also has the following method:
    <br/>
    confidence_interval(confidence_level=0.95)
    : Computes a confidence interval around the population
      mean for the given confidence level.
      The confidence interval is returned in a `namedtuple` with
      fields low and high.
      <br/>
      #### Versionadded
      Added in version 1.10.0.

### Notes

The statistic is calculated as `(np.mean(a) - popmean)/se`, where
`se` is the standard error. Therefore, the statistic will be positive
when the sample mean is greater than the population mean and negative when
the sample mean is less than the population mean.

Beginning in SciPy 1.9, `np.matrix` inputs (not recommended for new
code) are converted to `np.ndarray` before the calculation is performed. In
this case, the output will be a scalar or `np.ndarray` of appropriate shape
rather than a 2D `np.matrix`. Similarly, while masked elements of masked
arrays are ignored, the output will be a scalar or `np.ndarray` rather than a
masked array with `mask=False`.

**Array API Standard Support**

ttest_1samp has experimental support for Python Array API Standard compatible
backends in addition to NumPy. Please consider testing these features
by setting an environment variable `SCIPY_ARRAY_API=1` and providing
CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following
combinations of backend and device (or other capability) are supported.

| Library   | CPU   | GPU   |
|-----------|-------|-------|
| NumPy     | ✅    | n/a   |
| CuPy      | n/a   | ✅    |
| PyTorch   | ✅    | ⛔    |
| JAX       | ✅    | ✅    |
| Dask      | ✅    | n/a   |

ttest_1samp also accepts [MArrays](https://mdhaber.github.io/marray/tutorial.html) backed by the backends indicated above; masked values will be treated as though they were not present.
The `confidence_interval` method of the output object is incompatible with JAX JIT.
See [Support for the array API standard](https://docs.scipy.org/doc/scipy/dev/api-dev/array_api.html#dev-arrayapi) for more information.

### Examples

Suppose we wish to test the null hypothesis that the mean of a population
is equal to 0.5. We choose a confidence level of 99%; that is, we will
reject the null hypothesis in favor of the alternative if the p-value is
less than 0.01.

When testing random variates from the standard uniform distribution, which
has a mean of 0.5, we expect the data to be consistent with the null
hypothesis most of the time.

```pycon
>>> import numpy as np
>>> from scipy import stats
>>> rng = np.random.default_rng()
>>> rvs = stats.uniform.rvs(size=50, random_state=rng)
>>> stats.ttest_1samp(rvs, popmean=0.5)
TtestResult(statistic=2.456308468440, pvalue=0.017628209047638, df=49)
```

As expected, the p-value of 0.017 is not below our threshold of 0.01, so
we cannot reject the null hypothesis.

When testing data from the standard *normal* distribution, which has a mean
of 0, we would expect the null hypothesis to be rejected.

```pycon
>>> rvs = stats.norm.rvs(size=50, random_state=rng)
>>> stats.ttest_1samp(rvs, popmean=0.5)
TtestResult(statistic=-7.433605518875, pvalue=1.416760157221e-09, df=49)
```

Indeed, the p-value is lower than our threshold of 0.01, so we reject the
null hypothesis in favor of the default “two-sided” alternative: the mean
of the population is *not* equal to 0.5.

However, suppose we were to test the null hypothesis against the
one-sided alternative that the mean of the population is *greater* than
0.5. Since the mean of the standard normal is less than 0.5, we would not
expect the null hypothesis to be rejected.

```pycon
>>> stats.ttest_1samp(rvs, popmean=0.5, alternative='greater')
TtestResult(statistic=-7.433605518875, pvalue=0.99999999929, df=49)
```

Unsurprisingly, with a p-value greater than our threshold, we would not
reject the null hypothesis.

Note that when working with a confidence level of 99%, a true null
hypothesis will be rejected approximately 1% of the time.

```pycon
>>> rvs = stats.uniform.rvs(size=(100, 50), random_state=rng)
>>> res = stats.ttest_1samp(rvs, popmean=0.5, axis=1)
>>> np.sum(res.pvalue < 0.01)
1
```

Indeed, even though all 100 samples above were drawn from the standard
uniform distribution, which *does* have a population mean of 0.5, we would
mistakenly reject the null hypothesis for one of them.

ttest_1samp can also compute a confidence interval around the population
mean.

```pycon
>>> rvs = stats.norm.rvs(size=50, random_state=rng)
>>> res = stats.ttest_1samp(rvs, popmean=0)
>>> ci = res.confidence_interval(confidence_level=0.95)
>>> ci
ConfidenceInterval(low=-0.3193887540880017, high=0.2898583388980972)
```

The bounds of the 95% confidence interval are the
minimum and maximum values of the parameter popmean for which the
p-value of the test would be 0.05.

```pycon
>>> res = stats.ttest_1samp(rvs, popmean=ci.low)
>>> np.testing.assert_allclose(res.pvalue, 0.05)
>>> res = stats.ttest_1samp(rvs, popmean=ci.high)
>>> np.testing.assert_allclose(res.pvalue, 0.05)
```

Under certain assumptions about the population from which a sample
is drawn, the confidence interval with confidence level 95% is expected
to contain the true population mean in 95% of sample replications.

```pycon
>>> rvs = stats.norm.rvs(size=(50, 1000), loc=1, random_state=rng)
>>> res = stats.ttest_1samp(rvs, popmean=0)
>>> ci = res.confidence_interval()
>>> contains_pop_mean = (ci.low < 1) & (ci.high > 1)
>>> contains_pop_mean.sum()
953
```

<!-- !! processed by numpydoc !! -->
