Skip to content

ETS models can produce bad roots of characteristic equation that cannot be refit #341

Closed
@mitchelloharawild

Description

@mitchelloharawild

Ref: https://stackoverflow.com/questions/68673653/why-am-i-getting-parameters-out-of-range-error-after-fitting-a-default-ets-mod

cc @robjhyndman - I think this is due a difference in checks of characteristic equation roots between the C++ during optimisation (used in model()/estimate()), and the R check during initialisation (used in refit())

fable/R/etsmodel.R

Lines 503 to 512 in 0f3c42f

# End of easy tests. Now use characteristic equation
P <- c(phi * (1 - alpha - gamma), alpha + beta - alpha * phi + gamma - 1, rep(alpha + beta - alpha * phi, m - 2), (alpha + beta - phi), 1)
roots <- polyroot(P)
# cat("maxpolyroots: ", max(abs(roots)), "\n")
if (max(abs(roots)) > 1 + 1e-10) {
return(0)
}
}

From R, we have P[2] = alpha + beta - alpha * phi + gamma - 1

// End of easy tests. Now use characteristic equation
std::vector<double> op_new(m+2, alpha+beta-alpha*phi);
// P <- c(phi*(1-alpha-gamma),alpha+beta-alpha*phi+gamma-1,rep(alpha+beta-alpha*phi,m-2),(alpha+beta-phi),1)
op_new[0] = phi*(1-alpha-gamma);
op_new[1] = phi*(alpha+beta-alpha*phi+gamma-1);
op_new[m] = alpha+beta-phi;
op_new[m+1] = 1;
Environment base("package:base");
Function polyroot = base["polyroot"];
Function abs = base["abs"];
NumericVector res = abs(polyroot(op_new));
// Rprintf("alpha = %f, beta = %f, gamma = %f, phi = %f, m = %i\n",
// alpha, beta, gamma, phi, m);
// Rprintf("C: c(");
// for(int i=0;i<opr.size();i++) {
// Rprintf("%f, ", opr[i]);
// }
// Rprintf(")\n");
// Rprintf("C_new: c(");
//Rprintf("maxpolyroot: %f\n", max);
double max_root = max(res);
if(max_root > 1+1e-10) return(false);

From C++ we have op_new[1] = phi*(alpha+beta-alpha*phi+gamma-1)

Which is correct?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions