Quick Start

To demonstrate some of the features of the spines package we’ll begin by constructing a simple OLS regression model.

Creating a Model

First we’ll import the libraries we’ll need, in our case here just numpy and spines:

import numpy as np

from spines import Model, Parameter

Now we’ll construct the OLS Regression model class:

class OLSRegression(Model):
    OLS Regression model
    betas = Parameter(np.ndarray)
    intercept = Parameter(bool, default=False)

    def fit(self, X, y):
        """Fits the model"""
        if self.intercept:
            X = np.hstack((X, np.full((X.shape[0], 1), 1.0)))
        self.betas = np.matmul(np.matmul(np.linalg.inv(np.matmul(X.T, X)), X.T), y)

    def predict(self, X):
        return np.matmul(X, self.betas)

    def error(self, X, y):
        y_hat = self.predict(X)
        return np.mean((y-y_hat)**2.0)

Let’s generate some random, slightly noisy data to fit the model with:

X = np.random.rand(100, 3)
y = (X * np.array([1.0, 0.5, 2.0])).sum(axis=1)
X += np.random.normal(scale=0.01, size=X.shape)
y += np.random.normal(scale=0.05, size=y.shape)

Now we can create our model instance and fit it:

ols_model = OLSRegression()
ols_model.fit(X, y)

The results:

<ParameterStore final=True> {
  <Parameter betas [type=ndarray] (required)>: [0.99080627 0.51589418 2.00767143],
  <Parameter intercept [type=bool]>: False,
ols_model.error(X, y)