
    M/Ph'                         d Z ddlZddlmZ ddlmZ ddZ	ddZ
 G d d	          Zd
 ZddZedk    r e              e             dS dS )z;Kernel Ridge Regression for local non-parametric regression    N)spatial   c                     t          j        | d d t          j        d d f         |t          j        d d d d f         d          }t          j        d|z  |z            S )N   g      )sspminkowski_distance_pnpnewaxisexp)xyscalekwdsdists        u/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/regression/kernridgeregress_class.py
kernel_rbfr   	   sY    #Aaaa
111n$5a
111QQQ6GJJD6$u*d#$$$    r   c                     t          j        | d d t          j        d d f         |t          j        d d d d f         |          S )N)r   minkowski_distancer	   r
   )r   r   pr   s       r   kernel_euclidr      sA    !!AAAbjN"3Abj111n4EaHHHr   c                   8    e Zd ZdZdeddfdZd Zd ZefdZ	dS )	GaussProcessa  class to perform kernel ridge regression (gaussian process)

    Warning: this class is memory intensive, it creates nobs x nobs distance
    matrix and its inverse, where nobs is the number of rows (observations).
    See sparse version for larger number of observations


    Notes
    -----

    Todo:
    * normalize multidimensional x array on demand, either by var or cov
    * add confidence band
    * automatic selection or proposal of smoothing parameters

    Note: this is different from kernel smoothing regression,
       see for example https://en.wikipedia.org/wiki/Kernel_smoother

    In this version of the kernel ridge regression, the training points
    are fitted exactly.
    Needs a fast version for leave-one-out regression, for fitting each
    observation on all the other points.
    This version could be numerically improved for the calculation for many
    different values of the ridge coefficient. see also short summary by
    Isabelle Guyon (ETHZ) in a manuscript KernelRidge.pdf

    Needs verification and possibly additional statistical results or
    summary statistics for interpretation, but this is a problem with
    non-parametric, non-linear methods.

    Reference
    ---------

    Rasmussen, C.E. and C.K.I. Williams, 2006, Gaussian Processes for Machine
    Learning, the MIT Press, www.GaussianProcess.org/gpal, chapter 2

    a short summary of the kernel ridge regression is at
    http://www.ics.uci.edu/~welling/teaching/KernelsICS273B/Kernel-Ridge.pdf
    Ng      ?绽|=c                 8   || _         || _        || _        || _         ||||          | _        t
          j                            | j        t          j        | j        j	         |z  z             | _
        |#|| _        |                     |          | _        dS dS )az  
        Parameters
        ----------
        x : 2d array (N,K)
           data array of explanatory variables, columns represent variables
           rows represent observations
        y : 2d array (N,1) (optional)
           endogenous variable that should be fitted or predicted
           can alternatively be specified as parameter to fit method
        kernel : function, default: kernel_rbf
           kernel: (x1,x2)->kernel matrix is a function that takes as parameter
           two column arrays and return the kernel or distance matrix
        scale : float (optional)
           smoothing parameter for the rbf kernel
        ridgecoeff : float (optional)
           coefficient that is multiplied with the identity matrix in the
           ridge regression

        Notes
        -----
        After initialization, kernel matrix is calculated and if y is given
        as parameter then also the linear regression parameter and the
        fitted or estimated y values, yest, are calculated. yest is available
        as an attribute in this case.

        Both scale and the ridge coefficient smooth the fitted curve.

        r   N)r   kernelr   
ridgecoeffdistxsampler	   linalginveyeshapeKinvr   fityest)selfr   r   r   r   r   r   s          r   __init__zGaussProcess.__init__<   s    > 
$!6!AE222IMM$"2VT%5%;<ZG#H I I	=DFDIII =r   c                     t          j        | j        |          | _        t          j        | j        | j                  }|S )zAfit the training explanatory variables to a sample ouput variable)r	   dotr$   parestr   )r'   r   yhats      r   r%   zGaussProcess.fitg   s2    fTY**vd&t{33r   c                     || _         |                     || j        | j                  }t	          j        || j                  | _        | j        S )z?predict new y values for a given array of explanatory variablesr   )xpredictr   r   r   r	   r*   r+   ypredict)r'   r   distxpredicts      r   predictzGaussProcess.predictq   sA    {{1dfDJ{??|T[99}r   c                     |j                       |j        | j        | j        d| j        | j        d            |j        d            |j                       |j        | j        |d| j        | j        d            |j        d           dS )zsome basic plotsbo-r.-zsample (training) pointsz
all pointsN)figureplotr   r   r&   titler.   r/   )r'   r   plts      r   r6   zGaussProcess.plotx   s     	
tvty%@@@	,---
qt}T]5III	,r   )
__name__
__module____qualname____doc__r   r(   r%   r1   r8   r6    r   r   r   r      sw        & &P !($ ($ ($ ($V                 r   r   c            	      d   d\  } }d}d}t          j        d||           d d t           j        f         }|t          j        d          z  ddt          j        t           j                            | |                    z   z  z   }|t          j        |d d |d d f         d          z  }t          j        t          j	        |          t          j
        |          z   d          d d t           j        f         }|dt           j                            | d          z  z   }d	}t          |d d |d d f         |d d |d d f         t          d
          }	|	                    |          }
t          j                     t          j        ||d||
d           t          j        d           t          j                     t          j        |d|d|
d           t          j        d           t          |d d |d d f         |d d |d d f         t$          |d          }|                    |          }t          j                     t          j        ||d||d           t          j        d           t          j                     t          j        |d|d|d           t          j        d           d S )N)i        
   r   )r   r?   g      ?r   g      ?r   r   r   r   bor.z4euclid kernel: true y versus noisy y and estimated yr3   go-r4   Jeuclid kernel: true (green), noisy (blue) and estimated (red) observationsg?r   r   r   Erbf kernel: true versus noisy (blue) and estimated (red) observationsGrbf kernel: true (green), noisy (blue) and estimated (red) observations)r	   linspacer
   onesr   randomrandnstdsumsinsqrtr   r   r1   r8   r5   r6   r7   r   )mkupperr   xs1axs1y1truey1stridegp1yhatr1gp2yhatr2s                r   example1r^      s   
CAaE
E;qq!!!!!BJ,/D
rwu~~
3rvbiooa.B.B'C'C#C D
DC26#ccc!!!e*QCVBF3KK,Q//"*=F	%")//!A...	.BF
s88V8AAA:r((F(111*~m"') ) )C[[FJLLLHVRVVD111IDEEEJLLLHRfU6%000I     s88V8AAA:r((F(111*~j"t5 5 5C[[FJLLLHVRVVD111IUVVVJLLLHRfU6%000I      r   d   {Gz?c                 N   d}t          j        d||           d d t           j        f         }t          j        t          j        |dz            d          d d t           j        f         |z  }|dt           j                            | d          z  z   }d}t          |d d |d d f         |d d |d d f         t          d          }|	                    |          }	t          j                     t          j        ||d||	d           t          j        d	           t          j                     t          j        |d
|d|	d           t          j        d           t          |d d |d d f         |d d |d d f         t          |d          }
|
	                    |          }t          j                     t          j        ||d||d           t          j        d           t          j                     t          j        |d
|d|d           t          j        d           d S )Nr@   r   r   g?r   rB   rC   rD   zHeuclid kernel: true versus noisy (blue) and estimated (red) observationsr3   rE   r4   rF   r`   rG   rH   rI   )r	   rJ   r
   rO   rP   rL   rM   r   r   r1   r8   r5   r6   r7   r   )rR   r   rY   rT   rV   rW   rX   r   rZ   r[   r\   r]   s               r   example2rb      s   E
+aa
 
 2:
.CVBF36NN1%%aaa
l3C7F	$ryq+++	+BJ
s88V8AAA:r((F(111*~m"') ) )C[[FJLLLHVRVVD111IXYYYJLLLHRfU6%000I     s88V8AAA:r((F(111*~j"t5 5 5C[[FJLLLHVRVVD111IUVVVJLLLHRfU6%000I      r   __main__)r   )r   )r_   r`   r   )r<   numpyr	   scipyr   r   matplotlib.pylabpylabr8   r   r   r   r^   rb   r9   r=   r   r   <module>rh      s    A A                      % % % %I I I Im  m  m  m  m  m  m  m b  F   @ zHJJJ
 HJJJJJ r   