U
    md`                     @   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

          ?ir   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   )alphad   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