U
    Ãmœd`  ã                   @   sš   d Z ddlZdd„ Zdd„ Zdd„ Zd	d
„ ZdddœZG dd„ dƒZdddœZG dd„ deƒZ	ded< G dd„ deƒZ
G dd„ deƒZG dd„ deƒZdS )z|Examples of non-linear functions for non-parametric regression

Created on Sat Jan 05 20:21:22 2013

Author: Josef Perktold
é    Nc                 C   s   | dt  d| d  ¡  S )z(Fan and Gijbels example function 1

    é   éðÿÿÿ©ÚnpÚexp©Úx© r	   úg/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/sandbox/nonparametric/dgp_examples.pyÚfg1   s    r   c                 C   s   | dt  d| d d  ¡  S )z:Eubank similar to Fan and Gijbels example function 1

    ç      à?iÎÿÿÿr   r   r   r	   r	   r
   Úfg1eu   s    r   c                 C   s$   t  d|  ¡dt  d| d  ¡  S )z(Fan and Gijbels example function 2

    r   r   )r   Úsinr   r   r	   r	   r
   Úfg2   s    r   c                 C   s&   t  | d ¡|  d|   d| d   S )z&made up example with sin, square

    é   ç       @ç      ð?r   )r   r   r   r	   r	   r
   Úfunc1   s    r   zuBase Class for Univariate non-linear example

    Does not work on it's own.
    needs additional at least self.func
Ú )ÚdescriptionÚrefc                   @   s$   e Zd ZdZd	dd„Zd
dd„ZdS )Ú_UnivariateFunctiona’  %(description)s

    Parameters
    ----------
    nobs : int
        number of observations to simulate
    x : None or 1d array
        If x is given then it is used for the exogenous variable instead of
        creating a random sample
    distr_x : None or distribution instance
        Only used if x is None. The rvs method is used to create a random
        sample of the exogenous (explanatory) variable.
    distr_noise : None or distribution instance
        The rvs method is used to create a random sample of the errors.

    Attributes
    ----------
    x : ndarray, 1-D
        exogenous or explanatory variable. x is sorted.
    y : ndarray, 1-D
        endogenous or response variable
    y_true : ndarray, 1-D
        expected values of endogenous or response variable, i.e. values of y
        without noise
    func : callable
        underlying function (defined by subclass)

    %(ref)s
    éÈ   Nc                 C   s¢   |d kr:|d kr&t jjd| j|d�}n|j|d�}| ¡  || _|d kr^t jjd| j|d�}n|j|d�}t| dƒr„||  	| j¡9 }|  
|¡ | _}|| | _d S )Nr   )ÚlocÚscaleÚsize)r   Ú	het_scale)r   ÚrandomÚnormalÚs_xZrvsÚsortr   Ús_noiseÚhasattrr   ÚfuncÚy_trueÚy)ÚselfÚnobsr   Údistr_xÚdistr_noiseÚnoiser$   r	   r	   r
   Ú__init__P   s    
z_UnivariateFunction.__init__Tc                 C   s~   |dkr*ddl m} | ¡ }| ddd¡}|rD|j| j| jddd� t | j 	¡ | j 
¡ d¡}|j||  |¡dd	d
d� |jS )a	  plot the mean function and optionally the scatter of the sample

        Parameters
        ----------
        scatter : bool
            If true, then add scatterpoints of sample to plot.
        ax : None or matplotlib axis instance
            If None, then a matplotlib.pyplot figure is created, otherwise
            the given axis, ax, is used.

        Returns
        -------
        Figure
            This is either the created figure instance or the one associated
            with ax if ax is given.

        Nr   é   Úor   )Úalphaéd   r   Úbzdgp mean)ZlwÚcolorÚlabel)Zmatplotlib.pyplotZpyplotZfigureZadd_subplotÚplotr   r%   r   ZlinspaceÚminÚmaxr#   )r&   ZscatterZaxZpltZfigZxxr	   r	   r
   r3   h   s    z_UnivariateFunction.plot)r   NNN)TN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r3   r	   r	   r	   r
   r   /   s   
r   z=Fan and Gijbels example function 1

linear trend plus a hump
zÈ
References
----------
Fan, Jianqing, and Irene Gijbels. 1992. "Variable Bandwidth and Local
Linear Regression Smoothers."
The Annals of Statistics 20 (4) (December): 2008-2036. doi:10.2307/2242378.

c                       s(   e Zd Zeje Zd‡ fdd„	Z‡  ZS )ÚUnivariateFanGijbels1r   Nc                    s0   d| _ d| _t| _t| j| ƒj||||d� d S )Nr   gffffffæ?©r'   r   r(   r)   )r   r!   r   r#   ÚsuperÚ	__class__r+   ©r&   r'   r   r(   r)   ©r=   r	   r
   r+   ™   s    þzUnivariateFanGijbels1.__init__)r   NNN©r6   r7   r8   r   r9   Údocr+   Ú__classcell__r	   r	   r?   r
   r:   •   s   
r:   z4Fan and Gijbels example function 2

sin plus a hump
r   c                       s(   e Zd Zeje Zd‡ fdd„	Z‡  ZS )ÚUnivariateFanGijbels2r   Nc                    s0   d| _ d| _t| _t| j| ƒj||||d� d S )Nr   r   r;   )r   r!   r   r#   r<   r=   r+   r>   r?   r	   r
   r+   ª   s    þzUnivariateFanGijbels2.__init__)r   NNNr@   r	   r	   r?   r
   rC   §   s   
rC   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚUnivariateFanGijbels1EUz

    Eubank p.179f
    é2   Nc                    sD   |d krddl m} |j}d| _t| _t| j| ƒj||||d� d S )Nr   ©Ústatsg333333Ã?r;   )	ÚscipyrG   Úuniformr!   r   r#   r<   r=   r+   ©r&   r'   r   r(   r)   rG   r?   r	   r
   r+   ¸   s    þz UnivariateFanGijbels1EU.__init__)rE   NNN)r6   r7   r8   r9   r+   rB   r	   r	   r?   r
   rD   ²   s   rD   c                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )	ÚUnivariateFunc1z0

    made up, with sin and quadratic trend
    r   Nc                    s\   |d kr*|d kr*ddl m} | dd¡}n
|jd }d| _t| _tt| ƒj	||||d� d S )Nr   rF   éþÿÿÿé   r   r;   )
rH   rG   rI   Úshaper!   r   r#   r<   rK   r+   rJ   r?   r	   r
   r+   È   s    
þzUnivariateFunc1.__init__c                 C   s   t  t  d| ¡¡S )Né   )r   ÚsqrtÚabs)r&   r   r	   r	   r
   r   Ô   s    zUnivariateFunc1.het_scale)r   NNN)r6   r7   r8   r9   r+   r   rB   r	   r	   r?   r
   rK   Â   s   rK   )r9   Únumpyr   r   r   r   r   rA   r   r:   rC   rD   rK   r	   r	   r	   r
   Ú<module>   s$   	úXúÿ