GAfit: Genetic algorithm for preliminary estimation of a GMVAR,...

Description Usage Arguments Details Value References Examples

View source: R/geneticAlgorithm.R


GAfit estimates the specified GMVAR, StMVAR, or G-StMVAR model using a genetic algorithm. It's designed to find starting values for gradient based methods.


  model = c("GMVAR", "StMVAR", "G-StMVAR"),
  conditional = TRUE,
  parametrization = c("intercept", "mean"),
  constraints = NULL,
  same_means = NULL,
  structural_pars = NULL,
  ngen = 200,
  smart_mu = min(100, ceiling(0.5 * ngen)),
  initpop = NULL,
  ar_scale = 0.2,
  upper_ar_scale = 1,
  ar_scale2 = 1,
  regime_force_scale = 1,
  red_criteria = c(0.05, 0.01),
  pre_smart_mu_prob = 0,
  to_return = c("alt_ind", "best_ind"),
  seed = NULL



a matrix or class 'ts' object with d>1 columns. Each column is taken to represent a univariate time series. NA values are not supported.


a positive integer specifying the autoregressive order of the model.

For GMVAR and StMVAR models:

a positive integer specifying the number of mixture components.

For G-StMVAR models:

a size (2x1) integer vector specifying the number of GMVAR type components M1 in the first element and StMVAR type components M2 in the second element. The total number of mixture components is M=M1+M2.


is "GMVAR", "StMVAR", or "G-StMVAR" model considered? In the G-StMVAR model, the first M1 components are GMVAR type and the rest M2 components are StMVAR type.


a logical argument specifying whether the conditional or exact log-likelihood function


"intercept" or "mean" determining whether the model is parametrized with intercept parameters φ_{m,0} or regime means μ_{m}, m=1,...,M.


a size (Mpd^2 x q) constraint matrix C specifying general linear constraints to the autoregressive parameters. We consider constraints of form (φ_{1},...,φ_{M}) = C ψ, where φ_{m} = (vec(A_{m,1}),...,vec(A_{m,p}) (pd^2 x 1), m=1,...,M, contains the coefficient matrices and ψ (q x 1) contains the related parameters. For example, to restrict the AR-parameters to be the same for all regimes, set C= [I:...:I]' (Mpd^2 x pd^2) where I = diag(p*d^2). Ignore (or set to NULL) if linear constraints should not be employed.


Restrict the mean parameters of some regimes to be the same? Provide a list of numeric vectors such that each numeric vector contains the regimes that should share the common mean parameters. For instance, if M=3, the argument list(1, 2:3) restricts the mean parameters of the second and third regime to be the same but the first regime has freely estimated (unconditional) mean. Ignore or set to NULL if mean parameters should not be restricted to be the same among any regimes. This constraint is available only for mean parametrized models; that is, when parametrization="mean".


If NULL a reduced form model is considered. For structural model, should be a list containing the following elements:

  • W - a (dxd) matrix with its entries imposing constraints on W: NA indicating that the element is unconstrained, a positive value indicating strict positive sign constraint, a negative value indicating strict negative sign constraint, and zero indicating that the element is constrained to zero.

  • C_lambda - a (d(M-1) x r) constraint matrix that satisfies (λ_{2},..., λ_{M}) = C_{λ} γ where γ is the new (r x 1) parameter subject to which the model is estimated (similarly to AR parameter constraints). The entries of C_lambda must be either positive or zero. Ignore (or set to NULL) if the eigenvalues λ_{mi} should not be constrained.

See Virolainen (2020) for the conditions required to identify the shocks and for the B-matrix as well (it is W times a time-varying diagonal matrix with positive diagonal entries).


a positive integer specifying the number of generations to be ran through in the genetic algorithm.


a positive even integer specifying the population size in the genetic algorithm. Default is 10*n_params.


a positive integer specifying the generation after which the random mutations in the genetic algorithm are "smart". This means that mutating individuals will mostly mutate fairly close (or partially close) to the best fitting individual (which has the least regimes with time varying mixing weights practically at zero) so far.


a list of parameter vectors from which the initial population of the genetic algorithm will be generated from. The parameter vectors should be...

For unconstrained models:

Should be size ((M(pd^2+d+d(d+1)/2+2)-M1-1)x1) and have the form θ = (υ_{1}, ...,υ_{M}, α_{1},...,α_{M-1},ν), where

  • υ_{m} = (φ_{m,0},φ_{m},σ_{m})

  • φ_{m} = (vec(A_{m,1}),...,vec(A_{m,p})

  • and σ_{m} = vech(Ω_{m}), m=1,...,M,

  • ν=(ν_{M1+1},...,ν_{M})

  • M1 is the number of GMVAR type regimes.

For constrained models:

Should be size ((M(d+d(d+1)/2+2)+q-M1-1)x1) and have the form θ = (φ_{1,0},...,φ_{M,0},ψ, σ_{1},...,σ_{M},α_{1},...,α_{M-1},ν), where

  • ψ (qx1) satisfies (φ_{1},..., φ_{M}) = C ψ where C is a (Mpd^2xq) constraint matrix.

For same_means models:

Should have the form θ = (μ,ψ, σ_{1},...,σ_{M},α_{1},...,α_{M-1},ν), where

  • μ= (μ_{1},...,μ_{g}) where μ_{i} is the mean parameter for group i and g is the number of groups.

  • If AR constraints are employed, ψ is as for constrained models, and if AR constraints are not employed, ψ = (φ_{1},...,φ_{M}).

For structural models:

Should have the form θ = (φ_{1,0},...,φ_{M,0},φ_{1},...,φ_{M}, vec(W),λ_{2},...,λ_{M},α_{1},...,α_{M-1},ν), where

  • λ_{m}=(λ_{m1},...,λ_{md}) contains the eigenvalues of the mth mixture component.

If AR parameters are constrained:

Replace φ_{1},..., φ_{M} with ψ (qx1) that satisfies (φ_{1},..., φ_{M}) = C ψ, as above.

If same_means:

Replace (φ_{1,0},...,φ_{M,0}) with (μ_{1},...,μ_{g}), as above.

If W is constrained:

Remove the zeros from vec(W) and make sure the other entries satisfy the sign constraints.

If λ_{mi} are constrained:

Replace λ_{2},...,λ_{M} with γ (rx1) that satisfies (λ_{2},..., λ_{M}) = C_{λ} γ where C_{λ} is a (d(M-1) x r) constraint matrix.

Above, φ_{m,0} is the intercept parameter, A_{m,i} denotes the ith coefficient matrix of the mth mixture component, Ω_{m} denotes the error term covariance matrix of the m:th mixture component, and α_{m} is the mixing weight parameter. The W and λ_{mi} are structural parameters replacing the error term covariance matrices (see Virolainen, 2020). If M=1, α_{m} and λ_{mi} are dropped. If parametrization=="mean", just replace each φ_{m,0} with regimewise mean μ_{m}. vec() is vectorization operator that stacks columns of a given matrix into a vector. vech() stacks columns of a given matrix from the principal diagonal downwards (including elements on the diagonal) into a vector.

In the GMVAR model, M1=M and ν is dropped from the parameter vector. In the StMVAR model, M1=0. In the G-StMVAR model, the first M1 regimes are GMVAR type and the rest M2 regimes are StMVAR type. In StMVAR and G-StMVAR models, the degrees of freedom parameters in ν should be strictly larger than two.

The notation is similar to the cited literature.


a size (dx1) vector defining means of the normal distributions from which each mean parameter μ_{m} is drawn from in random mutations. Default is colMeans(data). Note that mean-parametrization is always used for optimization in GAfit - even when parametrization=="intercept". However, input (in initpop) and output (return value) parameter vectors can be intercept-parametrized.


a size (dx1) strictly positive vector defining standard deviations of the normal distributions from which each mean parameter μ_{m} is drawn from in random mutations. Default is 2*sd(data[,i]), i=1,..,d.


a size (dx1) strictly positive vector specifying the scale and variability of the random covariance matrices in random mutations. The covariance matrices are drawn from (scaled) Wishart distribution. Expected values of the random covariance matrices are diag(omega_scale). Standard deviations of the diagonal elements are sqrt(2/d)*omega_scale[i] and for non-diagonal elements they are sqrt(1/d*omega_scale[i]*omega_scale[j]). Note that for d>4 this scale may need to be chosen carefully. Default in GAfit is var(stats::ar(data[,i], order.max=10)$resid, na.rm=TRUE), i=1,...,d. This argument is ignored if structural model is considered.


a size (dx1) strictly positive vector partly specifying the scale and variability of the random covariance matrices in random mutations. The elements of the matrix W are drawn independently from such normal distributions that the expectation of the main diagonal elements of the first regime's error term covariance matrix Ω_1 = WW' is W_scale. The distribution of Ω_1 will be in some sense like a Wishart distribution but with the columns (elements) of W obeying the given constraints. The constraints are accounted for by setting the element to be always zero if it is subject to a zero constraint and for sign constraints the absolute value or negative the absolute value are taken, and then the variances of the elements of W are adjusted accordingly. This argument is ignored if reduced form model is considered.


a length M - 1 vector specifying the standard deviation of the mean zero normal distribution from which the eigenvalue λ_{mi} parameters are drawn from in random mutations. As the eigenvalues should always be positive, the absolute value is taken. The elements of lambda_scale should be strictly positive real numbers with the m-1th element giving the degrees of freedom for the mth regime. The expected value of the main diagonal elements ij of the mth (m>1) error term covariance matrix will be W_scale[i]*(d - n_i)^(-1)*sum(lambdas*ind_fun) where the (d x 1) vector lambdas is drawn from the absolute value of the t-distribution, n_i is the number of zero constraints in the ith row of W and ind_fun is an indicator function that takes the value one iff the ijth element of W is not constrained to zero. Basically, larger lambdas (or smaller degrees of freedom) imply larger variance.

If the lambda parameters are constrained with the (d(M - 1) x r) constraint matrix C_lambda, then provide a length r vector specifying the standard deviation of the (absolute value of the) mean zero normal distribution each of the γ parameters are drawn from (the γ is a (r x 1) vector). The expected value of the main diagonal elements of the covariance matrices then depend on the constraints.

This argument is ignored if M==1 or a reduced form model is considered. Default is rep(3, times=M-1) if lambdas are not constrained and rep(3, times=r) if lambdas are constrained.

As with omega_scale and W_scale, this argument should be adjusted carefully if specified by hand. NOTE that if lambdas are constrained in some other way than restricting some of them to be identical, this parameter should be adjusted accordingly in order to the estimation succeed!


a positive real number adjusting how large AR parameter values are typically proposed in construction of the initial population: larger value implies larger coefficients (in absolute value). After construction of the initial population, a new scale is drawn from (0, 0.) uniform distribution in each iteration.


the upper bound for ar_scale parameter (see above) in the random mutations. Setting this too high might lead to failure in proposing new parameters that are well enough inside the parameter space, and especially with large p one might want to try smaller upper bound (e.g., 0.5).


a positive real number adjusting how large AR parameter values are typically proposed in some random mutations (if AR constraints are employed, in all random mutations): larger value implies smaller coefficients (in absolute value). Values larger than 1 can be used if the AR coefficients are expected to be very small. If set smaller than 1, be careful as it might lead to failure in the creation of stationary parameter candidates


a non-negative real number specifying how much should natural selection favor individuals with less regimes that have almost all mixing weights (practically) at zero. Set to zero for no favoring or large number for heavy favoring. Without any favoring the genetic algorithm gets more often stuck in an area of the parameter space where some regimes are wasted, but with too much favouring the best genes might never mix into the population and the algorithm might converge poorly. Default is 1 and it gives 2x larger surviving probability weights for individuals with no wasted regimes compared to individuals with one wasted regime. Number 2 would give 3x larger probability weights etc.


a length 2 numeric vector specifying the criteria that is used to determine whether a regime is redundant (or "wasted") or not. Any regime m which satisfies sum(mixingWeights[,m] > red_criteria[1]) < red_criteria[2]*n_obs will be considered "redundant". One should be careful when adjusting this argument (set c(0, 0) to fully disable the 'redundant regime' features from the algorithm).


A number in [0,1] giving a probability of a "smart mutation" occuring randomly in each iteration before the iteration given by the argument smart_mu.


should the genetic algorithm return the best fitting individual which has "positive enough" mixing weights for as many regimes as possible ("alt_ind") or the individual which has the highest log-likelihood in general ("best_ind") but might have more wasted regimes?


a real number defining the minimum value of the log-likelihood function that will be considered. Values smaller than this will be treated as they were minval and the corresponding individuals will never survive. The default is -(10^(ceiling(log10(n_obs)) + d) - 1).


a single value, interpreted as an integer, or NULL, that sets seed for the random number generator in the beginning of the function call. If calling GAfit from fitGSMVAR, use the argument seeds instead of passing the argument seed.


The core of the genetic algorithm is mostly based on the description by Dorsey and Mayer (1995). It utilizes a slightly modified version of the individually adaptive crossover and mutation rates described by Patnaik and Srinivas (1994) and employs (50%) fitness inheritance discussed by Smith, Dike and Stegmann (1995).

By "redundant" or "wasted" regimes we mean regimes that have the time varying mixing weights practically at zero for almost all t. A model including redundant regimes would have about the same log-likelihood value without the redundant regimes and there is no purpose to have redundant regimes in a model.


Returns the estimated parameter vector which has the form described in initpop.



 # Preliminary estimation of a G-StMVAR(1, 1, 1) model with 50 generations.
 GA_estimates <- GAfit(gdpdef, p=1, M=c(1, 1), model="G-StMVAR",
                       ngen=50, seed=1)

saviviro/gmvarkit documentation built on Oct. 25, 2021, 2:14 a.m.