B
    Z                 @   s   d dl Zd dlmZ d dlZd dlZdZdd Z	dddZ
dd	 Zd
d ZdddZdddZG dd deZdd ZdddZdS )    NZrestructuredtextc             C   s6   t t | t jd}t |d|t |d  S )z
    Return the reciprocal of an array, setting all entries less than or
    equal to 0 to 0. Therefore, it presumes that X should be positive in
    general.
    r   g        )npZmaximumasarrayastypefloat64greaterZ
less_equal)Xx r	   <lib/python3.7/site-packages/statsmodels/sandbox/utils_old.pyrecipr   s    r   /$?c             C   s@   | j }tj| j dd| _ tt| t|  | }|| _ |S )zI
    Median Absolute Deviation:

    median(abs(a - median(a))) / c

    r   )axis)shaper   productZmedianZfabs)acr   _shapemr	   r	   r
   mad   s
    r   c             C   s$   t t | d}t |dd|  S )z
    Return the reciprocal of an array, setting all entries equal to 0
    as 0. It does not assume that X should be positive in
    general.
    r   g      ?)r   equalr   where)r   Ztestr	   r	   r
   recipr0   s    r   c                s:   t j d d} fddt |D }t t |S )zF
    Erase columns of zeros: can save some time in pseudoinverse.
       r   c                s   g | ]} d d |f qS )Nr	   ).0i)matrixr	   r
   
<listcomp>-   s    zclean0.<locals>.<listcomp>)r   addreduceZflatnonzeroarray	transpose)r   Zcolsumvalr	   )r   r
   clean0(   s    r"   -q=c             C   sh   t | } t| jdkrLtj| }tt j	t 
||  |t jS tt t | d S dS )zZ
    Return the rank of a matrix X based on its generalized inverse,
    not the SVD.
    r   g        N)r   r   lenr   scipylinalgZsvdvalsintr   r   r   maxr   Zint32Zalltruer   )r   ZcondDr	   r	   r
   rank0   s
    
(r*   c             C   s   |dkrt | }tj| dd\}}}t|}|ddd }g }x*t|D ]}||dd|| f  qJW tt|	tj
S )z
    Return a matrix whose column span is the same as X.

    If the rank of X is known it can be specified as r -- no check
    is made to ensure that this really is the rank of X.

    Nr   )Zfull_matrices)r*   LZsvdr   argsortrangeappendr   r    r   r   )r   rVr)   Uordervaluer   r	   r	   r
   fullrank<   s    	
r5   c               @   s"   e Zd ZdZd	ddZdd ZdS )
StepFunctiona  
    A basic step function: values at the ends are handled in the simplest
    way possible: everything to the left of x[0] is set to ival; everything
    to the right of x[-1] is set to y[-1].

    Examples
    --------
    >>> from numpy import arange
    >>> from statsmodels.sandbox.utils_old import StepFunction
    >>>
    >>> x = arange(20)
    >>> y = arange(20)
    >>> f = StepFunction(x, y)
    >>>
    >>> print f(3.2)
    3.0
    >>> print f([[3.2,4.5],[24,-3.1]])
    [[  3.   4.]
     [ 19.   0.]]
            Fc             C   s   t |}t |}|j|jkr(tdt|jdkr>tdt t j g|g| _t |g|g| _|st 	| j}t 
| j|d| _t 
| j|d| _| jjd | _d S )Nz3in StepFunction: x and y do not have the same shape   z.in StepFunction: x and y must be 1-dimensionalr   )r   r   r   
ValueErrorr$   Zhstackinfr   yr-   Ztaken)selfr   r;   Zivalsorted_xZ_yZasortr	   r	   r
   __init__f   s    

zStepFunction.__init__c             C   s"   t | j|d }|j}| j| S )Nr8   )r   Zsearchsortedr   r   r;   )r=   ZtimeZtindr   r	   r	   r
   __call__y   s    zStepFunction.__call__N)r7   F)__name__
__module____qualname____doc__r@   rA   r	   r	   r	   r
   r6   P   s   
r6   c             C   sN   t j| dd}|  t j|jdd|_|jd }t |d | }t||S )z9
    Return the ECDF of an array as a step function.
    T)copyr   )r   g      ?)r   r   sortr   r   Zaranger6   )valuesr   r<   r;   r	   r	   r
   ECDF   s    
rI   Tc             K   s`   |r| |f|}n.g }x|D ]}| | |f| qW t|}t|}tj|| || S )z
    Given a monotone function fn (no checking is done to verify monotonicity)
    and a set of x values, return an linearly interpolated approximation
    to its inverse from its values on x.
    )r/   r   r   r-   r%   ZinterpolateZinterp1d)fnr   Z
vectorizedkeywordsr;   r?   r   r	   r	   r
   monotone_fn_inverter   s    


rL   )r   r   )r#   )N)T)Znumpyr   Znumpy.linalgr&   r,   Zscipy.interpolater%   Zscipy.linalgZ__docformat__r   r   r   r"   r*   r5   objectr6   rI   rL   r	   r	   r	   r
   <module>   s   	
	

/