U
    Kvf                     @   s   d Z ddlZdddZeZedkrejjddd	ZeeZ	e
e	 d
d eeeD Ze
e	dddf edd   eedgdZe
e dS )z5
Created on Wed May 04 06:09:18 2011

@author: josef
    N皙?c                 C   s  d}t | } | j| }t j| |d}|dkr4|}n|t || t }t j||dd}g }tt	|d D ]T}	||	 }
| |
d 
 }| |
d  }|t ||
  }||
||	 ||f qpt |}d}|ddddf ||ddd	df  t d	dgg  }t ||fS )
a  empirical mean residual life or expected shortfall

    Parameters
    ----------
    x : 1-dimensional array_like
    frac : list[float], optional
        All entries must be between 0 and 1
    alpha : float, default 0.05
        FIXME: not actually used.

    TODO:
        check formula for std of mean
        does not include case for all observations
        last observations std is zero
        vectorize loop using cumsum
        frac does not work yet
    r   )axisNright)Zside   g\(\?   )npZasarrayshapesortfloorZastypeintZsearchsortedrangelenmeanZstdsqrtappendarrayZcolumn_stack)xfracalphar   ZnobsZxsortedZ
xthresholdZxlargerindexresultiZk_indrmeanZrstdZrmstdrescritZconfint r   M/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/sandbox/distributions/try_pot.pymean_residual_life
   s&    


<r   __main__   
   )sizec                 C   s   g | ]}t |d   qS )N)rvsr   ).0r   r   r   r   
<listcomp>?   s     r$   r   r   g      ?)r   )Nr   )__doc__Znumpyr   r   Zexpected_shortfall__name__randomZ
standard_tr"   r   printr   r   r   Zres_fracr   r   r   r   <module>   s   
. 