### Run all MICE steps and obtain results Source: https://www.statsmodels.org/stable/_modules/statsmodels/imputation/mice.html This example demonstrates how to perform all MICE imputation steps and obtain the final analysis results. It requires prior setup of the MICE model. ```python import statsmodels.api as sm from statsmodels.imputation import mice # Assume data and model_formula are defined # Example: data = sm.datasets.get_rdataset("Guerry", "spatial") # Example: model_formula = "Lottery ~ Literacy + np.log(Pop1831)" # Initialize MICE # model = mice.MICE(model_formula, sm.OLS, data.data) # Fit the model with MICE # results = model.fit(n_burnin=5, n_imputations=5) # Print summary of results # print(results.summary()) ``` -------------------------------- ### Example Usage Source: https://www.statsmodels.org/stable/generated/statsmodels.nonparametric.kernel_density.KDEMultivariate.html Example of initializing KDEMultivariate for bivariate data and accessing the computed bandwidth. ```APIDOC ## Example ```python import statsmodels.api as sm import numpy as np nobs = 300 np.random.seed(1234) # Seed random generator c1 = np.random.normal(size=(nobs,1)) c2 = np.random.normal(2, 1, size=(nobs,1)) # Estimate a bivariate distribution and display the bandwidth found: dens_u = sm.nonparametric.KDEMultivariate(data=[c1,c2], var_type='cc', bw='normal_reference') print(dens_u.bw) ``` ``` -------------------------------- ### start_params Property Source: https://www.statsmodels.org/stable/generated/statsmodels.tsa.statespace.dynamic_factor_mq.DynamicFactorMQ.html Gets the starting parameters for maximum likelihood estimation. ```APIDOC ## start_params ### Description Starting parameters for maximum likelihood estimation. ### Property `start_params` (array) ``` -------------------------------- ### Get Initial Parameters Source: https://www.statsmodels.org/stable/_modules/statsmodels/discrete/count_model.html Retrieves starting parameters for model fitting, particularly for zero-inflated models. It fits the main model first and then appends initial inflation parameters. ```python def _get_start_params(self): with warnings.catch_warnings(): warnings.simplefilter("ignore", category=ConvergenceWarning) start_params = self.model_main.fit(disp=0, method="nm").params start_params = np.append(np.ones(self.k_inflate) * 0.1, start_params) return start_params ``` -------------------------------- ### Getting Starting Values for Optimization Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/holtwinters/model.html Determines initial parameter values for optimization. Uses brute-force search for smoothing parameters if specified and no start_params are provided. Otherwise, uses provided start_params. ```python def _get_starting_values( self, params, start_params, use_brute, sel, hw_args, bounds, alpha, func, ): if start_params is None and use_brute and np.any(sel[:3]): # Have a quick look in the region for a good starting place for # alpha, beta & gamma using fixed values for initial m = self.seasonal_periods sv_sel = np.array([False] * (6 + m)) sv_sel[:3] = True sv_sel &= sel hw_args.xi = sv_sel.astype(np.int64) hw_args.transform = False # Setup the grid points, respecting constraints points = self._setup_brute(sv_sel, bounds, alpha) opt = opt_wrapper(func) best_val = np.inf best_params = points[0] for point in points: val = opt(point, hw_args) if val < best_val: best_params = point best_val = val params[sv_sel] = best_params elif start_params is not None: if len(start_params) != sel.sum(): msg = "start_params must have {0} values but has {1}." nxi, nsp = len(sel), len(start_params) raise ValueError(msg.format(nxi, nsp)) params[sel] = start_params return params ``` -------------------------------- ### WLS Model Initialization and Fitting Example Source: https://www.statsmodels.org/stable/_modules/statsmodels/regression/linear_model.html Demonstrates how to initialize and fit a Weighted Least Squares (WLS) model with custom weights. Includes examples of parameter estimation and hypothesis testing. ```python import statsmodels.api as sm Y = [1,3,4,5,2,3,4] X = range(1,8) X = sm.add_constant(X) wls_model = sm.WLS(Y,X, weights=list(range(1,8))) results = wls_model.fit() print(results.params) print(results.tvalues) print(results.t_test([1, 0])) print(results.f_test([0, 1])) ``` -------------------------------- ### Start Parameters Generation Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/exponential_smoothing/ets.html Generates the starting parameters in the external format. Includes default smoothing parameters and estimated initial states if applicable. Adapts seasonal values for well-posedness. ```python @property def _start_params(self): """ Default start params in the format of external parameters. This should not be called directly, but by calling ``self.start_params``. """ params = [] for p in self._smoothing_param_names: if p in self.param_names: params.append(self._default_start_params[p]) if self.initialization_method == "estimated": lvl_idx = len(params) params += [self.initial_level] if self.has_trend: params += [self.initial_trend] if self.has_seasonal: # we have to adapt the seasonal values a bit to make sure the # problem is well posed (see implementation notes above) initial_seasonal = self.initial_seasonal if self.seasonal == "mul": params[lvl_idx] *= initial_seasonal[-1] initial_seasonal /= initial_seasonal[-1] else: params[lvl_idx] += initial_seasonal[-1] initial_seasonal -= initial_seasonal[-1] params += initial_seasonal.tolist() return np.array(params) ``` -------------------------------- ### GEE Initialization and Starting Parameters Source: https://www.statsmodels.org/stable/_modules/statsmodels/genmod/generalized_estimating_equations.html Initializes a GEE model and computes starting parameters using a Binomial family model. This is useful for setting up complex GEE models. ```python super().__init__( endog, exog, groups, time, family, cov_struct, missing, offset, dep_data, constraint) def _starting_params(self): exposure = getattr(self, "exposure", None) model = GEE(self.endog, self.exog, self.groups, time=self.time, family=families.Binomial(), offset=self.offset, exposure=exposure) result = model.fit() return result.params ``` -------------------------------- ### Start Parameter Conversion and Bounding Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/exponential_smoothing/ets.html Converts start parameters to internal format, sets bounds for internal-only and fixed parameters, and ensures all start parameters are within their specified bounds. ```python def _convert_and_bound_start_params(self, params): """ This converts start params to internal params, sets internal-only parameters as bounded, sets bounds for fixed parameters, and then makes sure that all start parameters are within the specified bounds. """ internal_params = self._internal_params(params) # set bounds for missing and fixed for p in self._internal_param_names: ``` -------------------------------- ### BFGS Optimization Setup Source: https://www.statsmodels.org/stable/_modules/statsmodels/base/optimizer.html This code block demonstrates the setup and execution of the BFGS optimization algorithm. It handles default epsilon values and processes the output based on `full_output` and `retall` flags. ```python epsilon = kwargs.setdefault('epsilon', 1.4901161193847656e-08) retvals = optimize.fmin_bfgs(f, start_params, score, args=fargs, gtol=gtol, norm=norm, epsilon=epsilon, maxiter=maxiter, full_output=full_output, disp=disp, retall=retall, callback=callback) if full_output: if not retall: xopt, fopt, gopt, Hinv, fcalls, gcalls, warnflag = retvals else: (xopt, fopt, gopt, Hinv, fcalls, gcalls, warnflag, allvecs) = retvals converged = not warnflag retvals = {'fopt': fopt, 'gopt': gopt, 'Hinv': Hinv, 'fcalls': fcalls, 'gcalls': gcalls, 'warnflag': warnflag, 'converged': converged} if retall: retvals.update({'allvecs': allvecs}) else: xopt = retvals retvals = None return xopt, retvals ``` -------------------------------- ### Default Start Parameters Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/exponential_smoothing/ets.html Defines the default starting parameters for smoothing components (level, trend, seasonal) and trend damping. These are used when initial parameter estimation is not performed. ```python @property def _default_start_params(self): return { "smoothing_level": 0.1, "smoothing_trend": 0.01, "smoothing_seasonal": 0.01, "damping_trend": 0.98, } ``` -------------------------------- ### Estimate VARMAX Start Parameters Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/statespace/varmax.html Calculates initial starting parameters for the VARMAX model by performing a multivariate regression. This method handles missing data by interpolation and backward filling. ```python @property def start_params(self): params = np.zeros(self.k_params, dtype=np.float64) # A. Run a multivariate regression to get beta estimates endog = pd.DataFrame(self.endog.copy()) endog = endog.interpolate() endog = np.require(endog.bfill(), requirements="W") exog = None if self.k_trend > 0 and self.k_exog > 0: exog = np.c_[self._trend_data, self.exog] elif self.k_trend > 0: exog = self._trend_data elif self.k_exog > 0: exog = self.exog # Although the Kalman filter can deal with missing values in endog, # conditional sum of squares cannot if np.any(np.isnan(endog)): mask = ~np.any(np.isnan(endog), axis=1) ``` -------------------------------- ### Initialize KDEMultivariate with Normal Reference Bandwidth Source: https://www.statsmodels.org/stable/generated/statsmodels.nonparametric.kernel_density.KDEMultivariate.html This example demonstrates initializing a bivariate kernel density estimator with continuous variables and using the 'normal_reference' bandwidth selection rule. It then accesses and prints the calculated bandwidth. ```python import statsmodels.api as sm import numpy as np nobs = 300 np.random.seed(1234) # Seed random generator c1 = np.random.normal(size=(nobs,1)) c2 = np.random.normal(2, 1, size=(nobs,1)) dens_u = sm.nonparametric.KDEMultivariate(data=[c1,c2], var_type='cc', bw='normal_reference') dens_u.bw ``` -------------------------------- ### Get Initial Parameters for Null Model Source: https://www.statsmodels.org/stable/_modules/statsmodels/discrete/discrete_model.html Provides starting parameters for a null model (intercept only) for discrete distributions. It estimates an initial dispersion parameter. ```python @Appender(_get_start_params_null_docs) def _get_start_params_null(self): offset = getattr(self, "offset", 0) exposure = getattr(self, "exposure", 0) const = (self.endog / np.exp(offset + exposure)).mean() params = [np.log(const)] mu = const * np.exp(offset + exposure) resid = self.endog - mu a = self._estimate_dispersion(mu, resid, df_resid=resid.shape[0] - 1) params.append(a) return np.array(params) ``` -------------------------------- ### DynamicFactorMQ Initialization and Basic Usage Source: https://www.statsmodels.org/stable/generated/statsmodels.tsa.statespace.dynamic_factor_mq.DynamicFactorMQ.html Demonstrates initializing the DynamicFactorMQ model with default settings and printing its summary. ```APIDOC ## DynamicFactorMQ Initialization ### Description Initializes a Dynamic Factor Model with specified endogenous data and optional parameters. ### Parameters - **endog** (array_like): The observed time-series data. - **exog** (array_like, optional): Exogenous variables to include in the model. - **k_factors** (int, optional): The number of factors to include in the model. Defaults to 1. - **factor_order** (int, optional): The order of the autoregressive process for the factors. Defaults to 1. - **factor_multiplicities** (int, optional): The number of blocks of factors. Defaults to 1. - **factor_orders** (int or list, optional): The order of the autoregressive process for each factor block. Defaults to 1. - **idiosyncratic_ar_order** (int, optional): The order of the autoregressive process for the idiosyncratic errors. Defaults to 1. - **idiosyncratic_ar1** (bool, optional): Whether to model idiosyncratic errors with an AR(1) process. Defaults to True. - **idiosyncratic_multiplicities** (int, optional): The number of blocks of idiosyncratic errors. Defaults to 1. - **idiosyncratic_orders** (int or list, optional): The order of the autoregressive process for each idiosyncratic error block. Defaults to 1. - **error_cov_type** (str, optional): The type of covariance matrix for the errors. Options are 'diagonal' or 'unstructured'. Defaults to 'diagonal'. - **standardize** (bool, optional): Whether to standardize the observed variables. Defaults to True. - **enforce_stationarity** (bool, optional): Whether to enforce stationarity of the factors. Defaults to True. - **enforce_invertibility** (bool, optional): Whether to enforce invertibility of the idiosyncratic errors. Defaults to True. - **initialization** (str, optional): Method for initializing the state space representation. Defaults to 'approximate_diffuse'. - **loglikelihood_burn** (int, optional): Number of periods to burn for the log-likelihood calculation. Defaults to 0. - **endog_quarterly** (array_like, optional): Quarterly data to be used in a mixed-frequency model. ### Method ```python DynamicFactorMQ(endog, exog=None, k_factors=1, factor_order=1, factor_multiplicities=1, factor_orders=1, idiosyncratic_ar_order=1, idiosyncratic_ar1=True, idiosyncratic_multiplicities=1, idiosyncratic_orders=1, error_cov_type='diagonal', standardize=True, enforce_stationarity=True, enforce_invertibility=True, initialization='approximate_diffuse', loglikelihood_burn=0, endog_quarterly=None) ``` ### Example ```python import statsmodels.api as sm # Load sample data endog = sm.datasets.macrodata.load_pandas().data['infl'] # Initialize the model mod = sm.tsa.DynamicFactorMQ(endog) # Print the model summary print(mod.summary()) ``` ``` -------------------------------- ### Get Formatting Options Source: https://www.statsmodels.org/stable/_modules/statsmodels/iolib/table.html Retrieves and merges formatting options for a given output format. It starts with default formats and updates them with call-specific keyword arguments. ```python def _get_fmt(self, output_format, **fmt_dict): """Return dict, the formatting options. """ output_format = get_output_format(output_format) # first get the default formatting try: fmt = self.output_formats[output_format].copy() except KeyError: raise ValueError('Unknown format: %s' % output_format) # then, add formatting specific to this call fmt.update(fmt_dict) return fmt ``` -------------------------------- ### EstimatorSettings Example Usage Source: https://www.statsmodels.org/stable/_modules/statsmodels/nonparametric/_kernel_base.html Demonstrates how to create an EstimatorSettings object and pass it to KDEMultivariate for customized bandwidth estimation. ```python >>> settings = EstimatorSettings(randomize=True, n_jobs=3) >>> k_dens = KDEMultivariate(data, var_type, defaults=settings) ``` -------------------------------- ### Get Initial Parameters for GEE Source: https://www.statsmodels.org/stable/_modules/statsmodels/genmod/generalized_estimating_equations.html Determines starting parameters for the GEE fitting process by fitting a standard GLM model. This is used when initial parameters are not provided. ```python def _starting_params(self): if np.isscalar(self._offset_exposure): offset = None else: offset = self._offset_exposure model = GLM(self.endog, self.exog, family=self.family, offset=offset, freq_weights=self.weights) result = model.fit() return result.params ``` -------------------------------- ### Denton Method - Parameter Setup Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/interp/denton.html This snippet shows the initial setup for the Denton method, including checking and reshaping input arrays and determining the aggregation factor 'k' based on the specified frequency. It also handles the case for custom frequencies requiring a 'k' value. ```python indicator = asarray(indicator) if indicator.ndim == 1: indicator = indicator[:,None] benchmark = asarray(benchmark) if benchmark.ndim == 1: benchmark = benchmark[:,None] # get dimensions N = len(indicator) # total number of high-freq m = len(benchmark) # total number of low-freq # number of low-freq observations for aggregate measure # 4 for annual to quarter and 3 for quarter to monthly if freq == "aq": k = 4 elif freq == "qm": k = 3 elif freq == "other": k = kwargs.get("k") if not k: raise ValueError("k must be supplied with freq=\"other\"") else: raise ValueError("freq %s not understood" % freq) n = k*m # number of indicator series with a benchmark for back-series # if k*m != n, then we are going to extrapolate q observations if N > n: q = N - n else: q = 0 # make the aggregator matrix #B = block_diag(*(ones((k,1)),)*m) B = np.kron(np.eye(m), ones((k,1))) ``` -------------------------------- ### ZeroInflatedPoisson _get_start_params Source: https://www.statsmodels.org/stable/_modules/statsmodels/discrete/count_model.html Retrieves initial parameters for fitting a ZeroInflatedPoisson model. It uses a ZeroInflatedPoisson model to get starting parameters and appends a value for the zero-inflation parameter. ```python with warnings.catch_warnings(): warnings.simplefilter("ignore", category=ConvergenceWarning) start_params = ZeroInflatedPoisson(self.endog, self.exog, exog_infl=self.exog_infl).fit(disp=0).params start_params = np.append(start_params, 0.1) return start_params ``` -------------------------------- ### NonlinearIVGMM fitstart method Source: https://www.statsmodels.org/stable/_modules/statsmodels/sandbox/regression/gmm.html Provides initial starting values for the parameters. ```APIDOC ## fitstart(self) ### Description Provides initial starting values for the parameters. This might not be suitable for more general functions. ### Returns - array_like: An array of zeros with the shape of the number of explanatory variables. ``` -------------------------------- ### GLSAR Initialization and Basic Usage Source: https://www.statsmodels.org/stable/generated/statsmodels.regression.linear_model.GLSAR.html Demonstrates how to initialize the GLSAR model with data and an AR order, and then iteratively fit the model to estimate AR coefficients and model parameters. ```APIDOC ## GLSAR Initialization and Fitting ### Description Initializes the GLSAR model and demonstrates an iterative fitting process to estimate the autoregressive coefficients (rho) and model parameters. ### Parameters - **endog** (array_like) - The dependent variable. - **exog** (array_like) - The independent variables. An intercept should be added manually if needed. - **rho** (int or array_like) - The order of the autoregressive covariance or initial AR coefficients. - **missing** (str) - Method for handling missing values ('none', 'drop', 'raise'). - **hasconst** (None or bool) - Indicates if a constant is included in `exog`. ### Example ```python import statsmodels.api as sm import numpy as np X = range(1, 8) X = sm.add_constant(X) Y = [1, 3, 4, 5, 8, 10, 9] # Initialize with rho=2 (order of AR process) model = sm.GLSAR(Y, X, rho=2) # Iteratively fit the model res = model.iterative_fit(maxiter=6) # Access estimated AR coefficients print(model.rho) # Access regression results print(res.params) print(res.tvalues) ``` ``` -------------------------------- ### Get Initialization Keywords for Exponential Smoothing Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/statespace/exponential_smoothing.html Retrieves initialization keywords for exponential smoothing models, including seasonal periods. This is used internally for model setup and parameter handling. ```python def _get_init_kwds(self): kwds = super()._get_init_kwds() kwds['seasonal'] = self.seasonal_periods return kwds ``` -------------------------------- ### Calculate Starting Parameters Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/statespace/dynamic_factor.html Computes initial parameter estimates for the Dynamic Factor model. It uses PCA for factor loadings, OLS for exog coefficients, and VAR on PCA factors for factor transition parameters. ```python @property def start_params(self): params = np.zeros(self.k_params, dtype=np.float64) endog = self.endog.copy() mask = ~np.any(np.isnan(endog), axis=1) endog = endog[mask] if self.k_exog > 0: exog = self.exog[mask] # 1. Factor loadings (estimated via PCA) if self.k_factors > 0: # Use principal components + OLS as starting values res_pca = PCA(endog, ncomp=self.k_factors) mod_ols = OLS(endog, res_pca.factors) res_ols = mod_ols.fit() # Using OLS params for the loadings tends to gives higher starting # log-likelihood. params[self._params_loadings] = res_ols.params.T.ravel() # params[self._params_loadings] = res_pca.loadings.ravel() # However, using res_ols.resid tends to causes non-invertible # starting VAR coefficients for error VARs # endog = res_ols.resid endog = endog - np.dot(res_pca.factors, res_pca.loadings.T) # 2. Exog (OLS on residuals) if self.k_exog > 0: mod_ols = OLS(endog, exog=exog) res_ols = mod_ols.fit() # In the form: beta.x1.y1, beta.x2.y1, beta.x1.y2, ... params[self._params_exog] = res_ols.params.T.ravel() endog = res_ols.resid # 3. Factors (VAR on res_pca.factors) stationary = True if self.k_factors > 1 and self.factor_order > 0: # 3a. VAR transition (OLS on factors estimated via PCA) mod_factors = VAR(res_pca.factors) res_factors = mod_factors.fit(maxlags=self.factor_order, ic=None, trend='n') # Save the parameters params[self._params_factor_transition] = ( ``` -------------------------------- ### Get STL Seasonal Predictions Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/forecasting/stl.html Retrieves in-sample and out-of-sample seasonal predictions from an STL decomposition. Handles date parsing for start and end points, and dynamic forecasting logic. ```python data = PandasData(pd.Series(self._endog), index=self._index) if start is None: start = 0 (start, end, out_of_sample, prediction_index) = get_prediction_index( start, end, self._nobs, self._index, data=data ) if isinstance(dynamic, (str, dt.datetime, pd.Timestamp)): dynamic, _, _ = get_index_loc(dynamic, self._index) dynamic = dynamic - start elif dynamic is True: dynamic = 0 elif dynamic is False: # If `dynamic=False`, then no dynamic predictions dynamic = None nobs = self._nobs dynamic, _ = _check_dynamic(dynamic, start, end, nobs) in_sample_end = end + 1 if dynamic is None else dynamic seasonal = np.asarray(self._result.seasonal) predictions = seasonal[start:in_sample_end] oos = np.empty((0,)) if dynamic is not None: num = out_of_sample + end + 1 - dynamic oos = self._seasonal_forecast(num, None, offset=dynamic) elif out_of_sample: oos = self._seasonal_forecast(out_of_sample, None) oos_start = max(start - nobs, 0) oos = oos[oos_start:] predictions = np.r_[predictions, oos] return predictions ``` -------------------------------- ### Define Extended Time-Varying Regression Model Source: https://www.statsmodels.org/stable/examples/notebooks/generated/statespace_custom_models.html A complete example of a custom state space model extending `MLEModel`. It includes initialization, state space setup, parameter definitions, and transformations. ```python class TVRegressionExtended(sm.tsa.statespace.MLEModel): def __init__(self, y_t, x_t, w_t): exog = np.c_[x_t, w_t] # shaped nobs x 2 super(TVRegressionExtended, self).__init__( endog=y_t, exog=exog, k_states=2, initialization="diffuse" ) # Since the design matrix is time-varying, it must be # shaped k_endog x k_states x nobs # Notice that exog.T is shaped k_states x nobs, so we # just need to add a new first axis with shape 1 self.ssm["design"] = exog.T[np.newaxis, :, :] # shaped 1 x 2 x nobs self.ssm["selection"] = np.eye(self.k_states) self.ssm["transition"] = np.eye(self.k_states) # Which parameters need to be positive? self.positive_parameters = slice(1, 4) @property def param_names(self): return ["intercept", "var.e", "var.x.coeff", "var.w.coeff", "rho1", "rho2"] @property def start_params(self): """ Defines the starting values for the parameters The linear regression gives us reasonable starting values for the constant d and the variance of the epsilon error """ exog = sm.add_constant(self.exog) res = sm.OLS(self.endog, exog).fit() params = np.r_[res.params[0], res.scale, 0.001, 0.001, 0.7, 0.8] return params def transform_params(self, unconstrained): """ We constraint the last three parameters ('var.e', 'var.x.coeff', 'var.w.coeff') to be positive, because they are variances """ constrained = unconstrained.copy() constrained[self.positive_parameters] = ( constrained[self.positive_parameters] ** 2 ) return constrained def untransform_params(self, constrained): """ Need to unstransform all the parameters you transformed in the `transform_params` function """ unconstrained = constrained.copy() unconstrained[self.positive_parameters] = ( unconstrained[self.positive_parameters] ** 0.5 ) return unconstrained ``` -------------------------------- ### Example Usage of KDEMultivariateConditional Source: https://www.statsmodels.org/stable/_modules/statsmodels/nonparametric/kernel_density.html Demonstrates how to create and use the KDEMultivariateConditional class to estimate conditional densities. It shows sample data generation and bandwidth computation. ```python >>> import statsmodels.api as sm >>> nobs = 300 >>> c1 = np.random.normal(size=(nobs,1)) >>> c2 = np.random.normal(2,1,size=(nobs,1)) >>> dens_c = sm.nonparametric.KDEMultivariateConditional(endog=[c1], ... exog=[c2], dep_type='c', indep_type='c', bw='normal_reference') >>> dens_c.bw # show computed bandwidth array([ 0.41223484, 0.40976931]) ``` -------------------------------- ### Multi-step Forecasts with AutoReg Source: https://www.statsmodels.org/stable/examples/notebooks/generated/autoregressions.html Produces 12-step-ahead forecasts for the final 24 periods in the sample using a loop and dynamic prediction. This example requires prior setup for `res_glob`, `ind_prod`, and `plt`. ```python import numpy as np start = ind_prod.index[-24] forecast_index = pd.date_range(start, freq=ind_prod.index.freq, periods=36) cols = ["-".join(str(val) for val in (idx.year, idx.month)) for idx in forecast_index] forecasts = pd.DataFrame(index=forecast_index, columns=cols) for i in range(1, 24): fcast = res_glob.predict( start=forecast_index[i], end=forecast_index[i + 12], dynamic=True ) forecasts.loc[ fcast.index, cols[i]] = fcast _, ax = plt.subplots(figsize=(16, 10)) ind_prod.iloc[-24:].plot(ax=ax, color="black", linestyle="--") ax = forecasts.plot(ax=ax) ``` -------------------------------- ### GlobalOddsRatio.initialize Source: https://www.statsmodels.org/stable/generated/statsmodels.genmod.cov_struct.GlobalOddsRatio.initialize.html This method is called by GEE to perform setup before the fit method is executed. ```APIDOC ## GlobalOddsRatio.initialize ### Description Called by GEE, used by implementations that need additional setup prior to running fit. ### Parameters #### model - **model** (GEE class) - A reference to the parent GEE class instance. ``` -------------------------------- ### Normalize CalendarTimeTrend with Base Period Source: https://www.statsmodels.org/stable/generated/statsmodels.tsa.deterministic.CalendarTimeTrend.html This example shows how to create a CalendarTimeTrend object and normalize it using the first timestamp of the index as the base period. This ensures that time indices are calculated relative to the start of the data. ```python cal_trend_gen = CalendarTimeTrend("D", True, order=1, base_period=index[0]) cal_trend_gen.in_sample(index) ``` -------------------------------- ### Get Dynamic Predictions Source: https://www.statsmodels.org/stable/examples/notebooks/generated/statespace_sarimax_stata.html Performs dynamic predictions starting from a specified date. Dynamic predictions use previously predicted values as inputs for subsequent predictions, which can lead to increased forecast error over time. ```python # Dynamic predictions predict_dy = res.get_prediction(dynamic='1978-01-01') predict_dy_ci = predict_dy.conf_int() ``` -------------------------------- ### Initialize Factor Transition Matrix Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/statespace/dynamic_factor.html Sets up the parameters, fixed components, and indices for the factor transition matrix. This is crucial for models with dynamic factors, ensuring proper VAR(p) structure and identification constraints. ```python order = self.factor_order * self.k_factors k_factors = self.k_factors # Initialize the parameters self.parameters['factor_transition'] = ( self.factor_order * self.k_factors**2) # Setup fixed components of state space matrices # VAR(p) for factor transition if self.k_factors > 0: if self.factor_order > 0: self.ssm['transition', k_factors:order, :order - k_factors] = ( np.eye(order - k_factors)) self.ssm['selection', :k_factors, :k_factors] = np.eye(k_factors) # Identification requires constraining the state covariance to an # identity matrix self.ssm['state_cov', :k_factors, :k_factors] = np.eye(k_factors) # Setup indices of state space matrices self._idx_factor_transition = np.s_['transition', :k_factors, :order] ``` -------------------------------- ### SVAR Initialization and Parameter Setup Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/vector_ar/svar_model.html Initializes an SVAR model, setting up the structural parameter matrices A and B based on the specified type and provided masks. Handles default identity matrices for A and B if not given. ```python import numpy as np import statsmodels.tsa.base.tsa_model as tsbase from statsmodels.tools.decorators import deprecated_alias def svar_ckerr(svar_type, A, B): if A is None and (svar_type == 'A' or svar_type == 'AB'): raise ValueError('SVAR of type A or AB but A array not given.') if B is None and (svar_type == 'B' or svar_type == 'AB'): raise ValueError('SVAR of type B or AB but B array not given.') class SVAR(tsbase.TimeSeriesModel): r""" Fit VAR and then estimate structural components of A and B, defined: .. math:: Ay_t = A_1 y_{t-1} + u0ldots + A_p y_{t-p} + B\var(\epsilon_t) Parameters ---------- endog : array_like 1-d endogenous response variable. The independent variable. dates : array_like must match number of rows of endog svar_type : str "A" - estimate structural parameters of A matrix, B assumed = I "B" - estimate structural parameters of B matrix, A assumed = I "AB" - estimate structural parameters indicated in both A and B matrix A : array_like neqs x neqs with unknown parameters marked with 'E' for estimate B : array_like neqs x neqs with unknown parameters marked with 'E' for estimate References ---------- Hamilton (1994) Time Series Analysis """ y = deprecated_alias("y", "endog", remove_version="0.11.0") def __init__(self, endog, svar_type, dates=None, freq=None, A=None, B=None, missing='none'): super().__init__(endog, None, dates, freq, missing=missing) #(self.endog, self.names, # self.dates) = data_util.interpret_data(endog, names, dates) self.neqs = self.endog.shape[1] types = ['A', 'B', 'AB'] if svar_type not in types: raise ValueError('SVAR type not recognized, must be in ' + str(types)) self.svar_type = svar_type svar_ckerr(svar_type, A, B) self.A_original = A self.B_original = B # initialize A, B as I if not given # Initialize SVAR masks if A is None: A = np.identity(self.neqs) self.A_mask = A_mask = np.zeros(A.shape, dtype=bool) else: A_mask = np.logical_or(A == 'E', A == 'e') self.A_mask = A_mask if B is None: B = np.identity(self.neqs) self.B_mask = B_mask = np.zeros(B.shape, dtype=bool) else: B_mask = np.logical_or(B == 'E', B == 'e') self.B_mask = B_mask # convert A and B to numeric #TODO: change this when masked support is better or with formula #integration Anum = np.zeros(A.shape, dtype=float) Anum[~A_mask] = A[~A_mask] Anum[A_mask] = np.nan self.A = Anum Bnum = np.zeros(B.shape, dtype=float) Bnum[~B_mask] = B[~B_mask] Bnum[B_mask] = np.nan self.B = Bnum #LikelihoodModel.__init__(self, endog) #super().__init__(endog) ``` -------------------------------- ### Get Prediction Results Source: https://www.statsmodels.org/stable/_modules/statsmodels/regression/recursive_ls.html Retrieves prediction results from the recursive least squares model. Supports specifying start and end points for predictions. Note: Dynamic prediction or forecasts are not supported when constraints are present. ```python if start is None: start = self.model._index[0] # Handle start, end, dynamic start, end, out_of_sample, prediction_index = ( self.model._get_prediction_index(start, end, index)) # Handle `dynamic` if isinstance(dynamic, (bytes, str)): dynamic, _, _ = self.model._get_index_loc(dynamic) if self.model._r_matrix is not None and (out_of_sample or dynamic): raise NotImplementedError('Cannot yet perform out-of-sample or' ' dynamic prediction in models with' ' constraints.') # Perform the prediction # This is a (k_endog x npredictions) array; do not want to squeeze in # case of npredictions = 1 prediction_results = self.filter_results.predict( start, end + out_of_sample + 1, dynamic, **kwargs) # Return a new mlemodel.PredictionResults object res_obj = PredictionResults(self, prediction_results, information_set=information_set, signal_only=signal_only, row_labels=prediction_index) return PredictionResultsWrapper(res_obj) ``` -------------------------------- ### Get Starting Parameters for Process Regression Source: https://www.statsmodels.org/stable/_modules/statsmodels/regression/process_regression.html Obtains initial parameter estimates for the process regression model. It uses Ordinary Least Squares (OLS) for the mean structure parameters and sets other parameters to zero. ```python start_params = mod._get_start() ``` -------------------------------- ### MNLogit.initialize() Source: https://www.statsmodels.org/stable/generated/statsmodels.discrete.discrete_model.MNLogit.initialize.html Preprocesses the data for MNLogit. ```APIDOC ## MNLogit.initialize() ### Description Preprocesses the data for MNLogit. ### Method initialize ### Parameters This method does not appear to take any explicit parameters in the provided documentation. ``` -------------------------------- ### Generate Q-Q plot for two samples Source: https://www.statsmodels.org/stable/generated/statsmodels.graphics.gofplots.qqplot_2samples.html This example demonstrates how to generate a Q-Q plot for two samples drawn from normal distributions. It first creates ProbPlot instances for each sample and then uses qqplot_2samples to plot them. Ensure matplotlib is installed for plt.show(). ```python import statsmodels.api as sm import numpy as np import matplotlib.pyplot as plt from statsmodels.graphics.gofplots import qqplot_2samples x = np.random.normal(loc=8.5, scale=2.5, size=37) y = np.random.normal(loc=8.0, scale=3.0, size=37) pp_x = sm.ProbPlot(x) pp_y = sm.ProbPlot(y) qqplot_2samples(pp_x, pp_y) plt.show() ``` -------------------------------- ### Gibbs Sampler Setup and Initialization Source: https://www.statsmodels.org/stable/examples/notebooks/generated/statespace_tvpvar_mcmc_cfa.html Initializes the Gibbs sampler by setting the number of iterations and burn-in period, creating storage arrays for posterior draws, and populating them with initial values for the observation and state covariance matrices. It also constructs the simulation smoother object. ```python # Gibbs sampler setup niter = 11000 burn = 1000 # 1. Create storage arrays store_states = np.zeros((niter + 1, mod.nobs, mod.k_states)) store_obs_cov = np.zeros((niter + 1, mod.k_endog, mod.k_endog)) store_state_cov = np.zeros((niter + 1, mod.k_states)) # 2. Put in the initial values store_obs_cov[0] = initial_obs_cov store_state_cov[0] = initial_state_cov_diag mod.update_variances(store_obs_cov[0], store_state_cov[0]) # 3. Construct posterior samplers sim = mod.simulation_smoother(method='cfa') ``` -------------------------------- ### Get Predictions with Dynamic Factor MQ Model Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/statespace/dynamic_factor_mq.html Retrieves predictions from the dynamic factor model. Allows specifying start and end points for predictions, dynamic forecasting options, and information set for conditioning. Can also return forecasts in the original scale of the data if standardization was applied. ```python res = super().get_prediction(start=start, end=end, dynamic=dynamic, information_set=information_set, signal_only=signal_only, index=index, exog=exog, extend_model=extend_model, extend_kwargs=extend_kwargs, **kwargs) # If applicable, convert predictions back to original space if self.model.standardize and original_scale: prediction_results = res.prediction_results k_endog, _ = prediction_results.endog.shape mean = np.array(self.model._endog_mean) std = np.array(self.model._endog_std) if self.model.k_endog > 1: mean = mean[None, :] std = std[None, :] res._results._predicted_mean = ( res._results._predicted_mean * std + mean) if k_endog == 1: res._results._var_pred_mean *= std**2 else: res._results._var_pred_mean = ( std * res._results._var_pred_mean * std.T) return res ``` -------------------------------- ### Install statsmodels with pip Source: https://www.statsmodels.org/stable/install.html Install the latest released version of statsmodels using pip. This is a common method for Python package installation. ```bash python -m pip install statsmodels ``` -------------------------------- ### initialize Source: https://www.statsmodels.org/stable/generated/statsmodels.genmod.cov_struct.Autoregressive.html Called by GEE, used by implementations that need additional setup prior to running fit. ```APIDOC ## initialize ### Description Called by GEE, used by implementations that need additional setup prior to running fit. ### Method `initialize(model)` ### Parameters * **model**: The model object. ``` -------------------------------- ### Install statsmodels with Anaconda Source: https://www.statsmodels.org/stable/install.html Use this command to install the latest release of statsmodels when using the Anaconda distribution. It installs from the conda-forge channel. ```bash conda install -c conda-forge statsmodels ``` -------------------------------- ### Install statsmodels from GitHub source Source: https://www.statsmodels.org/stable/install.html Install statsmodels directly from its GitHub repository using pip. This requires a C compiler and Cython to be installed. ```bash python -m pip install git+https://github.com/statsmodels/statsmodels ``` -------------------------------- ### fitstart Method Source: https://www.statsmodels.org/stable/generated/statsmodels.sandbox.regression.gmm.IVGMM.html Creates an array of zeros, typically used for initializing parameter estimates. ```APIDOC fitstart() ``` -------------------------------- ### Find Starting Parameters for Beta Regression Source: https://www.statsmodels.org/stable/_modules/statsmodels/othermod/betareg.html Determines initial parameter estimates for the optimization algorithm using a few iterations of weighted least squares (WLS). This is not a full scoring algorithm but provides a good starting point. ```python def _start_params(self, niter=2, return_intermediate=False): """find starting values Parameters ---------- niter : int Number of iterations of WLS approximation return_intermediate : bool If False (default), then only the preliminary parameter estimate will be returned. If True, then also the two results instances of the WLS estimate for mean parameters and for the precision parameters will be returned. Returns ------- sp : ndarray start parameters for the optimization res_m2 : results instance (optional) Results instance for the WLS regression of the mean function. res_p2 : results instance (optional) Results instance for the WLS regression of the precision function. Notes ----- This calculates a few iteration of weighted least squares. This is not a full scoring algorithm. """ pass ``` -------------------------------- ### MLEModel.start_params Source: https://www.statsmodels.org/stable/generated/statsmodels.tsa.statespace.mlemodel.MLEModel.start_params.html This property returns the starting parameters for maximum likelihood estimation as an array. ```APIDOC ## MLEModel.start_params ### Description Starting parameters for maximum likelihood estimation. ### Property Type (array) ``` -------------------------------- ### FDR and Bonferroni Example Source: https://www.statsmodels.org/stable/_modules/statsmodels/sandbox/stats/multicomp.html Executes example functions for False Discovery Rate (FDR) and Bonferroni multiple comparison corrections. This snippet relies on a separate example file `ex_multicomp.py`. ```python from statsmodels.sandbox.stats.multicomp import example_fdr_bonferroni example_fdr_bonferroni() ``` -------------------------------- ### Install statsmodels with extras Source: https://www.statsmodels.org/stable/install.html Install statsmodels along with optional dependencies by specifying extras in the pip install command. Replace `` with a comma-separated list like 'build,develop,docs'. ```bash python -m pip install statsmodels[extras] ``` -------------------------------- ### ExponentialSmoothing.start_params Source: https://www.statsmodels.org/stable/_modules/statsmodels/tsa/statespace/exponential_smoothing.html Returns the starting parameters for the model optimization. ```APIDOC ## ExponentialSmoothing.start_params ### Description Returns the starting parameters for the model optimization. These are derived based on the model components (trend, seasonal, damping) and initialization method. ### Property `start_params` ### Returns - `numpy.ndarray`: The starting parameters for optimization. ``` -------------------------------- ### Starting Mean Calculation Source: https://www.statsmodels.org/stable/generated/statsmodels.genmod.families.family.Poisson.html Provides a starting value for the mean in the IRLS algorithm. ```APIDOC ## starting_mu(y) ### Description Starting value for mu in the IRLS algorithm. ### Parameters * **y** - The observed data. ``` -------------------------------- ### Oblimin Objective Function Setup Source: https://www.statsmodels.org/stable/_modules/statsmodels/multivariate/factor_rotation/_wrappers.html Sets up the objective function for oblimin rotations, supporting both orthogonal and oblique rotation methods. ```python elif method == 'oblimin': assert len(method_args) == 2, ('Both %s family parameter and ' 'rotation_method should be ' 'provided' % method) rotation_method = method_args[1] assert rotation_method in ['orthogonal', 'oblique'], ( 'rotation_method should be one of {orthogonal, oblique}') gamma = method_args[0] if algorithm == 'gpa': vgQ = lambda L=None, A=None, T=None: oblimin_objective( L=L, A=A, T=T, gamma=gamma, return_gradient=True) elif algorithm == 'gpa_der_free': ff = lambda L=None, A=None, T=None: oblimin_objective( L=L, A=A, T=T, gamma=gamma, rotation_method=rotation_method, return_gradient=False) else: raise ValueError('Algorithm %s is not possible for %s ' 'rotation' % (algorithm, method)) ``` -------------------------------- ### Starting Weights for GMM Source: https://www.statsmodels.org/stable/_modules/statsmodels/sandbox/regression/gmm.html Generates an identity matrix to be used as the starting weights for GMM estimation. ```python def start_weights(self, inv=True): """Create identity matrix for starting weights""" return np.eye(self.nmoms) ``` -------------------------------- ### Initialize EstimatorSettings Source: https://www.statsmodels.org/stable/generated/statsmodels.nonparametric.kernel_density.EstimatorSettings.html Instantiate EstimatorSettings to configure density estimation parameters. This is useful for customizing bandwidth estimation behavior, especially for large datasets or multiple variables. ```python settings = EstimatorSettings(randomize=True, n_jobs=3) >>> k_dens = KDEMultivariate(data, var_type, defaults=settings) ``` -------------------------------- ### BayesMixedGLM Starting Values Source: https://www.statsmodels.org/stable/_modules/statsmodels/genmod/bayes_mixed_glm.html Generates initial starting values for the parameters of a BayesMixedGLM. These are used to initialize the optimization process. ```python def _get_start(self): start_fep = np.zeros(self.k_fep) start_vcp = np.ones(self.k_vcp) start_vc = np.random.normal(size=self.k_vc) start = np.concatenate((start_fep, start_vcp, start_vc)) return start ```