"""
Effect sizes
============

**spm1d** version 0.5 introduces an ``effect_size`` method that returns effect-size continua.
The returned effect size type depends on the test type:

- t tests: Cohen's d and Hedges' g
- F tests: partial η², η² and ω²
- multivariate tests: Mahalanobis D and multivariate partial η²

For example:
"""

# sphinx_gallery_thumbnail_path = '_static/thumbs/effect_size.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]
spm     = spm1d.stats.ttest_paired( y1, y0 )
es      = spm.effect_size()  # effect-size continua
print( es )

# %%
#  |
#  |
#
#  The figure below depicts the means and standard deviations of the two
#  conditions (top) and Cohen's d (bottom).
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) )
ax0 = fig.add_axes( [0.02, 0.50, 0.96, 0.48] )
ax1 = fig.add_axes( [0.02, 0.08, 0.96, 0.42] )
spm1d.plot.plot_mean_sd( y0, ax=ax0, linecolor='k', facecolor='0.7' )
spm1d.plot.plot_mean_sd( y1, ax=ax0, linecolor='#0e6e84', facecolor='#2eb0ce' )
q = np.arange( es.d.size )
ax1.plot( q, es.d, lw=3, color='#0e6e84' )
ax1.axhline( 0, color='0.6', lw=1 )
ax1.text( 0.02, 0.05, "Cohen's d", transform=ax1.transAxes, ha='left', va='bottom', fontsize=26, color='#0e6e84' )
ax1.set_xlim( ax0.get_xlim() )
ax0.set_xticks( [] );  ax0.set_yticks( [] )
ax1.set_yticks( [] )
ax1.tick_params( labelsize=14 )
for ax in (ax0, ax1):
    for side in ('left', 'right', 'top'):
        ax.spines[side].set_visible( False )
ax0.spines['bottom'].set_visible( False )

# %%
#  |
#  |
#
#  Below Cohen's d and Hedges' g are compared; the latter includes a small-sample
#  correction.
plt.figure( figsize=(8, 4.5), tight_layout=True )
ax = plt.gca()
ax.plot( es.d, lw=3, color='#08343f', label="Cohen's d" )
ax.plot( es.g, lw=2, color='#2eb0ce', ls='--', label="Hedges' g  (small-sample correction)" )
ax.axhline( 0, color='0.6', ls=':' )
ax.legend( loc='lower left' )
ax.set_title( 'Effect-size continua' )
