
    ]h(                         S r SSKJr  SSKJrJrJrJr  SSKr	SSK
rSSKJrJr  SSKJrJr  SSKJr  SS	KJr   " S
 S5      rg)z0Container for the result of running optimization    )OrderedDict)DictOptionalTupleUnionN)MethodOptimizeArgs)
get_loggerscan_optimize_csv   )InferenceMetadata)RunSetc                   D   \ rS rSrSrS\SS4S jrS\4S jrS\S\	\
R                  \4   4S	 jrS
\SS4S jr\S\\S4   4S j5       r\S\4S j5       r\S\
R                  4S j5       r\S\\
R                     4S j5       r\S\R0                  4S j5       r\S\\R0                     4S j5       r\S\\\
R8                  4   4S j5       rSSS.S\S\S\S\	\
R                  \4   4S jjr SS\S\\\	\
R                  \4   4   4S jjr S S\\   SS4S jjr!Sr"g)!
CmdStanMLE   z\
Container for outputs from CmdStan optimization.
Created by :meth:`CmdStanModel.optimize`.
runsetreturnNc                    UR                   [        R                  :X  d$  [        SR	                  UR                   5      5      eXl        UR                  5       U l        U R
                  R                  R                  n[        U[        5      (       d   eUR                  U l        U R                  UR                  S   5        g)zInitialize object.z?Wrong runset method, expecting optimize runset, found method {}r   N)methodr   OPTIMIZE
ValueErrorformatr   _check_retcodes	converged_argsmethod_args
isinstancer	   save_iterations_save_iterations_set_mle_attrs	csv_files)selfr   optimize_argss      hC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\cmdstanpy/stanfit/mle.py__init__CmdStanMLE.__init__   s    }}/""(&"7  //1))55<
 
 	
 
 '4&C&CF,,Q/0    c                    SR                  U R                  R                  U R                  R                  R                  R                  S/ S95      nSR                  USR                  U R                  R                  5      SR                  U R                  R                  5      5      nU R                  (       d"  SR                  U5      nSR                  U5      nU$ )NzCmdStanMLE: model={}{}r   )cmdz#{}
 csv_file:
	{}
 output_file:
	{}z
	z{}
 Warning: invalid estimate, z#{} optimization failed to converge.)
r   r   modelr   r   composejoinr!   stdout_filesr   )r"   reprs     r$   __repr__CmdStanMLE.__repr__'   s    '..KKt{{00<<DDQBDO
 ;AAKK--.KK001

 ~~5<<TBD8??EDr'   attrc                     UR                  S5      (       a  [        SU 35      e U R                  U5      $ ! [         a  n[        UR                  6 eSnAff = f)z)Synonymous with ``fit.stan_variable(attr)_zUnknown variable name N)
startswithAttributeErrorstan_variabler   args)r"   r1   es      r$   __getattr__CmdStanMLE.__getattr__5   sY    ??3 #9$!@AA	*%%d++ 	* !&&))	*s   7 
AAAsample_csv_0c                     [        XR                  5      n[        U5      U l        US   U l        US   U l        U R                  (       a  US   U l        g g )Ncolumn_namesmle	all_iters)r   r   r   	_metadata_column_names_mle
_all_iters)r"   r;   metas      r$   r    CmdStanMLE._set_mle_attrs?   sP     /D/DE*40.2>.B $U	  *.{*;DO !r'   .c                     U R                   $ )z
Names of estimated quantities, includes joint log probability,
and all parameters, transformed parameters, and generated quantities.
)rA   r"   s    r$   r=   CmdStanMLE.column_namesG   s     !!!r'   c                     U R                   $ )z
Returns object which contains CmdStan configuration as well as
information about the names and structure of the inference method
and model output variables.
)r@   rG   s    r$   metadataCmdStanMLE.metadataO   s     ~~r'   c                 n    U R                   (       d  [        5       R                  S5        U R                  $ )z
Returns all final estimates from the optimizer as a numpy.ndarray
which contains all optimizer outputs, i.e., the value for `lp__`
as well as all Stan program variables.
2Invalid estimate, optimization failed to converge.)r   r
   warningrB   rG   s    r$   optimized_params_npCmdStanMLE.optimized_params_npX   s*     ~~L  D yyr'   c                     U R                   (       d  [        5       R                  S5        gU R                  (       d  [        5       R                  S5        U R                  $ )z
Returns all saved iterations from the optimizer and final estimate
as a numpy.ndarray which contains all optimizer outputs, i.e.,
the value for `lp__` as well as all Stan program variables.

lIntermediate iterations not saved to CSV output file. Rerun the optimize method with "save_iterations=True".NrM   )r   r
   rN   r   rC   rG   s    r$   optimized_iterations_np"CmdStanMLE.optimized_iterations_npe   sN     $$L  I ~~L  D r'   c                     U R                   R                  5       (       d  [        5       R                  S5        [        R
                  " U R                  /U R                  S9$ )z
Returns all final estimates from the optimizer as a pandas.DataFrame
which contains all optimizer outputs, i.e., the value for `lp__`
as well as all Stan program variables.
rM   columns)r   r   r
   rN   pd	DataFramerB   r=   rG   s    r$   optimized_params_pdCmdStanMLE.optimized_params_pdy   sI     {{**,,L  D ||TYYK1B1BCCr'   c                     U R                   (       d  [        5       R                  S5        gU R                  (       d  [        5       R                  S5        [        R
                  " U R                  U R                  S9$ )z
Returns all saved iterations from the optimizer and final estimate
as a pandas.DataFrame which contains all optimizer outputs, i.e.,
the value for `lp__` as well as all Stan program variables.

rR   NrM   rV   )r   r
   rN   r   rX   rY   rC   r=   rG   s    r$   optimized_iterations_pd"CmdStanMLE.optimized_iterations_pd   s_     $$L  I ~~L  D ||DOOT5F5FGGr'   c                     U R                   R                  5       (       d  [        5       R                  S5        [	        [        U R                  U R                  5      5      $ )z{
Returns all estimates from the optimizer, including `lp__` as a
Python Dict.  Only returns estimate from final iteration.
rM   )r   r   r
   rN   r   zipr=   rB   rG   s    r$   optimized_params_dict CmdStanMLE.optimized_params_dict   sH     {{**,,L  D 3t00$))<==r'   FTinc_iterationswarnvarrd   re   c                x   XR                   R                  ;  a5  [        SU S3SR                  U R                   R                  5      -   5      eU(       a1  U(       a*  U R                  (       d  [        5       R                  S5        U(       a8  U R                  R                  5       (       d  [        5       R                  S5        U(       a  U R                  (       a  U R                  nOU R                  n U R                   R                  U   R                  U5      nUR                  S:X  d  UR                  S:X  a)  [        5       R                  S5        UR                  5       $ U$ ! [         aD    [        SU S3SR                  U R                   R                  R                  5       5      -   5      ef = f)	a  
Return a numpy.ndarray which contains the estimates for the
for the named Stan program variable where the dimensions of the
numpy.ndarray match the shape of the Stan program variable.

This functionaltiy is also available via a shortcut using ``.`` -
writing ``fit.a`` is a synonym for ``fit.stan_variable("a")``

:param var: variable name

:param inc_iterations: When ``True`` and the intermediate estimates
    are included in the output, i.e., the optimizer was run with
    ``save_iterations=True``, then intermediate estimates are included.
    Default value is ``False``.

See Also
--------
CmdStanMLE.stan_variables
CmdStanMCMC.stan_variable
CmdStanPathfinder.stan_variable
CmdStanVB.stan_variable
CmdStanGQ.stan_variable
CmdStanLaplace.stan_variable
zUnknown variable name: z
Available variables are z, rR   rM    )r   zThe default behavior of CmdStanMLE.stan_variable() will change in a future release to always return a numpy.ndarray, even for scalar variables.)r@   	stan_varsr   r,   r   r
   rN   r   r   rC   rB   extract_reshapeshapeitemKeyErrorkeys)r"   rf   rd   re   dataouts         r$   r6   CmdStanMLE.stan_variable   su   > nn...)# /+ +-1YYt~~7O7O-PQ  N4+@+@L  I 3355L  D d33??D99D	"nn66s;KKC yyB#))t"3$$@
 xxz!J 	)# /+ +))DNN4499;<= 	s   8A0E+ )E+ +AF9c                     U R                   R                  5       (       d  [        5       R                  S5        0 nU R                  R
                   H  nU R                  X1SS9X#'   M     U$ )a$  
Return a dictionary mapping Stan program variables names
to the corresponding numpy.ndarray containing the inferred values.

:param inc_iterations: When ``True`` and the intermediate estimates
    are included in the output, i.e., the optimizer was run with
    ``save_iterations=True``, then intermediate estimates are included.
    Default value is ``False``.


See Also
--------
CmdStanMLE.stan_variable
CmdStanMCMC.stan_variables
CmdStanPathfinder.stan_variables
CmdStanVB.stan_variables
CmdStanGQ.stan_variables
CmdStanLaplace.stan_variables
rM   Frc   )r   r   r
   rN   r@   ri   r6   )r"   rd   resultnames       r$   stan_variablesCmdStanMLE.stan_variables   sh    , {{**,,L  D NN,,D--% . FL - r'   dirc                 :    U R                   R                  U5        g)a/  
Move output CSV files to specified directory.  If files were
written to the temporary session directory, clean filename.
E.g., save 'bernoulli-201912081451-1-5nm6as7u.csv' as
'bernoulli-201912081451-1.csv'.

:param dir: directory path

See Also
--------
stanfit.RunSet.save_csvfiles
cmdstanpy.from_csv
N)r   save_csvfiles)r"   rw   s     r$   ry   CmdStanMLE.save_csvfiles  s     	!!#&r'   )rC   rA   r@   rB   r   r   r   )F)N)#__name__
__module____qualname____firstlineno____doc__r   r%   strr/   r   npndarrayfloatr9   r    propertyr   r=   r   rJ   rO   r   rS   rX   rY   rZ   r]   r   float64ra   boolr6   ru   ry   __static_attributes__rh   r'   r$   r   r      s   
1v 1$ 1"# * *bjj%.?(@ *<3 <4 < "eCHo " " +   
RZZ 
 
 "**)=  & 
DR\\ 
D 
D H",,)? H H& 	>tCO'< 	> 	>  %EE 	E
 E 
rzz5 	!EP &+"	c5U*++	,B'# '$ ' 'r'   r   )r   collectionsr   typingr   r   r   r   numpyr   pandasrX   cmdstanpy.cmdstan_argsr   r	   cmdstanpy.utilsr
   r   rJ   r   r   r   r   rh   r'   r$   <module>r      s/    6 # / /   7 9 ' L' L'r'   