|
|
# Simple Linear Regression and Prediction
|
|
|
|
|
|
An implementation of simple linear regression and ridge regression using ordinary least squares.
|
|
|
|
|
|
# Available Predicates
|
|
|
|
|
|
* [initModel/7](/PrologMethods/Regression/linear_regression#initmodel7)
|
|
|
* [initModelWithWeights/9](/PrologMethods/Regression/linear_regression#initmodelwithweights9)
|
|
|
* [computeError/6](/PrologMethods/Regression/linear_regression#computeerror6)
|
|
|
* [parameters/2](/PrologMethods/Regression/linear_regression#parameters2)
|
|
|
* [modifyParameters/2](/PrologMethods/Regression/linear_regression#modifyparameters2)
|
|
|
* [predict/5](/PrologMethods/Regression/linear_regression#predict5)
|
|
|
* [train/7](/PrologMethods/Regression/linear_regression#train7)
|
|
|
* [trainWithWeights/9](/PrologMethods/Regression/linear_regression#trainwithweights9)
|
|
|
|
|
|
---
|
|
|
|
|
|
[links/resources](/PrologMethods/Regression/linear_regression#connected-linksresources)
|
|
|
|
|
|
## **_initModel/7_**
|
|
|
|
|
|
Initializes the linear_regression model and trains it but doesnt include weights.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
initModel( +pointer(float_array), +integer, +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
+float32,
|
|
|
+integer).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| data | +matrix | X, matrix of data points. | - |
|
|
|
| responses | +vector | y, the measured data for each point in X. | - |
|
|
|
| lambda | +float | Regularization constant for ridge regression. | 0.0 |
|
|
|
| intercept | +integer(bool) | Whether or not to include an intercept term. | (1)true |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_initModelWithWeights/9_**
|
|
|
|
|
|
Initializes the linear_regression model, trains it and adds weights to it.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
initModelWithWeights( +pointer(float_array), +integer, +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
+float32,
|
|
|
+integer).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| data | +matrix | X, matrix of data points. | - |
|
|
|
| responses | +vector | y, the measured data for each point in X. | - |
|
|
|
| weights | +vector | Observation weights (for boosting). | - |
|
|
|
| lambda | +float | Regularization constant for ridge regression. | 0.0 |
|
|
|
| intercept | +integer(bool) | Whether or not to include an intercept term. | (1)true |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_computeError/6_**
|
|
|
|
|
|
Calculate the L2 squared error on the given predictors and responses using this linear regression model.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
computeError( +pointer(float_array), +integer, +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
[-float32]).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| points | +matrix | Matrix of predictors (X). | - |
|
|
|
| responses | +vector | Transposed vector of responses (y^T). | - |
|
|
|
| error | -float | As this number decreases to 0, the linear regression fit is better. | - |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_parameters/2_**
|
|
|
|
|
|
Get the parameters (the b vector).
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
parameters([-pointer(float_array)], -integer).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| parameters | -vector | Get the parameters (the b vector). | - |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_modifyParameters/2_**
|
|
|
|
|
|
Modify the parameters (the b vector).
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
modifyParameters(+pointer(float_array), +integer).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| newParameters | +vector | Modify the parameters (the b vector). | - |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_predict/5_**
|
|
|
|
|
|
Calculate y_i for each data point in points.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
predict( +pointer(float_array), +integer, +integer,
|
|
|
-pointer(float_array), -integer).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| points | +matrix | the data points to calculate with. | - |
|
|
|
| predictions | -vector | y, will contain calculated values on completion. | - |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_train/7_**
|
|
|
|
|
|
Train the linear_regression model on the given data.
|
|
|
|
|
|
Careful! This will completely ignore and overwrite the existing model. This particular implementation does not have an incremental training algorithm.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
train( +pointer(float_array), +integer, +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
+integer,
|
|
|
[-float32]).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| data | +matrix | X, matrix of data points. | - |
|
|
|
| responses | +vector | y, the measured data for each point in X. | - |
|
|
|
| intercept | +integer(bool) | Whether or not to include an intercept term. | (1)true |
|
|
|
| error | -float | The least quares error after training. | - |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_trainWithWeights/9_**
|
|
|
|
|
|
Train the linear_regression model on the given data and weights.
|
|
|
|
|
|
Careful! This will completely ignore and overwrite the existing model. This particular implementation does not have an incremental training algorithm.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
trainWithWeights( +pointer(float_array), +integer, +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
+integer,
|
|
|
[-float32]).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| data | +matrix | X, matrix of data points. | - |
|
|
|
| responses | +vector | y, the measured data for each point in X. | - |
|
|
|
| weights | +vector | Observation weights (for boosting). | - |
|
|
|
| intercept | +integer(bool) | Whether or not to include an intercept term. | (1)true |
|
|
|
| error | -float | The least quares error after training. | - |
|
|
|
|
|
|
---
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
* [MLpack::linear_regression_C++\_documentation](https://www.mlpack.org/doc/stable/doxygen/classmlpack_1_1regression_1_1LinearRegression.html)
|
|
|
* [MLpack::linear_regression_Python_documentation](https://www.mlpack.org/doc/stable/python_documentation.html#linear_regression)
|
|
|
|
|
|
added some of the links from the python documentation
|
|
|
|
|
|
* [lars](https://gitlab.cs.uni-duesseldorf.de/stups/abschlussarbeiten/prolog-mlpack-libary/-/wikis/PrologMethods/Regression/lars)
|
|
|
* [Linear regression on Wikipedia](https://en.wikipedia.org/wiki/Linear_regression) |
|
|
\ No newline at end of file |