
p7]c           @   s>   d  Z  d d l Z d d l m Z d d d d d d  Z d S(   sE   
Bland-Altman mean-difference plots

Author: Joses Ho
License: BSD-3
iNi   (   t   utilsg\(\?c         C   s?  t  j |  \ } } t |   t |  k r< t d   n  | d k  r` t d j |    n  t j |  | g d d } |  | }	 t j |	  }
 t j |	 d d } | p i  } d | k r d | d <n  | p i  } | p i  } xF | | g D]8 } d | k rd | d <n  d	 | k r d
 | d	 <q q Wd | k rHd | d <n  d | k rad | d <n  | j | |	 |  | j	 |
 |  | j
 d j t j |
 d   d d' d d d d d d d d | d k rd | | } | j |
 | |
 |  | | } |
 | } |
 | } x0 t | | g  D] \ } } | j	 | |  q'W| j
 d j | t j | d   d d( d d d d d d d d | j
 d j | t j | d   d d) d d d d d d n1 | d k rd! | } | j |
 | |
 |  n  | j d" d d# | j d$ d d# | j d% d&  | j   | S(*   s
  
    Tukey's Mean Difference Plot.

    Tukey's Mean Difference Plot (also known as a Bland-Altman plot) is a
    graphical method to analyze the differences between two methods of
    measurement. The mean of the measures is plotted against their difference.

    For more information see
    https://en.wikipedia.org/wiki/Bland-Altman_plot

    Parameters
    ----------
    m1, m2: pandas Series or array-like
    sd_limit : float, default 1.96
        The limit of agreements expressed in terms of the standard deviation of
        the differences. If `md` is the mean of the differences, and `sd` is
        the standard deviation of those differences, then the limits of
        agreement that will be plotted will be

                       md - sd_limit * sd, md + sd_limit * sd

        The default of 1.96 will produce 95% confidence intervals for the means
        of the differences.
        If sd_limit = 0, no limits will be plotted, and the ylimit of the plot
        defaults to 3 standard deviatons on either side of the mean.
    ax: matplotlib AxesSubplot instance, optional
        If `ax` is None, then a figure is created. If an axis instance is
        given, the mean difference plot is drawn on the axis.
    scatter_kwargs: keywords
        Options to to style the scatter plot. Accepts any keywords for the
        matplotlib Axes.scatter plotting method
    mean_line_kwds: keywords
        Options to to style the scatter plot. Accepts any keywords for the
        matplotlib Axes.axhline plotting method
    limit_lines_kwds: keywords
        Options to to style the scatter plot. Accepts any keywords for the
        matplotlib Axes.axhline plotting method

    Returns
    -------
    fig : matplotlib Figure
        If `ax` is None, the created figure.  Otherwise the figure to which
        `ax` is connected.

    References
    ----------
    Bland JM, Altman DG (1986). "Statistical methods for assessing agreement
    between two methods of clinical measurement"

    Examples
    --------

    Load relevant libraries.

    >>> import statsmodels.api as sm
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt

    Making a mean difference plot.

    >>> # Seed the random number generator.
    >>> # This ensures that the results below are reproducible.
    >>> np.random.seed(9999)
    >>> m1 = np.random.random(20)
    >>> m2 = np.random.random(20)
    >>> f, ax = plt.subplots(1, figsize = (8,5))
    >>> sm.graphics.mean_diff_plot(m1, m2, ax = ax)
    >>> plt.show()

    .. plot:: plots/graphics-mean_diff_plot.py
    s'   m1 does not have the same length as m2.i    s   sd_limit ({}) is less than 0.t   axist   si   t   colort   grayt	   linewidthi   t	   linestyles   --t   :s   mean diff:
{}i   t   xygGz?g      ?t   horizontalalignmentt   rightt   verticalalignmentt   centert   fontsizei   t   xycoordss   axes fractiong      ?s	   -SD{}: {}gQ?t   bottoms	   +SD{}: {}gq=
ףp?i   t
   Differencei   t   Meanst	   labelsizei   (   gGz?g      ?(   gGz?gQ?(   gGz?gq=
ףp?(   R    t   create_mpl_axt   lent
   ValueErrort   formatt   npt   meant   stdt   scattert   axhlinet   annotatet   roundt   set_ylimt	   enumeratet
   set_ylabelt
   set_xlabelt   tick_paramst   tight_layout(   t   m1t   m2t   sd_limitt   axt   scatter_kwdst   mean_line_kwdst   limit_lines_kwdst   figt   meanst   diffst	   mean_difft   std_difft   kwdst	   half_ylimt   limit_of_agreementt   lowert   uppert   jt   lim(    (    s=   lib/python2.7/site-packages/statsmodels/graphics/agreement.pyt   mean_diff_plot   sx    I
!


$$


(   t   __doc__t   numpyR   t    R    t   NoneR7   (    (    (    s=   lib/python2.7/site-packages/statsmodels/graphics/agreement.pyt   <module>   s   	