Bayesian Linear Regression
An implementation of the bayesian linear regression.
:- use_module('path/to/.../src/methods/bayesian_linear_regression/bayesian_linear_regression.pl').
%% usage example
TrainData = [5.1,3.5,1.4, 4.9,3.0,1.4, 4.7,3.2,1.3, 4.6,3.1,1.5],
TestData = [3,2,0, 5,1,4, 0,0,4, 3,3,5, 0,5,5, 2,5,5],
blr_initModel(1,0,50,0.0001),
blr_train(TrainData, 3, [0,1,0,1])
blr_predict(TestData, 3, PredictionsList).
Available Predicates
- blr_initModel/4
- blr_alpha/1
- blr_beta/1
- blr_dataOffset/1
- blr_dataScale/1
- blr_omega/1
- blr_predict/3
- blr_predictWithStd/4
- blr_rmse/4
- blr_train/3
- blr_variance/1
blr_initModel/4
Should be called before all other predicates!
Initiates the Model so now train/5 can be called.
Before predict/5 or predictWitStd/7 can be used train/5 has to be called before
%% predicate definition
blr_initModel(CenterData, ScaleData, NIterMax, Tol) :-
NIterMax >= 0,
Tol > 0,
initModelI(CenterData, ScaleData, NIterMax, Tol).
%% foreign c++ predicate definition
foreign(initModel, c, initModelI( +integer,
+integer,
+integer, +float32)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
centerData | +(bool)integer |
Whether or not center the data according to the examples. Set 1 (true) or 0 (false) |
(1) true |
scaleData | +(bool)integer |
Whether or not scale the data according to the standard deviation of each feature. Set 1 (true) or 0 (false) |
(0) false |
nIterMax | +integer | Maximum number of iterations for convergency. | 50 |
tol | +float | Level from which the solution is considered sufficientlly stable. | 1e-4 |
blr_alpha/1
Get the precision (or inverse variance) of the gaussian prior.
train/5 should be called before.
%% predicate definition
blr_alpha(Alpha) :-
alphaI(Alpha).
%% foreign c++ predicate definition
foreign(alpha, c, alphaI([-float32])).
Parameters
Name | Type | Description | Default |
---|---|---|---|
alpha | -float | Returns the alpha value | - |
blr_beta/1
Get the precision (or inverse variance) beta of the model.
blr_train/5 should be called before.
%% predicate definition
blr_beta(Beta) :-
betaI(Beta).
%% foreign c++ predicate definition
foreign(beta, c, betaI([-float32])).
Parameters
Name | Type | Description | Default |
---|---|---|---|
beta | -float | Returns the beta value | - |
blr_dataOffset/1
Get the mean vector computed on the features over the training points.
%% predicate definition
blr_dataOffset(ResponsesList) :-
dataOffsetI(X, Xsize),
convert_float_array_to_list(X, Xsize, ResponsesList).
%% foreign c++ predicate definition
foreign(dataOffset, c, dataOffsetI(-pointer(float_array), -integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
responsesOffset | -vector | - | - |
blr_dataScale/1
Get the vector of standard deviations computed on the features over the training points.
%% predicate definition
blr_dataScale(DataOffsetList) :-
dataScaleI(X, Xsize),
convert_float_array_to_list(X, Xsize, DataOffsetList).
%% foreign c++ predicate definition
foreign(dataScale, c, dataScaleI(-pointer(float_array), -integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
dataOffset | -vector | - | - |
blr_omega/1
Get the solution vector.
%% predicate definition
blr_omega(OmegaList) :-
omegaI(X, Xsize),
convert_float_array_to_list(X, Xsize, OmegaList).
%% foreign c++ predicate definition
foreign(omega, c, omegaI(-pointer(float_array), -integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
omega | -vector | Omega solution vector | - |
blr_predict/3
Predict yi for each data point in the given data matrix using the currently-trained Bayesian Ridge model.
%% predicate definition
blr_predict(PointsList, PointsRows, PredictionsList) :-
convert_list_to_float_array(PointsList, PointsRows, array(Xsize, Xrows, X)),
predictI(X, Xsize, Xrows, Y, Ysize),
convert_float_array_to_list(Y, Ysize, PredictionsList).
%% foreign c++ predicate definition
foreign(predict, c, predictI( +pointer(float_array), +integer, +integer,
-pointer(float_array), -integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
points | +matrix | The data points to apply the model. | - |
predictions | -vector | y, Contains the predicted values on completion. | - |
blr_predictWithStd/4
Predict yi and the standard deviation of the predictive posterior distribution for each data point in the given data matrix, using the currently-trained Bayesian Ridge estimator.
%% predicate definition
blr_predictWithStd(PointsList, PointsRows, PredictionsList, STDList) :-
convert_list_to_float_array(PointsList, PointsRows, array(Xsize, Xrows, X)),
predictWithStdI(X, Xsize, Xrows, Y, Ysize, Z, Zsize),
convert_float_array_to_list(Y, Ysize, PredictionsList),
convert_float_array_to_list(Z, Zsize, STDList).
%% foreign c++ predicate definition
foreign(predictWithStd, c, predictWithStdI( +pointer(float_array), +integer, +integer,
-pointer(float_array), -integer,
-pointer(float_array), -integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
points | +matrix | The data points to apply the model. | - |
predictions | -vector | y, Contains the predicted values on completion. | - |
std | -vector | Standard deviations of the predictions. | - |
blr_rmse/4
Compute the Root Mean Square Error between the predictions returned by the model and the true responses.
%% predicate definition
blr_rmse(DataList, DataRows, ResponsesList, RMSE) :-
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrows, X)),
convert_list_to_float_array(ResponsesList, array(Ysize, Y)),
rmseI(X, Xsize, Xrows, Y, Ysize, RMSE).
%% foreign c++ predicate definition
foreign(rmse, c, rmseI( +pointer(float_array), +integer, +integer,
+pointer(float_array), +integer,
[-float32])).
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | +matrix | Data points to predict | - |
responses | +vector | A vector of targets. | - |
rmse | -float | Root mean squared error. | - |
blr_train/3
Run BayesianLinearRegression.
The input matrix (like all mlpack matrices) should be column-major – each column is an observation and each row is a dimension.
%% predicate definition
blr_train(DataList, DataRows, ResponsesList) :-
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrows, X)),
convert_list_to_float_array(ResponsesList, array(Ysize, Y)),
trainI(X, Xsize, Xrows, Y, Ysize).
%% foreign c++ predicate definition
foreign(train, c, trainI( +pointer(float_array), +integer, +integer,
+pointer(float_array), +integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | +matrix | Column-major input data, dim(P, N). | - |
responses | +vector | A vector of targets, dim(N). | - |
blr_variance/1
Get the estimate variance.
blr_train/5 should be called before.
%% predicate definition
blr_variance(Variance) :-
varianceI(Variance).
%% foreign c++ predicate definition
foreign(variance, c, varianceI([-float32])).
Parameters
Name | Type | Description | Default |
---|---|---|---|
variance | -float | 1.0/beta | - |
Connected Links/Resources
If you want a more detailed explanation, then go to the python documentation. There is most of the time a good explanation on how the methods work and what the parameters do.
added some of the links from the python documentation