
    'YHh3                         d Z ddlZd Zd Zd Zd Zddd	Z G d
 d      Zddd	Z G d de      Z	ded<    G d de      Z
 G d de      Z G d de      Zy)z|Examples of non-linear functions for non-parametric regression

Created on Sat Jan 05 20:21:22 2013

Author: Josef Perktold
    Nc                 D    | dt        j                  d| dz  z        z  z   S )z(Fan and Gijbels example function 1

       npexpxs    g/var/www/html/planif/env/lib/python3.12/site-packages/statsmodels/sandbox/nonparametric/dgp_examples.pyfg1r      s%     q266#1*%%%%    c                 J    | dt        j                  d| dz
  dz  z        z  z   S )z:Eubank similar to Fan and Gijbels example function 1

          ?ir   r   r	   s    r   fg1eur      s*     sRVVC1s7Q,.////r   c                 p    t        j                  d| z        dt        j                  d| dz  z        z  z   S )z(Fan and Gijbels example function 2

    r   r   )r   sinr   r	   s    r   fg2r      s1     66!a%=1rvvcAqDj1111r   c                 V    t        j                  | dz        | z  d| z  z   d| dz  z  z
  S )z&made up example with sin, square

              @      ?r   )r   r   r	   s    r   func1r      s1     66!a%=1rAv%QT	11r   zuBase Class for Univariate non-linear example

    Does not work on it's own.
    needs additional at least self.func
 )descriptionrefc                        e Zd ZdZddZddZy)_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                    |Q|-t         j                  j                  d| j                  |      }n|j	                  |      }|j                          || _        |-t         j                  j                  d| j                  |      }n|j	                  |      }t        | d      r|| j                  | j                        z  }| j                  |      x| _        }||z   | _        y )Nr   )locscalesize)r!   	het_scale)r   randomnormals_xrvssortr
   s_noisehasattrr"   funcy_truey)selfnobsr
   distr_xdistr_noisenoiser+   s          r   __init__z_UnivariateFunction.__init__O   s    9II$$$(($FKKTK*FFHII$$$,,T$JEOOO.E4%T^^DFF++E  $yy|+f%r   c                    |)ddl m} |j                         }|j                  ddd      }|r)|j	                  | j
                  | j                  dd       t        j                  | j
                  j                         | j
                  j                         d      }|j	                  || j                  |      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)lwcolorlabel)matplotlib.pyplotpyplotfigureadd_subplotplotr
   r,   r   linspaceminmaxr*   )r-   scatteraxpltfigxxs         r   r@   z_UnivariateFunction.plotg   s    $ :+**,CAq)BGGDFFDFFCsG3[[tvvzz|S9
DIIbMas*Eyyr      NNN)TN)__name__
__module____qualname____doc__r2   r@    r   r   r   r   .   s    G< 0r   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                   >     e Zd Zej                  ez  Zd fd	Z xZS )UnivariateFanGijbels1c                 r    d| _         d| _        t        | _        t        | j
                  |   ||||       y )Nr   gffffff?r.   r
   r/   r0   )r%   r(   r   r*   super	__class__r2   r-   r.   r
   r/   r0   rU   s        r   r2   zUnivariateFanGijbels1.__init__   ;    	dnnd,$!5<9D 	- 	Fr   rI   rK   rL   rM   r   rN   docr2   __classcell__rU   s   @r   rQ   rQ      s    !))C/GF Fr   rQ   z4Fan and Gijbels example function 2

sin plus a hump
r   c                   >     e Zd Zej                  ez  Zd fd	Z xZS )UnivariateFanGijbels2c                 r    d| _         d| _        t        | _        t        | j
                  |   ||||       y )Nr   r   rS   )r%   r(   r   r*   rT   rU   r2   rV   s        r   r2   zUnivariateFanGijbels2.__init__   rW   r   rI   rX   r[   s   @r   r]   r]      s    !))C/GF Fr   r]   c                   $     e Zd ZdZd fd	Z xZS )UnivariateFanGijbels1EUz

    Eubank p.179f
    c                     |ddl m} |j                  }d| _        t        | _        t        | j                  | #  ||||       y )Nr   statsg333333?rS   )	scipyrc   uniformr(   r   r*   rT   rU   r2   r-   r.   r
   r/   r0   rc   rU   s         r   r2   z UnivariateFanGijbels1EU.__init__   sE    ?#mmG	dnnd,$!5<9D 	- 	Fr   )2   NNN)rK   rL   rM   rN   r2   rZ   r[   s   @r   r`   r`      s    
F Fr   r`   c                   *     e Zd ZdZd fd	Zd Z xZS )UnivariateFunc1z0

    made up, with sin and quadratic trend
    c                     ||ddl m} |j                  dd      }n|j                  d   }d| _        t
        | _        t        | !  ||||       y )Nr   rb      r   rS   )	rd   rc   re   shaper(   r   r*   rT   r2   rf   s         r   r2   zUnivariateFunc1.__init__   sW    9#mmB*G771:D	da5<9D 	 	Fr   c                 X    t        j                  t        j                  d|z               S )N   )r   sqrtabs)r-   r
   s     r   r"   zUnivariateFunc1.het_scale   s    wwrvvac{##r   rI   )rK   rL   rM   rN   r2   r"   rZ   r[   s   @r   ri   ri      s    

F$r   ri   )rN   numpyr   r   r   r   r   rY   r   rQ   r]   r`   ri   rO   r   r   <module>rs      s    &022
 
U Up

F/ 
F M 	F/ 	FF1 F $) $r   