
    M/Ph1                     8    d Z ddlZddlmZ  G d d          ZdS )aO  Generate a random process with panel structure

Created on Sat Dec 17 22:15:27 2011

Author: Josef Perktold


Notes
-----
* written with unbalanced panels in mind, but not flexible enough yet
* need more shortcuts and options for balanced panel
* need to add random intercept or coefficients
* only one-way (repeated measures) so far

    N   )correlation_structuresc                   <    e Zd ZdZddej        dddfdZd Zd ZdS )	PanelSampleae  data generating process for panel with within correlation

    allows various within correlation structures, but no random intercept yet

    Parameters
    ----------
    nobs : int
        total number of observations
    k_vars : int
        number of explanatory variables to create in exog, including constant
    n_groups int
        number of groups in balanced sample
    exog : None or ndarray
        default is None, in which case a exog is created
    within : bool
        If True (default), then the exog vary within a group. If False, then
        only variation across groups is used.
        TODO: this option needs more work
    corr_structure : ndarray or ??
        Default is np.eye.
    corr_args : tuple
        arguments for the corr_structure
    scale : float
        scale of noise, standard deviation of normal distribution
    seed : None or int
        If seed is given, then this is used to create the random numbers for
        the sample.

    Notes
    -----
    The behavior for panel robust covariance estimators seems to differ by
    a large amount by whether exog have mostly within group or across group
    variation. I do not understand why this should be the case from the theory,
    and this would warrant more investigation.

    This is just used in one example so far and needs more usage to see what
    will be useful to add.

    NT r   c
                 n   ||z  }
|
|z  }|| _         |
| _        || _        || _        || _        t          j        t          j        |          |
          | _        t          j        |dz             |
z  | _	        |v|r*t          j
        t          j        dd|
          |          }n)t          j        t          j        dd|
          |          }|d d d f         t          j        |          z  }|| _        d | _        d | _        |	 t
          j                            dd          }	|	| _        t
          j                            |	          | _        |t          j        |
          z  | _         | j        |
g|R  }t-          j        || j                  | _        t          j        |          | _        d S )Nr   r      i?B )nobsnobs_in_groupsk_varscorr_structurenprepeatarangegroupsgroup_indicestilelinspaceexogy_truebetarandomrandintseedRandomStaterandom_stateonesstdcscorr2covcovzerosgroup_means)selfr   r   r   r   withinr   	corr_argsscaler   r   tcorrs                f/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/panel/random_panel.py__init__zPanelSample.__init__>   s    x 	 ,i	( 3 3V<<Yxz22V;< 	BGBK1f55x@@ Ibk"Qv66AAQQQtV9bi///D	 	<9$$Q//D	I11$77 276??*"t"66I666;tTX..8H--    c                     | j         !| j                            d          | _        d S t	          j        | j        | j                   | _        d S )Nr   )r   r   sumr   r   dot)r&   s    r,   
get_y_truezPanelSample.get_y_trueo   s>    9)--**DKKK&DI66DKKKr.   c                    | j         }| j        |                                  | j        }| j        }d}|rd| j                             t          j        |          | j        |          	                                }|t          j
        | j        |          z  }nt          j        | j        t          j                  }|                    t          j                   t#          | j                  D ]c}| j        ||dz            \  }}| j        |         }	| j                             |	t          j        | j                  z  | j                  |||<   d| j        |z   }
|
S )zT
        generate endog for a random panel dataset with within correlation

        NT)sizer	   )r   r   r2   r   r   multivariate_normalr   r$   r#   ravelr   r%   emptyr   float64fillnanranger   r   )r&   r   r   r   use_balancednoiseiiidxidxuppmean_iendogs              r,   generate_panelzPanelSample.generate_panelv   sW    ";OO= 	Q%99"(6:J:J26(7? : A AAF  RYt/888EEHTY
33EJJrvDM** Q Q"0BqD9V)"-$($5$I$I(.1E1E(Etx%Q %Qc&j!! e#r.   )	__name__
__module____qualname____doc__r   eyer-   r2   rC   r   r.   r,   r   r      sd        & &P 59 ""AD.. .. .. ..b7 7 7! ! ! ! !r.   r   )rG   numpyr    r   r!   r   r   r.   r,   <module>rK      sk          * * * * * *B B B B B B B B B Br.   