"""
One-sample t test
=================

A one-sample t test assesses whether the mean of a set of observations
differs from a specified datum.

The example below uses a simulated dataset from `Pataky et al. (2015)
<https://doi.org/10.1016/j.jbiomech.2015.02.051>`_ and tests whether their mean
differs from zero. The dataset contains ten observations.
"""

import numpy as np
from matplotlib import pyplot as plt
import spm1d
import jikudata as jd
plt.rc( 'font', size=18 )             # minimum font size for all figure text

dataset = jd.SimulatedPataky2015b()               # 1D dataset:  ten observations
y       = dataset.y                               # (10,101) array of observations
ti      = spm1d.stats.ttest( y, 0 ).inference( 0.05 )   # null hypothesis:  mean = 0

# %%
#  |
#  |
#
#  The figure below depicts observations and the sample mean.
plt.figure( figsize=(6.4, 4.5), tight_layout=True )
ax = plt.gca()
h0 = ax.plot( y.T, color='0.6', lw=0.8 )[0]
h1 = ax.plot( y.mean( axis=0 ), color='#2eb0ce', lw=5 )[0]
ax.legend( [h0,h1], ['Observations', 'Mean'], fontsize=20 )

# %%
#  |
#  |
#
#  The figure below depicts the test statistic and the critical threshold. The
#  probability that smooth, random Gaussian fields would produce a test statistic
#  field that exceeds this threshold --- when there is truly no effect --- is α.
plt.figure( figsize=(8, 4.5), tight_layout=True )
ax = plt.gca()
ti.plot( ax=ax )
ax.yaxis.label.set_size( 20 )
h0, h1 = ax.lines[0], ax.lines[2]                 # the test statistic and the (upper) critical threshold
ax.legend( [h0, h1], ['Test statistic', 'Critical threshold'], fontsize=20, ncol=2, loc='lower center', bbox_to_anchor=(0.5, 1) )   # above the axes
