
    M/Ph/                     R    d Z ddlZddlmZ  G d d          Z G d d          ZdS )	zp
This module contains scatterplot smoothers, that is classes
who generate a smooth fit of a set of (x,y) pairs.
    N   )kernelsc                   >    e Zd ZdZd
dZd Zd Zd Zd Zd Z	d	 Z
dS )KernelSmootherz
    1D Kernel Density Regression/Kernel Smoother

    Requires:
    x - array_like of x values
    y - array_like of y values
    Kernel - Kernel object, Default is Gaussian.
    Nc                     |t          j                    }|| _        t          j        |          | _        t          j        |          | _        d S N)r   GaussianKernelnparrayxy)selfr   r   r
   s       k/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/nonparametric/smoothers.py__init__zKernelSmoother.__init__   s>    >%''F!!    c                     d S r    r   s    r   fitzKernelSmoother.fit   s    r   c                 D     t          j         fd|D                       S )Nc                 :    g | ]}                     |          S r   predict.0xxr   s     r   
<listcomp>z+KernelSmoother.__call__.<locals>.<listcomp>#   s%    666bb))666r   r   r   r   r   s   ` r   __call__zKernelSmoother.__call__"   s(    x6666A666777r   c                      t          j        |          dk    r& j                             j         j        |          S t          j         fdt          j        |          D                       S )z
        Returns the kernel smoothed prediction at x

        If x is a real number then a single value is returned.

        Otherwise an attempt is made to cast x to numpy.ndarray and an array of
        corresponding y-points is returned.
        r   c                 \    g | ](}j                             j        j        |          )S r   )r
   smoothr   r   r   s     r   r   z*KernelSmoother.predict.<locals>.<listcomp>1   sC     @ @ @T[//CC @ @ @r   )r   sizer
   r$   r   r   r   r    s   ` r   r   zKernelSmoother.predict%   s     71::??;%%dfdfa8888 @ @ @ @358A;;@ @ @ A A Ar   c                     t          |t                    rQt          j         j                  }|                                 |dd|         }                     |          }||fS t          j         fd|D                       S )ae  
        Returns the fitted curve and 1-sigma upper and lower point-wise
        confidence.
        These bounds are based on variance only, and do not include the bias.
        If the bandwidth is much larger than the curvature of the underlying
        function then the bias could be large.

        x is the points on which you want to evaluate the fit and the errors.

        Alternatively if x is specified as a positive integer, then the fit and
        confidence bands points will be returned after every
        xth sample point - so they are closer together where the data
        is denser.
        Nc                 \    g | ](}j                             j        j        |          )S r   )r
   
smoothconfr   r   r   s     r   r   z'KernelSmoother.conf.<locals>.<listcomp>J   sG     M M MDF "[33DFDFBGG M M Mr   )
isinstanceintr   r   r   sortconf)r   r   sorted_xconfxconffits   `    r   r,   zKernelSmoother.conf4   s     a 	Nx''HMMOOOSSqSMEii&&G7##8 M M M MJKM M M N N Nr   c                 D     t          j         fd|D                       S )Nc                 \    g | ](}j                             j        j        |          )S r   )r
   	smoothvarr   r   r   s     r   r   z&KernelSmoother.var.<locals>.<listcomp>O   s1    OOOr..tvtvrBBOOOr   r   r    s   ` r   varzKernelSmoother.varN   s(    xOOOOQOOOPPPr   c                 P    t          j        |                     |                    S r   )r   sqrtr3   r    s     r   stdzKernelSmoother.stdQ   s    wtxx{{###r   r   )__name__
__module____qualname____doc__r   r   r!   r   r,   r3   r6   r   r   r   r   r      s              8 8 8A A AN N N4Q Q Q$ $ $ $ $r   r   c                   R    e Zd ZdZddZd Zd ZddZd Zd Z	dd	Z
dd
ZddZdS )PolySmootherz
    Polynomial smoother up to a given order.
    Fit based on weighted least squares.

    The x values can be specified at instantiation or when called.

    This is a 3 liner with OLS or WLS, see test.
    It's here as a test smoother for GAM
    Nc                 :   || _         t          j        |dz   ft          j                  | _        hj        dk    r!t          dj                   dd d f         t          j        fdt          |dz             D                       j
        | _        d S d S )Nr   z3Warning: 2d x detected in PolySmoother init, shape:r   c                     g | ]}|z  S r   r   r   ir   s     r   r   z)PolySmoother.__init__.<locals>.<listcomp>r   s    <<<q!t<<<r   )orderr   zerosfloat64coefndimprintshaper   rangeTX)r   rA   r   s     `r   r   zPolySmoother.__init__h   s    
 HeAgZ44	=vzzKQWUUUAaaaC&X<<<<U57^^<<<==?DFFF	 =r   c                 *    |                                  S )z6alias of df_model for backwards compatibility
        )df_modelr   s    r   df_fitzPolySmoother.df_fitt   s     }}r   c                     | j         dz   S )z5
        Degrees of freedom used in the fit.
        r   )rA   r   s    r   rL   zPolySmoother.df_modely   s     zA~r   c                     d S r   r   )r   ds     r   gramzPolySmoother.gram   s    r   c                      | j         |i |S )zoalias for fit,  for backwards compatibility,

        do we need it with different behavior than fit?

        )r   )r   argskwdss      r   r$   zPolySmoother.smooth   s     tx&&&&r   c                 &    | j         | j        z
  dz
  S )z<
        Residual degrees of freedom from last fit.
        r   )NrA   r   s    r   df_residzPolySmoother.df_resid   s     v
"Q&&r   c                 .    |                      |          S )N)r   r   r    s     r   r!   zPolySmoother.__call__   s    ||a|   r   c                    bj         dk    r!t          dj                   d d df         t          j        fdt          | j        dz             D                       }n| j        }|j        d         | j        j        d         k    r,t          j	        t          j
        || j                            S t          j	        t          j
        |j        | j                            S )Nr   6Warning: 2d x detected in PolySmoother predict, shape:r   c                     g | ]}|z  S r   r   r?   s     r   r   z(PolySmoother.predict.<locals>.<listcomp>   s    >>>Q1a4>>>r   )rE   rF   rG   r   r   rH   rA   rJ   rD   squeezedotrI   )r   r   rJ   s    ` r   r   zPolySmoother.predict   s    =vzzNPQPWXXXAAAaC&>>>>%
1*=*=>>>??AAA 71:+++:bfQ	22333:bfQS$)44555r   c                    |j         d         | _        |j        dk    r|d d d f         }|&t          j        |                                          rd}d}nt          j        |          d d d f         } t          | d          st          d          n_j        dk    rt          dj                    t          j
        fdt          | j        dz             D                       j        | _        | j        |z  }||z  }t          j                            ||d          d         | _        t          j        | j                  | _        d S )	Nr   r   rJ   zx needed to fit PolySmootherrZ   c                     g | ]}|z  S r   r   r?   s     r   r   z$PolySmoother.fit.<locals>.<listcomp>   s    CCC!1CCCr   )rcond)rG   rV   rE   r   isnanallr5   hasattr
ValueErrorrF   r   rH   rA   rI   rJ   linalglstsqrD   r\   params)r   r   r   weights_wrJ   _ys     `    r   r   zPolySmoother.fit   sH   6Q;;!!!D&	A?bhw//3355?GBB!!!!!D&)B94%% A !?@@@A vzzNPQPWXXXXCCCCuTZ\/B/BCCCDDFDF FRKV IOOArO44Q7	j++r   r   )NN)r7   r8   r9   r:   r   rM   rL   rQ   r$   rW   r!   r   r   r   r   r   r<   r<   T   s         &
@ 
@ 
@ 
@  
     ' ' '' ' '! ! ! !6 6 6 6", , , , , ,r   r<   )r:   numpyr    r   r   r<   r   r   r   <module>rn      s              C$ C$ C$ C$ C$ C$ C$ C$Jk, k, k, k, k, k, k, k, k, k,r   