Skip to content

Statistical models

Variadic function definitions in the KX Insights ML-Analytics library for various statistical models

.ml.kxi.stats. OLS.fit fit an Ordinary Least Squares model WLS.fit fit a Weighted Least Squares model

ML Toolkit statistical algorithms for examples of function returns for prediction/update, not covered below

.ml.kxi.stats.OLS.fit

Fit an Ordinary Least Squares model

.ml.kxi.stats.OLS.fit[endog;exog]
.ml.kxi.stats.OLS.fit[endog;exog;config]

Where

  • endog is the numerical endogenous variable (variable determined by it's relationship with others)
  • exog are the exogenous variables (N variables which effect the endogenous variable)
  • config is an optional dictionary argument containing modifications to default behavior, with key

    key type default description
    trend boolean 1b Does the model expect a trend to be accounted for in fitting?
q)endog:til 10
q)exog:3+2*til 10

// Fit a model using default configuration
q)show mdl1:.ml.kxi.stats.OLS.fit[endog;exog]
modelInfo| `coef`variables`statsDict!(-1.5 0.5;(+(,`name)!,`yIntercept`x0)!+`..
predict  | {[config;exog]
  modelInfo:config`modelInfo;
  trend:`yIntercept i..
q)mdl1[`modelInfo;`variables]
name      | coef stdErr       tStat         pValue C195
----------| ---------------------------------------------------
yIntercept| -1.5 2.571224e-16 -5.833798e+15 0      5.929253e-16
x0        | 0.5  1.93265e-17  2.587121e+16  0      4.456699e-17

// Fit a model modifying the default behaviour
q)show mdl2:.ml.kxi.stats.OLS.fit[endog;exog;enlist[`trend]!enlist 0b]
modelInfo| `coef`variables`statsDict!(,0.3983051;(+(,`name)!,,`x0)!+`coef`std..
predict  | {[config;exog]
  modelInfo:config`modelInfo;
  trend:`yIntercept i..
q)mdl2[`modelInfo;`variables]
name| coef      stdErr     tStat    pValue       C195
----| -----------------------------------------------------
x0  | 0.3983051 0.01622758 24.54495 2.875458e-10 0.03742086

.ml.kxi.stats.WLS.fit

Fit a Weighted Least Squares model

.ml.kxi.stats.WLS.fit[endog;exog]
.ml.kxi.stats.WLS.fit[endog;exog;config]

Where

  • endog is the numerical endogenous variable (variable determined by its relationship with others)
  • exog are the exogenous variables (\(N\) variables which affect the endogenous variable)
  • config is an optional dictionary argument containing modifications to default behavior, with keys

    key type default description
    weights float[] :: The weights to be applied to the endogenous variable (must be the same length as endog), if default these are inferred from the data
    trend boolean 1b Does the model expect a trend to be accounted for in fitting
q)endog:til 10
q)exog:3+2*til 10

// Fit a model using default configuration
q)show mdl1:.ml.kxi.stats.WLS.fit[endog;exog]
modelInfo| `coef`variables`statsDict`weights!(-1.5 0.5;(+(,`name)!,`yIntercep..
predict  | {[config;exog]
  modelInfo:config`modelInfo;
  trend:`yIntercept i..
q)show each mdl1[`modelInfo;`variables`weights];
name      | coef stdErr       tStat         pValue C195
----------| ---------------------------------------------------
yIntercept| -1.5 5.126997e-15 -2.925689e+14 0      1.182288e-14
x0        | 0.5  3.853687e-16 1.297459e+15  0      8.886618e-16
130.4302 46.95487 23.95657 14.49224 9.701419 6.945986 5.217208 4.06184 3.2517..

// Fit a model modifying the default behaviour
q)weights:10?1f
q)show mdl2:.ml.kxi.stats.WLS.fit[endog;exog;`trend`weights!(0b;weights)]
modelInfo| `coef`variables`statsDict`weights!(,0.4012137;(+(,`name)!,,`x0)!+`..
predict  | {[config;exog]
  modelInfo:config`modelInfo;
  trend:`yIntercept i..
q)show each mdl2[`modelInfo;`variables`weights];
name| coef      stdErr     tStat    pValue       C195
----| -----------------------------------------------------
x0  | 0.4012137 0.01625652 24.68018 2.723961e-10 0.03748759
0.5232126 0.6587993 0.4064619 0.3612185 0.6559165 0.6997534 0.5315412 0.89392..