"""
Directional hypotheses
======================

**spm1d** 0.5 supports one- and two-tailed tests via the ``dirn``
keyword argument:

- ``inference( alpha, dirn=0 )`` --- two-tailed (default)
- ``inference( alpha, dirn=1 )`` --- one-tailed, positive direction
- ``inference( alpha, dirn=-1 )`` --- one-tailed, negative direction

Refer to :doc:`/theory/directional` for more details.
"""

# sphinx_gallery_thumbnail_path = '_static/thumbs/directional.png'
import numpy as np
from matplotlib import pyplot as plt
import spm1d
import jikudata as jd

dataset = jd.PlantarArchAngle()     # 1D dataset:  two conditions, ten participants
y, A    = dataset.y, dataset.x
y0, y1  = y[A == 0], y[A == 1]
t       = spm1d.stats.ttest_paired( y1, y0 )

# %%
#  |
#  |
#
#  **spm1d** 0.5 also supports the version 0.4-style use of
#  ``two_tailed=True``, but this is deprecated and will be unavailable
#  in future versions.
#
#  The figure below depicts the critical thresholds for a single test statistic
#  under the three ``dirn`` possibilities.
plt.rc( 'font', family='sans-serif' )
plt.rc( 'font', **{'sans-serif': ['Helvetica Neue', 'Helvetica', 'Arial', 'Liberation Sans', 'DejaVu Sans']} )
fig    = plt.figure( figsize=(8, 6) )
ax     = fig.add_axes( [0.02, 0.08, 0.96, 0.90] )
ti0    = t.inference( 0.05, dirn=0 )      # two-tailed
ti1    = t.inference( 0.05, dirn=1 )      # one-tailed, positive direction
ti2    = t.inference( 0.05, dirn=-1 )     # one-tailed, negative direction
ax.plot( t.z, 'k', lw=3 )
ax.axhline( 0, color='0.6', ls=':' )
ax.axhline(  ti0.zc, color='k',  ls='--', lw=2.5 )
ax.axhline( -ti0.zc, color='k',  ls='--', lw=2.5 )
ax.axhline(  ti1.zc, color='#0e6e84', ls='--', lw=2.5 )
ax.axhline( -ti2.zc, color='#2eb0ce', ls='--', lw=2.5 )
ax.text( 30, -ti0.zc - 0.15, f'dirn =  0:   h0reject = {ti0.h0reject}', ha='left', va='top',    fontsize=22, color='k' )
ax.text( 30,  ti1.zc - 0.15, f'dirn =  1:   h0reject = {ti1.h0reject}', ha='left', va='top',    fontsize=22, color='#0e6e84' )
ax.text( 30, -ti2.zc + 0.15, f'dirn = -1:   h0reject = {ti2.h0reject}', ha='left', va='bottom', fontsize=22, color='#2eb0ce' )
ax.set_xlim( 0, 100 )
ax.set_ylim( min( t.z.min() - 0.3, -1.35 * ti0.zc ), 1.3 * ti0.zc )
ax.set_yticks( [] )
ax.tick_params( labelsize=14 )
for side in ('left', 'right', 'top'):
    ax.spines[side].set_visible( False )

# %%
#  |
#  |
#
#  A one-tailed test has a lower threshold so is more sensitive to
#  effects in its specified direction. However it cannot detect effects
#  in the opposite direction.
for dirn in (0, 1, -1):
    ti = t.inference( 0.05, dirn=dirn )
    print( f'dirn = {dirn:2d}:  zc = {ti.zc:.3f},  h0reject = {ti.h0reject}' )
