Stepwise Regression: private API
PortfolioOptimisers._regression — Method
_regression(re::StepwiseRegression{<:PValue, <:ForwardSelection}, x::VecNum,
F::MatNum)Grows a factor set from empty, admitting the model whose p-values all clear the threshold.
A candidate is admissible only when every coefficient of that candidate model is significant, so a factor that is significant on its own is rejected when it makes an incumbent insignificant.
Algorithm
- Set
includedempty andvalto zero, so the loop runs at least once. - While
valdoes not exceedre.crit.t, do steps 3 to 6. - Take the factors that
includeddoes not hold, givingexcluded. - For each
iofexcluded, fitre.tgtto an intercept column and the columns[included; i], and read the p-value of columni, givingtest_pval. Keepiasnew_factorwhentest_pvalis the smallest so far and no p-value of that candidate model exceedsre.crit.t, and keep that model's p-values aspvals. - Stop the loop when step 4 kept no factor. Otherwise push
new_factorontoincluded. - Set
valto the largest entry ofpvals. - Call
add_best_factor_after_pval_failure!, which acts only whenincludedis still empty.
Arguments
re: Stepwise regression estimator with aPValuecriterion and theForwardSelectionalgorithm.x: Response vector,observations × 1.F: Data matrixobservations × factorsif thedimskeyword does not exist ordims = 1,factors × observationswhendims = 2.
Returns
included::Vector{Int}: Indices of the selected factors, in the order the loop added them, so it is not sorted. It holds at least one index.
Related
PortfolioOptimisers._regression — Method
_regression(re::StepwiseRegression{<:MinMaxValStepwiseRegressionCriterion,
<:ForwardSelection}, x::VecNum, F::MatNum)Grows a factor set from empty, adding the factor whose model scores best.
The starting score is the worst value the criterion can take, so the first addition always happens and the selection is never empty. get_forward_reg_incl_excl! reads the direction from regression_polarity, so one loop serves a minimised and a maximised criterion alike.
Algorithm
- Set
includedempty andexcludedto every factor index, in ascending order. - Read the criterion from
re.critandre.tgtwithregression_criterion_func, givingcriterion_func, and the starting score fromregression_threshold, givingt. - Fill
valuewith that same worst score, one entry per factor. - Do steps 5 to 7 at most once per observation.
- For each
iofexcluded, fitre.tgtto an intercept column and the columns[included; i], and writecriterion_funcof that fit intovalue[i]. - Call
get_forward_reg_incl_excl!, which moves the best factor and returns the newt. - Stop when step 6 moved no factor.
Arguments
re: Stepwise regression estimator with aMinMaxValStepwiseRegressionCriterioncriterion and theForwardSelectionalgorithm.x: Response vector,observations × 1.F: Data matrixobservations × factorsif thedimskeyword does not exist ordims = 1,factors × observationswhendims = 2.
Returns
included::Vector{Int}: Indices of the selected factors, in the order the loop added them, so it is not sorted. It holds at least one index.
Related
PortfolioOptimisers._regression — Method
_regression(re::StepwiseRegression{<:PValue, <:BackwardElimination}, x::VecNum,
F::MatNum)Shrinks a factor set from full, dropping the factor of largest p-value while any exceeds the threshold.
Algorithm
- Fit
re.tgtto an intercept column and every column ofF, read the coefficient p-values, and setvalto the largest of them. - Set
includedto every factor index andexcludedempty. - While
valexceedsre.crit.t, do steps 4 to 6. - Set
includedto the factors thatexcludeddoes not hold. Stop the loop whenincludedis empty. - Fit
re.tgtto an intercept column and the columnsincluded, givingpvals. - Set
valto the largest entry ofpvals, and push that entry's factor ontoexcluded. The push acts only when step 3 runs the loop again, because step 4 is what readsexcluded, so the last iteration's push is discarded. - Call
add_best_factor_after_pval_failure!, which acts only whenincludedis empty.
Arguments
re: Stepwise regression estimator with aPValuecriterion and theBackwardEliminationalgorithm.x: Response vector,observations × 1.F: Data matrixobservations × factorsif thedimskeyword does not exist ordims = 1,factors × observationswhendims = 2.
Returns
included::VecInt: Indices of the selected factors, in ascending order. It holds at least one index. It is the1:size(F, 2)range itself when the full model already passes the threshold, and aVector{Int}otherwise.
Related
PortfolioOptimisers._regression — Method
_regression(re::StepwiseRegression{<:MinMaxValStepwiseRegressionCriterion,
<:BackwardElimination}, x::VecNum, F::MatNum)Shrinks a factor set from full, removing the factor whose reduced model scores best.
value[j] is the score of the model that omits j, so the reading of "best" is the same as the forward direction's and not its inverse: under a minimised criterion the code removes the factor of lowest value, and under a maximised one the factor of highest.
Algorithm
- Set
includedto every factor index, in ascending order. - Read the criterion from
re.critandre.tgtwithregression_criterion_func, givingcriterion_func. - Fit
re.tgtto an intercept column and every column ofF, and setttocriterion_funcof that full model. - Fill
valuewith the worst score the criterion can take, one entry per factor. - Do steps 6 to 8 at most once per observation.
- For each factor of
included, fitre.tgtto an intercept column and the other columns ofincluded, and writecriterion_funcof that fit into that factor's entry ofvalue. Fit the intercept column alone whenincludedholds one factor. - Call
get_backward_reg_incl!, which removes the best factor and returns the newt. - Stop when step 7 removed no factor.
Arguments
re: Stepwise regression estimator with aMinMaxValStepwiseRegressionCriterioncriterion and theBackwardEliminationalgorithm.x: Response vector,observations × 1.F: Data matrixobservations × factorsif thedimskeyword does not exist ordims = 1,factors × observationswhendims = 2.
Returns
included::Vector{Int}: Indices of the selected factors, in ascending order. It can be empty: a criterion that rewards every removal removes every factor, which is the common outcome on a response the factors do not explain. The callerregressionwarns on an empty return, naming the asset, and fits the intercept column alone.
Related
PortfolioOptimisers.add_best_factor_after_pval_failure! — Function
add_best_factor_after_pval_failure!(tgt::AbstractRegressionTarget,
included::VecInt, F::MatNum,
x::VecNum)Adds the factor of smallest p-value when a p-value search selected none.
Both PValue methods of _regression call it last, so a p-value search never returns an empty factor set. It does nothing when included already holds a factor, and it warns whenever it adds one, because the factor it adds failed the threshold by construction.
Algorithm
- Return at once when
includedis not empty. - For each factor
iofF, fittgtto an intercept column and columni, and read that column's p-value, givingtest_pval. - Keep the smallest
test_pvalof step 2 and the factor that carries it, givingbest_pvalandnew_factor. - Warn, naming
new_factorandbest_pval. - Push
new_factorontoincluded.
Arguments
tgt: Regression target that fits each candidate model.included: Indices of the factors the search selected. Written in place.F: Data matrixobservations × factorsif thedimskeyword does not exist ordims = 1,factors × observationswhendims = 2.x: Response vector,observations × 1.
Returns
nothing:includedgains exactly one index when it was empty, and is unchanged otherwise.
Related
PortfolioOptimisers.get_forward_reg_incl_excl! — Function
get_forward_reg_incl_excl!(crit::MinMaxValStepwiseRegressionCriterion,
value::VecNum, excluded::VecInt,
included::VecInt, t::Number)Moves the best excluded factor into included when it improves the criterion.
regression_polarity states the direction: findmin and < under a minimised criterion, findmax and > under a maximised one. The search reads the whole value vector, and the answer is still the best excluded factor. An entry of an included factor holds the score that selected it; t is that same score, and it only moves in the improving direction, so no included entry improves on the current t. A score that does improve on t therefore belongs to an excluded factor, and searchsortedfirst always finds its index in excluded.
Algorithm
- Read
bestandimprovesfromregression_polarity. - Find the best entry of
valuewithbest, givingvaland its indexidx. - Return
tunchanged whenvaldoes not improve ont. - Otherwise find the position of
idxinexcluded, move that entry to the end ofincluded, and setttoval.
Arguments
crit: Stepwise regression criterion, whose polarity states which direction is better.value: Criterion score of each factor. Entryiis the score of the model that adds factoritoincluded.excluded: Indices of the factors outside the model, in ascending order. Written in place.included: Indices of the factors inside the model, in the order the search added them. Written in place.t: Threshold value.
Returns
t::Number: The score of the factor that moved, or the inputtwhen no factor moved.
Related
PortfolioOptimisers.get_backward_reg_incl! — Function
get_backward_reg_incl!(crit::MinMaxValStepwiseRegressionCriterion, value::VecNum,
included::VecInt, t::Number)Removes the best included factor from included when its removal improves the criterion.
regression_polarity states the direction: findmin and < under a minimised criterion, findmax and > under a maximised one. The search reads the whole value vector, and the answer is still the best included factor. An entry of a removed factor holds the score that removed it; t is that same score, and it only moves in the improving direction, so no removed entry improves on the current t. A score that does improve on t therefore belongs to an included factor, and searchsortedfirst always finds its index in included.
Algorithm
- Read
bestandimprovesfromregression_polarity. - Find the best entry of
valuewithbest, givingvaland its indexidx. That is the best model reachable by one removal. - Return
tunchanged whenvaldoes not improve ont. - Otherwise find the position of
idxinincluded, remove that entry, and setttoval.
Arguments
crit: Stepwise regression criterion, whose polarity states which direction is better.value: Criterion score of each factor. Entryjis the score of the model that omits factorj, so the best entry names the removal that helps most.included: Indices of the factors inside the model, in ascending order. Written in place.t: Threshold value.
Returns
t::Number: The score of the model left by the removal, or the inputtwhen no factor was removed.
Related