
    Q-j                    J   d dl mZ d dlZd dlmZ d dlmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ erd d
lmZ d dlmZ dZ e	j,                  ddd      Z G d dej0                        Z ed       G d de             ZddZ ed       G d de             Zy)    )annotationsN)cast)TYPE_CHECKING)_DEPRECATION_WARNING_TEMPLATE)experimental_class)optuna_warn)StudyDirection)
TrialState)FrozenTrial)Trialzterminator:cv_scoresz`optuna.terminator` modulez4.9.0z6.0.0)named_verr_verc                  B    e Zd ZdZej
                  	 	 	 	 	 	 dd       Zy)BaseErrorEvaluatorz Base class for error evaluators.c                     y N selftrialsstudy_directions      i/Users/jameslopez/projects/TradingBot25/.venv/lib/python3.12/site-packages/optuna/terminator/erroreval.pyevaluatezBaseErrorEvaluator.evaluate    s     	    Nr   zlist[FrozenTrial]r   r	   returnfloat)__name__
__module____qualname____doc__abcabstractmethodr   r   r   r   r   r      s9    *! ( 
	 r   r   )	metaclassz3.2.0c                  $    e Zd ZdZ	 	 	 	 	 	 ddZy)CrossValidationErrorEvaluatora  An error evaluator for objective functions based on cross-validation.

    This evaluator evaluates the objective function's statistical error, which comes from the
    randomness of dataset. This evaluator assumes that the objective function is the average of
    the cross-validation and uses the scaled variance of the cross-validation scores in the best
    trial at the moment as the statistical error.

    c                   |D cg c]"  }|j                   t        j                  k(  s!|$ }}t        |      dkD  sJ |t        j
                  k(  rt        |d       }nt        |d       }|j                  }t        |v r
|t           }nt        d      t        |      }|dkD  sJ d       d|z  d|dz
  z  z   }|t        j                  |      z  }	t        j                  |	      }
t        |
      S c c}w )a  Evaluate the statistical error of the objective function based on cross-validation.

        Args:
            trials:
                A list of trials to consider. The best trial in ``trials`` is used to compute the
                statistical error.

            study_direction:
                The direction of the study.

        Returns:
            A float representing the statistical error of the objective function.

        r   c                .    t        d| j                        S Nr   r   valuets    r   <lambda>z8CrossValidationErrorEvaluator.evaluate.<locals>.<lambda>K       43Ir   )keyc                .    t        d| j                        S r*   r+   r-   s    r   r/   z8CrossValidationErrorEvaluator.evaluate.<locals>.<lambda>M   r0   r   zCross-validation scores have not been reported. Please call `report_cross_validation_scores(trial, scores)` during a trial and pass the list of scores as `scores`.   z9Should be guaranteed by `report_cross_validation_scores`.)stater
   COMPLETElenr	   MAXIMIZEmaxminsystem_attrs_CROSS_VALIDATION_SCORES_KEY
ValueErrornpvarsqrtr   )r   r   r   trial
best_trialbest_trial_attrs	cv_scoreskscaler>   stds              r   r   z&CrossValidationErrorEvaluator.evaluate4   s    & &,RVEu{{j>Q>Q/Q%VR6{Qn555V)IJJV)IJJ%22'+;;()EFI.  	N1uQQQuAQU#bffY''ggclSz3 Ss
   "C4C4Nr   )r   r    r!   r"   r   r   r   r   r'   r'   )   s(    ,!, (, 
	,r   r'   c                    t        t        t               t        |      dk  rt	        d      | j
                  j                  | j                  t        |       y)a  A function to report cross-validation scores of a trial.

    This function should be called within the objective function to report the cross-validation
    scores. The reported scores are used to evaluate the statistical error for termination
    judgement.

    Args:
        trial:
            A :class:`~optuna.trial.Trial` object to report the cross-validation scores.
        scores:
            The cross-validation scores of the trial.

    r3   z:The length of `scores` is expected to be greater than one.N)	r   _DEPRECATION_WARNING_MESSAGEFutureWarningr6   r<   storageset_trial_system_attr	_trial_idr;   )r@   scoress     r   report_cross_validation_scoresrN   c   sC     ,m<
6{aUVV	MM''9UW]^r   c                  ,    e Zd ZdZddZ	 	 	 	 	 	 ddZy)StaticErrorEvaluatora7  An error evaluator that always returns a constant value.

    This evaluator can be used to terminate the optimization when the evaluated improvement
    potential is below the fixed threshold.

    Args:
        constant:
            A user-specified constant value to always return as an error estimate.

    c                    || _         y r   	_constant)r   constants     r   __init__zStaticErrorEvaluator.__init__   s	    !r   c                    | j                   S r   rR   r   s      r   r   zStaticErrorEvaluator.evaluate   s    
 ~~r   N)rT   r   r   Noner   )r   r    r!   r"   rU   r   r   r   r   rP   rP   x   s-    	"! ( 
	r   rP   )r@   r   rM   zlist[float]r   rW   )
__future__r   r#   typingr   r   numpyr=   optuna._deprecatedr   optuna._experimentalr   optuna._warningsr   optuna.studyr	   optuna.trial._stater
   optuna.trialr   r   r;   formatrH   ABCMetar   r'   rN   rP   r   r   r   <module>rc      s    " 
     < 3 ( ' * ("  6 C<CC	%

  	3;; 	 G6$6 6 6r_* G-  r   