
    ה9iRR                         d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	 ddl
m
Z
 ddlZddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZmZmZmZ ddl m!Z!m"Z" ddl#m$Z$m%Z% d Z& G d deee      Z'y)zBase class for mixture models.    N)ABCMetaabstractmethod)nullcontext)IntegralReal)time)cluster)BaseEstimatorDensityMixin_fit_context)kmeans_plusplus)ConvergenceWarning)check_random_state)_convert_to_numpy_is_numpy_namespace
_logsumexp_max_precision_float_dtypeget_namespaceget_namespace_and_device)Interval
StrOptions)check_is_fittedvalidate_datac                 ^    | j                   |k7  rt        d|d|d| j                         y)zValidate the shape of the input parameter 'param'.

    Parameters
    ----------
    param : array

    param_shape : tuple

    name : str
    zThe parameter 'z' should have the shape of z
, but got N)shape
ValueError)paramparam_shapenames      _/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/sklearn/mixture/_base.py_check_shaper!      s2     {{k![%++/
 	
 "    c                      e Zd ZU dZ eeddd      g eeddd      g eeddd      g eeddd      g eeddd      g eh d      gd	gd
gdg eeddd      gd
Ze	e
d<   d Zed&d       Zd&dZed        Zd&dZ ed      d&d       Zd&dZed        Zed        Zed        Zd Zd&dZd Zd Zd'dZd&dZed&d        Zed&d!       Zd&d"Zd# Z d$ Z!d% Z"y)(BaseMixturezBase class for mixture models.

    This abstract class specifies an interface for all mixture classes and
    provides basic common methods for mixture models.
       Nleft)closedg        r   >   kmeansrandomrandom_from_data	k-means++random_statebooleanverbose
n_componentstol	reg_covarmax_itern_initinit_paramsr,   
warm_startr.   verbose_interval_parameter_constraintsc                     || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        y Nr/   )selfr0   r1   r2   r3   r4   r5   r,   r6   r.   r7   s              r    __init__zBaseMixture.__init__G   sN     )" &($ 0r"   c                      y)zCheck initial parameters of the derived class.

        Parameters
        ----------
        X : array-like of shape  (n_samples, n_features)
        N r;   Xxps      r    _check_parameterszBaseMixture._check_parameters_        	r"   c                 J   t        ||      \  }}}|j                  \  }}| j                  dk(  rt        j                  || j
                  f|j                        }t        j                  | j
                  d|      j                  |      j                  }d|t        j                  |      |f<   n^| j                  dk(  rb|j                  |j                  || j
                  f      |j                  |      }||j                  |d	      d
d
|j                  f   z  }n| j                  dk(  rc|j	                  || j
                  f|j                  |      }|j!                  || j
                  d      }	t#        |	      D ]  \  }
}d|||
f<    n{| j                  dk(  rlt        j                  || j
                  f|j                        }t%        || j
                  |      \  }}	d||	t        j                  | j
                        f<   | j'                  |       y
)a?  Initialize the model parameters.

        Parameters
        ----------
        X : array-like of shape  (n_samples, n_features)

        random_state : RandomState
            A random number generator instance that controls the random seed
            used for the method chosen to initialize the parameters.
        rA   r(   )dtyper%   )
n_clustersr4   r,   r)   sizerF   deviceaxisNr*   F)rI   replacer+   )r,   )r   r   r5   npzerosr0   rF   r	   KMeansfitlabels_arangeasarrayuniformsumnewaxischoice	enumerater   _initialize)r;   r@   r,   rA   _rK   	n_samplesresplabelindicescolindexs               r    _initialize_parametersz"BaseMixture._initialize_parametersi   s    1r:Avww	1x'88Y(9(9:!''JD#00 Q  12D9%u,-)::$$9d6G6G*H$Igg  D
 BFF4aF(BJJ77D!3388D--.aggf  D #)) 1 15 * G (0 %
U#$UCZ %,88Y(9(9:!''JD(!!)JAw
 ;<D"))D$5$5667D!r"   c                      y)zInitialize the model parameters of the derived class.

        Parameters
        ----------
        X : array-like of shape  (n_samples, n_features)

        resp : array-like of shape (n_samples, n_components)
        Nr>   )r;   r@   r^   s      r    r[   zBaseMixture._initialize   s     	r"   c                 *    | j                  ||       | S )a  Estimate model parameters with the EM algorithm.

        The method fits the model ``n_init`` times and sets the parameters with
        which the model has the largest likelihood or lower bound. Within each
        trial, the method iterates between E-step and M-step for ``max_iter``
        times until the change of likelihood or lower bound is less than
        ``tol``, otherwise, a ``ConvergenceWarning`` is raised.
        If ``warm_start`` is ``True``, then ``n_init`` is ignored and a single
        initialization is performed upon the first call. Upon consecutive
        calls, training starts where it left off.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            List of n_features-dimensional data points. Each row
            corresponds to a single data point.

        y : Ignored
            Not used, present for API consistency by convention.

        Returns
        -------
        self : object
            The fitted mixture.
        )fit_predict)r;   r@   ys      r    rR   zBaseMixture.fit   s    6 	Ar"   T)prefer_skip_nested_validationc                    t        |      \  }}t        | ||j                  |j                  gd      }|j                  d   | j
                  k  r(t        d| j
                   d|j                  d          | j                  ||       | j                  xr t        | d       }|r| j                  nd}|j                   }g }d	| _        t        | j                        }	|j                  \  }
}t        |      D ]Q  }| j!                  |       |r| j#                  ||	|       |r|j                   n| j$                  }g }| j&                  dk(  r| j)                         }d}jd	}t        d| j&                  dz         D ]  }|}| j+                  ||      \  }}| j-                  |||       | j/                  ||      }|j1                  |       ||z
  }| j3                  ||       t5        |      | j6                  k  sd
} n | j9                  ||       ||kD  s||j                   k(  s5|}| j)                         }}|}|| _        T | j                  s)| j&                  dkD  rt;        j<                  dt>               | jA                  |       | _!        || _        || _"        | j+                  ||      \  }}|jG                  |d      S )a  Estimate model parameters using X and predict the labels for X.

        The method fits the model ``n_init`` times and sets the parameters with
        which the model has the largest likelihood or lower bound. Within each
        trial, the method iterates between E-step and M-step for `max_iter`
        times until the change of likelihood or lower bound is less than
        `tol`, otherwise, a :class:`~sklearn.exceptions.ConvergenceWarning` is
        raised. After fitting, it predicts the most probable label for the
        input data points.

        .. versionadded:: 0.20

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            List of n_features-dimensional data points. Each row
            corresponds to a single data point.

        y : Ignored
            Not used, present for API consistency by convention.

        Returns
        -------
        labels : array, shape (n_samples,)
            Component labels.
           )rF   ensure_min_samplesr   z:Expected n_samples >= n_components but got n_components = z, n_samples = rE   
converged_r%   FTzBest performing initialization did not converge. Try different init parameters, or increase max_iter, tol, or check for degenerate data.rL   )$r   r   float64float32r   r0   r   rB   r6   hasattrr4   infrl   r   r,   range_print_verbose_msg_init_begrc   lower_bound_r3   _get_parameters_e_step_m_step_compute_lower_boundappend_print_verbose_msg_iter_endabsr1   _print_verbose_msg_init_endwarningswarnr   _set_parametersn_iter_lower_bounds_argmax)r;   r@   rg   rA   r\   do_initr4   max_lower_boundbest_lower_boundsr,   r]   initlower_boundcurrent_lower_boundsbest_paramsbest_n_iter	convergedn_iterprev_lower_boundlog_prob_normlog_respchanges                         r    rf   zBaseMixture.fit_predict   s   8 a A$"**bjj)AVWX771:)))**.*;*;)< = wwqzl, 
 	qR( F74+FG 'Q66')$*;*;<ww	1&M $	0D,,T2++A|+C%,266'$2C2CK#% }}!"224!	#At}}q'89 F'2$.2ll1l.D+M8LLHL4"&";";Hm"TK(//<(+;;F44VVD6{TXX-$(	 00iH0Ow4N&1O"&"6"6"8K"(K(<%&/DOI$	0R 4==1#4MM9 # 	[R0"+.
 ll1l,8yyy**r"   c                 t    t        ||      \  }}| j                  ||      \  }}|j                  |      |fS )a  E step.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)

        Returns
        -------
        log_prob_norm : float
            Mean of the logarithms of the probabilities of each sample in X

        log_responsibility : array, shape (n_samples, n_components)
            Logarithm of the posterior probabilities (or responsibilities) of
            the point of each sample in X.
        rE   )r   _estimate_log_prob_respmean)r;   r@   rA   r\   r   r   s         r    ru   zBaseMixture._e_step:  sB      aB'A"&">">qR">"Hxww}%x//r"   c                      y)a*  M step.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)

        log_resp : array-like of shape (n_samples, n_components)
            Logarithm of the posterior probabilities (or responsibilities) of
            the point of each sample in X.
        Nr>   )r;   r@   r   s      r    rv   zBaseMixture._m_stepN  s     	r"   c                      y r:   r>   )r;   s    r    rt   zBaseMixture._get_parameters\      r"   c                      y r:   r>   )r;   paramss     r    r~   zBaseMixture._set_parameters`  r   r"   c                 l    t        |        t        | |d      }t        | j                  |      d      S )a  Compute the log-likelihood of each sample.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            List of n_features-dimensional data points. Each row
            corresponds to a single data point.

        Returns
        -------
        log_prob : array, shape (n_samples,)
            Log-likelihood of each sample in `X` under the current model.
        Fresetr%   rL   )r   r   r   _estimate_weighted_log_prob)r;   r@   s     r    score_sampleszBaseMixture.score_samplesd  s2     	$/$::1=AFFr"   c                 p    t        |      \  }}t        |j                  | j                  |                  S )a  Compute the per-sample average log-likelihood of the given data X.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_dimensions)
            List of n_features-dimensional data points. Each row
            corresponds to a single data point.

        y : Ignored
            Not used, present for API consistency by convention.

        Returns
        -------
        log_likelihood : float
            Log-likelihood of `X` under the Gaussian mixture model.
        )r   floatr   r   )r;   r@   rg   rA   r\   s        r    scorezBaseMixture.scorew  s1    " a ARWWT//2344r"   c                     t        |        t        |      \  }}t        | |d      }|j                  | j	                  |      d      S )a  Predict the labels for the data samples in X using trained model.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            List of n_features-dimensional data points. Each row
            corresponds to a single data point.

        Returns
        -------
        labels : array, shape (n_samples,)
            Component labels.
        Fr   r%   rL   )r   r   r   r   r   )r;   r@   rA   r\   s       r    predictzBaseMixture.predict  sF     	a A$/yy99!<1yEEr"   c                     t        |        t        | |d      }t        |      \  }}| j                  ||      \  }}|j	                  |      S )a  Evaluate the components' density for each sample.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            List of n_features-dimensional data points. Each row
            corresponds to a single data point.

        Returns
        -------
        resp : array, shape (n_samples, n_components)
            Density of each Gaussian component for each sample in X.
        Fr   rE   )r   r   r   r   exp)r;   r@   rA   r\   r   s        r    predict_probazBaseMixture.predict_proba  sP     	$/a A2212<8vvhr"   c                    t        |        t        | j                        \  }}}|dk  rt        d| j                  z        | j                  j
                  \  }}t        | j                        }|j                  |t        | j                  |            }| j                  dk(  rzt        j                  t        t        | j                  |      t        | j                  |      |      D 	
cg c]"  \  }}	}
|j!                  ||	t#        |
            $ c}
}	}      }n| j                  dk(  rut        j                  t        t        | j                  |      |      D 
cg c]5  \  }}
|j!                  |t        | j                  |      t#        |
            7 c}
}      }nt        j                  t        t        | j                  |      t        | j                  |      |      D 	
cg c]3  \  }}	}
||j%                  |
|f      t        j&                  |	      z  z   5 c}
}	}      }|j)                  t+        t-        |            D cg c]-  }|j/                  t#        ||         ||j0                  |      / c}      }t3        ||      }|j5                  |||      |fS c c}
}	}w c c}
}w c c}
}	}w c c}w )ay  Generate random samples from the fitted Gaussian distribution.

        Parameters
        ----------
        n_samples : int, default=1
            Number of samples to generate.

        Returns
        -------
        X : array, shape (n_samples, n_features)
            Randomly generated sample.

        y : array, shape (nsamples,)
            Component labels.
        r%   zNInvalid value for 'n_samples': %d . The sampling requires at least one sample.fulltiedrH   rJ   )rA   rK   )r   r   means_r   r0   r   r   r,   multinomialr   weights_covariance_typerO   vstackzipcovariances_multivariate_normalintstandard_normalsqrtconcatrq   lenr   int64r   rU   )r;   r]   rA   r\   device_
n_featuresrngn_samples_compr   
covariancesampler@   irg   max_float_dtypes                  r    r   zBaseMixture.sample  s     	1$++>Awq=$'+'8'8: 
 )): !2!23(;
 6)		 7:)$++r:)$*;*;R@&7 2z6 ++D*c&kJ	A !!V+		
 +.)$++r:N+	 'v ++/0A0A2FF	A 		
 7:)$++r:)$*;*;R@&7		 	 3z6 ))
/C)Dggj)**	A II s>23 N1-.'R
 57Kzz!?7zCQFFU	s   -'J) :J0
+8J62J=c                 N    | j                  ||      | j                  |      z   S )a  Estimate the weighted log-probabilities, log P(X | Z) + log weights.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)

        Returns
        -------
        weighted_log_prob : array, shape (n_samples, n_component)
        rE   )_estimate_log_prob_estimate_log_weightsr?   s      r    r   z'BaseMixture._estimate_weighted_log_prob  s.     &&qR&043M3MQS3M3TTTr"   c                      y)zEstimate log-weights in EM algorithm, E[ log pi ] in VB algorithm.

        Returns
        -------
        log_weight : array, shape (n_components, )
        Nr>   )r;   rA   s     r    r   z!BaseMixture._estimate_log_weights  rC   r"   c                      y)a9  Estimate the log-probabilities log P(X | Z).

        Compute the log-probabilities per each component for each sample.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)

        Returns
        -------
        log_prob : array, shape (n_samples, n_component)
        Nr>   r?   s      r    r   zBaseMixture._estimate_log_prob  s     	r"   c                 "   t        ||      \  }}| j                  ||      }t        |d|      }t        |      rt	        j
                  d      n	t               }|5  ||dd|j                  f   z
  }ddd       ||fS # 1 sw Y   |fS xY w)a@  Estimate log probabilities and responsibilities for each sample.

        Compute the log probabilities, weighted log probabilities per
        component and responsibilities for each sample in X with respect to
        the current state of the model.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)

        Returns
        -------
        log_prob_norm : array, shape (n_samples,)
            log p(X)

        log_responsibilities : array, shape (n_samples, n_components)
            logarithm of the responsibilities
        rE   r%   )rM   rA   ignore)underN)r   r   r   r   rO   errstater   rX   )r;   r@   rA   r\   weighted_log_probr   context_managerr   s           r    r   z#BaseMixture._estimate_log_prob_resp&  s    & aB'A <<Q2<F"#41D ,?r+BBKKh' 	  	H(=BJJ+GGH	H h&&	H h&&s   BBc                     | j                   dk(  rt        d|z         y| j                   dk\  r/t        d|z         t               | _        | j                  | _        yy)(Print verbose message on initialization.r%   zInitialization %drj   N)r.   printr   _init_prev_time_iter_prev_time)r;   r4   s     r    rr   z'BaseMixture._print_verbose_msg_init_begF  sS    <<1%./\\Q%./#'6D #'#7#7D  r"   c                     || j                   z  dk(  r^| j                  dk(  rt        d|z         y| j                  dk\  r0t               }t        d||| j                  z
  |fz         || _        yyy)r   r   r%   z  Iteration %drj   z0  Iteration %d	 time lapse %.5fs	 ll change %.5fN)r7   r.   r   r   r   )r;   r   diff_llcur_times       r    ry   z'BaseMixture._print_verbose_msg_iter_endO  s|    D)))Q.||q &/0"6Hx$*>*>>HI (0$ # /r"   c           	          |rdnd}| j                   dk(  rt        d| d       y
| j                   dk\  r/t               | j                  z
  }t        d| d|dd	|dd       y
y
)z.Print verbose message on the end of iteration.r   zdid not converger%   zInitialization .rj   z. time lapse z.5fzs	 lower bound N)r.   r   r   r   )r;   lbinit_has_convergedconverged_msgts        r    r{   z'BaseMixture._print_verbose_msg_init_end\  sw    '9?Q<<1OM?!45\\Q---A!-aW Es81 r"   r:   )r%   )#__name__
__module____qualname____doc__r   r   r   r   r8   dict__annotations__r<   r   rB   rc   r[   rR   r   rf   ru   rv   rt   r~   r   r   r   r   r   r   r   r   r   rr   ry   r{   r>   r"   r    r$   r$   1   s    "(AtFCDsD89tS$v>?h4?@Haf=>LM
 (( k;%h4GH$D 10  5"n 	 	< 5m+ 6m+^0(      G&5(F& (KGZU    '@80
r"   r$   )	metaclass)(r   r|   abcr   r   
contextlibr   numbersr   r   r   numpyrO   sklearnr	   sklearn.baser
   r   r   sklearn.clusterr   sklearn.exceptionsr   sklearn.utilsr   sklearn.utils._array_apir   r   r   r   r   r   sklearn.utils._param_validationr   r   sklearn.utils.validationr   r   r!   r$   r>   r"   r    <module>r      sX    $
  ' " "    B B + 1 ,  A C
$u, ur"   