RainbowStats™Command reference · Model selection
Search variables. Select lags. Inspect the model.

GENETIC_MODEL

Use a genetic algorithm to search for a useful combination of predictors and lags. Evaluate each candidate with a random forest, then extract the selected forest and its proposed linear regression.

Predictor selectionLag selectionChronological validationExtractable models

Syntax

Basic command
G=GENETIC_MODEL(LIST(Y,X1,X2,X3),MAX_LAG:6,MAX_VARIABLES:2,SEED:12345)

Y is the dependent variable. The remaining series are candidate explanatory variables. Supply at least one predictor. The operator aligns the series and searches subsets containing at least one predictor.

For each selected predictor, the algorithm chooses one lag between zero and MAX_LAG. A lag of two pairs the predictor at observation t − 2 with the dependent variable at t.

Keep the objects. Assign the result to a variable so you can extract the chosen forest, regression, selected data, and test results.

Extract the forest and proposed regression
RF=EXTRACT(G,forest)
R=EXTRACT(RF,regression)
EXTRACT(R,slideshow)

The first extraction returns an actual RandomForestOperator. The second returns an actual RegressionOperator. EXTRACT(G,regression) is a shortcut to the same cached regression object.

How the search works

Each candidate specifies which predictors to include and the lag of each included predictor. The search combines promising candidates through selection, crossover, and mutation. It retains the best candidate and caches repeated specifications.

1. Train · first 60%

Fit a random forest for each candidate using the earliest observations.

2. Select · next 20%

Score predictions on later observations and apply a penalty for additional predictors.

3. Test · final 20%

Refit the winner on the first 80%, then evaluate it on the reserved final period.

These are the default splits. Change them with TRAIN_PCT and VALIDATION_PCT. The remaining observations form the test block.

The selection score

Validation RMSE + predictor count × validation baseline RMSE × PENALTY_PCT ÷ 100

Lower scores are better. The baseline predicts the training-period mean of Y throughout validation. With the default penalty of 1%, every included predictor adds 1% of that baseline RMSE to the selection score. Set PENALTY_PCT:0 to select by validation RMSE alone.

The first MAX_LAG aligned observations are removed for every candidate, including candidates with smaller lags. This keeps the target dates identical across comparisons.

The test period does not choose the model. The extracted forest and regression use training plus validation observations. The final test observations are excluded from their fitting sample.

Named options

All option values are integers. Use NAME:value, ARG(NAME,value), or an ARG_LIST(...) through the command wiring.

OptionDefaultMeaning
POPULATION16Candidates per generation; 4–128.
GENERATIONS20Maximum generations; 1–500.
MAX_EVALUATIONS200Maximum distinct candidate evaluations; at least POPULATION and at most 10,000.
MUTATION_PCT15Mutation probability per predictor gene; 1–100%.
MAX_LAG0Largest observation lag searched; 0–120, below sample size.
MAX_VARIABLES5Maximum included predictors; at least one, capped to the available count.
TREES64Trees in each forest; 1–1,000.
MAX_DEPTH8Maximum tree depth; 1–30.
MIN_LEAF5Minimum observations per forest leaf; at least one.
TRAIN_PCT60Initial training share; at least 10%.
VALIDATION_PCT20Subsequent validation share; at least 5%. Training plus validation cannot exceed 95%.
PENALTY_PCT1Complexity penalty per included predictor; 0–100% of validation baseline RMSE.
SEED12345Seed for the genetic search and forest fitting.

The current version searches predictor subsets and lags. Forest settings stay fixed during the search. MTRY uses the forest's default of approximately one third of the selected predictor count, with a minimum of one.

Input and sample requirements

Supply finite values, matching dates, and strictly increasing observation dates. At most 100 candidate predictors are accepted. After the common lag trim, training needs at least max(2 × MIN_LEAF, MAX_VARIABLES + 3) observations; validation and test each need at least five.

For an initial run, try a smaller population, fewer trees, and a modest evaluation budget. More candidates and trees increase computing time. The search can stop before its limits if a generation evaluates no new specifications.

Available extracts

Extract keyResult
forestSelected RandomForestOperator. Aliases: random_forest, randomforest, model.
regressionProposed RegressionOperator through the selected forest. Alias: proposed_regression.
selected_dataDataList containing Y and selected lagged predictors, using training plus validation only. Alias: data.
summarySample splits, settings, selection score, validation RMSE, and final test diagnostics.
selectedSelected predictor labels, input indices, and lags. Alias: variables.
candidatesLeading 20 candidate specifications and penalized validation scores.
historyNumber array of the best score after each completed generation.
lagsNumber array in input predictor order: −1 means excluded; 0 or higher gives the selected lag.
test_predictedForest predictions for the reserved test period. Alias: test_fitted.
test_residualsTest actual values minus test predictions.
test_chartActual and predicted values during the final test period.
test_rmseNumeric final test root mean squared error.
test_r_squaredNumeric final test R²; it can be negative.
slideshowSummary, selected predictors, leading candidates, and test chart.

Continue working with the forest

Inspect the selected model
RF=EXTRACT(G,forest)
EXTRACT(RF,slideshow)
EXTRACT(RF,importance)
EXTRACT(RF,benchmark)
R=EXTRACT(RF,regression)
EXTRACT(R,slideshow)

EXTRACT(RF,importance) returns the forest's permutation-importance values in selected predictor order. Its slideshow includes the labeled importance table.

Example: unemployment changes and economic activity

Search monthly payroll growth, consumer-price growth, industrial-production growth, and changes in the ten-year Treasury yield for a useful specification of monthly unemployment-rate changes. Each command is on one line.

Complete RainbowStats script
Unemployment=SET_NAME(SERIES_SINCE(UNRATE,19900101),"Unemployment Rate")
Payrolls=SET_NAME(SERIES_SINCE(PAYEMS,19900101),"Nonfarm Payrolls")
Prices=SET_NAME(SERIES_SINCE(CPIAUCSL,19900101),"Consumer Prices")
Production=SET_NAME(SERIES_SINCE(INDPRO,19900101),"Industrial Production")
BondYield=SET_NAME(SERIES_SINCE(GS10,19900101),"Monthly Ten-Year Treasury Yield")
Y=SET_NAME(DIFF(Unemployment),"Monthly Unemployment Rate Change")
X1=SET_NAME(LOGDIFF(Payrolls),"Monthly Payroll Log Change")
X2=SET_NAME(LOGDIFF(Prices),"Monthly CPI Log Change")
X3=SET_NAME(LOGDIFF(Production),"Monthly Industrial Production Log Change")
X4=SET_NAME(DIFF(BondYield),"Monthly Ten-Year Yield Change")
Inputs=SAME_DATE_RANGE(LIST(Y,X1,X2,X3,X4))
G=GENETIC_MODEL(Inputs,POPULATION:12,GENERATIONS:10,MAX_EVALUATIONS:80,MUTATION_PCT:20,MAX_LAG:6,MAX_VARIABLES:3,TREES:48,MAX_DEPTH:8,MIN_LEAF:5,TRAIN_PCT:60,VALIDATION_PCT:20,PENALTY_PCT:1,SEED:12345)
EXTRACT(G,slideshow)
RF=EXTRACT(G,forest)
EXTRACT(RF,slideshow)
ProposedRegression=EXTRACT(RF,regression)
EXTRACT(ProposedRegression,slideshow)
EXTRACT(G,lags)
TestResiduals=EXTRACT(G,test_residuals)
LINE_CHART(TestResiduals)
EXTRACT(G,test_rmse)
EXTRACT(G,test_r_squared)

The four entries in the lag array correspond to payrolls, CPI, industrial production, and the bond yield, in that order. For example, [2, −1, 4, −1] means payroll growth at lag two and industrial-production growth at lag four were selected; CPI and yield changes were excluded.

This lag vector illustrates how to read the output. The result depends on the supplied data, sample dates, and search settings.

Reading the results

Start with the final test period

Compare test RMSE with the test mean-baseline RMSE in the summary. The baseline predicts the mean from training plus validation. Lower model RMSE indicates better test performance than that simple benchmark. Inspect the test chart and residuals to see where the selected forest succeeds or misses.

Use the regression to inspect the selected inputs

The proposed regression fits actual Y against the selected lagged predictors with an intercept. It gives a linear comparison to the forest. A variable can help a forest through nonlinear relationships or interactions while contributing little to the linear regression.

Regression t-statistics and p-values are exploratory after this search: they do not adjust for the algorithm's selection of variables and lags.

Distinguish lags from a forecasting horizon

Lags count aligned observations. With consistently spaced monthly inputs, lag two represents two months. Lag zero uses a contemporaneous predictor. For a forecasting interpretation, predictors must be available at the forecast date; the operator does not account for publication delays or data revisions.

Keep the final test independent

The forest's OOB and OLS LOOCV diagnostics describe its training-plus-validation sample. Use the genetic operator's reserved test results for chronological evaluation. If you repeatedly change settings after inspecting the test results, that period becomes part of model selection.

What the command establishes: a candidate predictive specification found by a bounded search. It does not guarantee the globally best model or establish a causal effect.