a
    \¹:bh  ã                   @   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eƒ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	   úMlib/python3.9/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 u r:|d u r&t jjd| j|d}n|j|d}| ¡  || _|d u r^t jjd| j|d}n|j|d}t| dƒr„||  	| j¡9 }|  
|¡ | _}|| | _d S )Nr   )ZlocZscaleÚsize)r   Ú	het_scale)r   ZrandomZnormalÚs_xZrvsÚsortr   Ús_noiseÚhasattrr   ÚfuncÚy_trueÚy)ÚselfÚnobsr   Údistr_xÚdistr_noiseZnoiser    r	   r	   r
   Ú__init__P   s    
z_UnivariateFunction.__init__Tc                 C   s~   |du 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   )Zalphaéd   r   Úbzdgp mean)ZlwZcolorZlabel)Zmatplotlib.pyplotZpyplotZfigureZadd_subplotÚplotr   r!   r   ZlinspaceÚminÚmaxr   )r"   ZscatterZaxZpltZfigZxxr	   r	   r
   r+   h   s    z_UnivariateFunction.plot)r   NNN)TN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   r+   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%   ©r5   r	   r
   r&   ™   s    þzUnivariateFanGijbels1.__init__)r   NNN©r.   r/   r0   r   r1   Údocr&   Ú__classcell__r	   r	   r7   r
   r2   •   s   
r2   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   r3   )r   r   r   r   r4   r5   r&   r6   r7   r	   r
   r&   ª   s    þzUnivariateFanGijbels2.__init__)r   NNNr8   r	   r	   r7   r
   r;   §   s   
r;   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚUnivariateFanGijbels1EUz

    Eubank p.179f
    é2   Nc                    sD   |d u rddl m} |j}d| _t| _t| j| ƒj||||d d S )Nr   ©Ústatsg333333Ã?r3   )	Úscipyr?   Úuniformr   r   r   r4   r5   r&   ©r"   r#   r   r$   r%   r?   r7   r	   r
   r&   ¸   s    þz UnivariateFanGijbels1EU.__init__)r=   NNN)r.   r/   r0   r1   r&   r:   r	   r	   r7   r
   r<   ²   s   r<   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 u r*|d u r*ddl m} | dd¡}n
|jd }d| _t| _tt| ƒj	||||d d S )Nr   r>   éþÿÿÿé   r   r3   )
r@   r?   rA   Úshaper   r   r   r4   rC   r&   rB   r7   r	   r
   r&   È   s    
þzUnivariateFunc1.__init__c                 C   s   t  t  d| ¡¡S )Né   )r   ZsqrtÚabs)r"   r   r	   r	   r
   r   Ô   s    zUnivariateFunc1.het_scale)r   NNN)r.   r/   r0   r1   r&   r   r:   r	   r	   r7   r
   rC   Â   s   rC   )r1   Znumpyr   r   r   r   r   r9   Úobjectr   r2   r;   r<   rC   r	   r	   r	   r
   Ú<module>   s$   	úXúÿ