
    ]h6!                         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  SSKJr  SSKJr  SS	KJr  SS
KJr   " S S5      rg)zCContainer for the results of running autodiff variational inference    )OrderedDict)DictOptionalTupleUnionN)Method)scan_variational_csv)
get_logger   )InferenceMetadata)RunSetc            	       p   \ 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\4S j5       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\\\
R                  4   4S j5       r\S\4S j5       rSS.S\S\\   S\	\
R                  \4   4S jjrSS.S\\   S\\\	\
R                  \4   4   4S jjr \S\
R                  4S j5       r!\S\R.                  4S j5       r"SS\\   SS4S jjr#Sr$g)	CmdStanVB   zb
Container for outputs from CmdStan variational run.
Created by :meth:`CmdStanModel.variational`.
runsetreturnNc                     UR                   [        R                  :X  d$  [        SR	                  UR                   5      5      eXl        U R                  UR                  S   5        g)zInitialize object.zEWrong runset method, expecting variational inference, found method {}r   N)methodr   VARIATIONAL
ValueErrorformatr   _set_variational_attrs	csv_files)selfr   s     gC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\cmdstanpy/stanfit/vb.py__init__CmdStanVB.__init__   sT    }} 2 22""(&"7  ##F$4$4Q$78    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$ )NzCmdStanVB: model={}{}r   )cmdz#{}
 csv_file:
	{}
 output_file:
	{}z
	)	r   r   model_argsmethod_argscomposejoinr   stdout_files)r   reprs     r   __repr__CmdStanVB.__repr__!   s    &--KKt{{00<<DDQBDO
 ;AAKK--.KK001
 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   r*   es      r   __getattr__CmdStanVB.__getattr__-   sY    ??3 #9$!@AA	*%%d++ 	* !&&))	*s   7 
AAAsample_csv_0c                     [        U5      n[        U5      U l        US   U l        US   U l        US   U l        US   U l        g )Ncolumn_namesetavariational_meanvariational_sample)r	   r   	_metadata_column_names_eta_variational_mean_variational_sample)r   r4   metas      r   r    CmdStanVB._set_variational_attrs7   sL    #L1*40.2>.B;	-12D-E/34H/I r   c                 ,    [        U R                  5      $ )z
Total number of information items returned by sampler.
Includes approximation information and names of model parameters
and computed quantities.
)lenr;   r   s    r   columnsCmdStanVB.columns@   s     4%%&&r   .c                     U R                   $ )z
Names of information items returned by sampler for each draw.
Includes approximation information and names of model parameters
and computed quantities.
)r;   rC   s    r   r6   CmdStanVB.column_namesI   s     !!!r   c                     U R                   $ )z#
Step size scaling parameter 'eta'
)r<   rC   s    r   r7   CmdStanVB.etaR   s    
 yyr   c                     U R                   $ )z2
Returns inferred parameter means as numpy array.
)r=   rC   s    r   variational_params_npCmdStanVB.variational_params_npY   s    
 %%%r   c                 V    [         R                  " U R                  /U R                  S9$ )z7
Returns inferred parameter means as pandas DataFrame.
rD   )pd	DataFramer=   r6   rC   s    r   variational_params_pdCmdStanVB.variational_params_pd`   s$    
 ||T334d>O>OPPr   c                 T    [        [        U R                  U R                  5      5      $ )z)Returns inferred parameter means as Dict.)r   zipr6   r=   rC   s    r   variational_params_dict!CmdStanVB.variational_params_dictg   s"     3t00$2H2HIJJr   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:   rC   s    r   metadataCmdStanVB.metadatal   s     ~~r   meanvarr[   c                   Uc  [        5       R                  S5        SnU(       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  a0  U(       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, with
a leading axis added for the number of draws from the variational
approximation.

* If the variable is a scalar variable, the return array has shape
  ( draws, ).
* If the variable is a vector, the return array has shape
  ( draws, len(vector))
* If the variable is a matrix, the return array has shape
  ( draws, size(dim 1), size(dim 2) )
* If the variable is an array with N dimensions, the return array
  has shape ( draws, size(dim 1), ..., size(dim N))

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 mean: if True, return the variational mean. Otherwise,
    return the variational sample.  The default behavior will
    change in a future release to return the variational sample.

See Also
--------
CmdStanVB.stan_variables
CmdStanMCMC.stan_variable
CmdStanMLE.stan_variable
CmdStanPathfinder.stan_variable
CmdStanGQ.stan_variable
CmdStanLaplace.stan_variable
zThe default behavior of CmdStanVB.stan_variable() will change in a future release to return the variational sample, rather than the mean.
To maintain the current behavior, pass the argument mean=TrueT )r   zThe default behavior of CmdStanVB.stan_variable(mean=True) will change in a future release to always return a numpy.ndarray, even for scalar variables.zUnknown variable name: z
Available variables are z, )r
   warningr=   r>   r:   	stan_varsextract_reshapeshapeitemKeyErrorr   r%   keys)r   r\   r[   drawsouts        r   r/   CmdStanVB.stan_variableu   s    L <L   D**E,,E	"nn66s;KKC yyB#))t"3L((0 xxz!J 	)# /+ +))DNN4499;<= 	s    A7B: 8B: :ADc                f    0 nU R                   R                   H  nU R                  X1S9X#'   M     U$ )a5  
Return a dictionary mapping Stan program variables names
to the corresponding numpy.ndarray containing the inferred values.

See Also
--------
CmdStanVB.stan_variable
CmdStanMCMC.stan_variables
CmdStanMLE.stan_variables
CmdStanGQ.stan_variables
CmdStanPathfinder.stan_variables
CmdStanLaplace.stan_variables
rZ   )r:   r`   r/   )r   r[   resultnames       r   stan_variablesCmdStanVB.stan_variables   s7      NN,,D--d->FL -r   c                     U R                   $ )z6Returns the set of approximate posterior output draws.)r>   rC   s    r   r9   CmdStanVB.variational_sample   s     '''r   c                 T    [         R                  " U R                  U R                  S9$ )zN
Returns the set of approximate posterior output draws as
a pandas DataFrame.
rN   )rO   rP   r>   r6   rC   s    r   variational_sample_pdCmdStanVB.variational_sample_pd   s!     ||D44d>O>OPPr   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   rs   s     r   ru   CmdStanVB.save_csvfiles   s     	!!#&r   )r;   r<   r:   r=   r>   r   )N)%__name__
__module____qualname____firstlineno____doc__r   r   strr(   r   npndarrayfloatr2   r   propertyintrD   r   r6   r7   rK   rO   rP   rQ   r   rU   r   rX   r   boolr/   rl   r9   rq   ru   __static_attributes__r^   r   r   r   r      s
   
9v 9$ 9
# 
* *bjj%.?(@ *J3 J4 J ' ' ' "eCHo " " U   &rzz & & Qr|| Q Q Kc2::o)> K K +   37II!)$I	rzz5 	!IX )-~	c5U*++	,* (BJJ ( ( Qr|| Q Q'# '$ ' 'r   r   )r{   collectionsr   typingr   r   r   r   numpyr}   pandasrO   cmdstanpy.cmdstan_argsr   cmdstanpy.utilsr	   cmdstanpy.utils.loggingr
   rX   r   r   r   r   r^   r   r   <module>r      s2    I # / /   ) 0 . ' _' _'r   