Skip to main content

Build Warped Linear Regression Models

Project description

peak-engines is a Python package for building Warped Linear Regression models.

Warped Linear Regression is like Ordinary Least Squares but with an extra transformation step where target values are remapped using a parameterized monotonic function and adjusted so as to maximize likelihood on a linear model. The transformation makes Warped Linear Regression more general purpose than Ordinary Least Squares and able to fit models with non-normal error distributions.

For more details on the math behind Warped Linear Regression models see What to Do When Your Model Has a Non-Normal Error Distribution.

Installation

pip install peak-engines

Getting started

Load an example dataset

from sklearn.datasets import load_boston
X, y = load_boston(return_X_y=True)

Split out training and testing portions

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
  X, y, test_size=0.1, random_state=0)

Fit a Warped Linear Regression Model

import peak_engines
model = peak_engines.WarpedLinearRegressionModel()
model.fit(X_train, y_train)

Visualize the warping function

import numpy as np
import matplotlib.pyplot as plt
y_range = np.arange(np.min(y), np.max(y), 0.01)
z = model.warper_.compute_latent(y_range)
plt.plot(y_range, z)
plt.xlabel('Median Housing Value in $1000s')
plt.ylabel('Latent Variable')
plt.scatter(y, model.warper_.compute_latent(y))
Warping Function

Make predictions

from sklearn.metrics import mean_squared_error
y_pred = model.predict(X_test)
print(mean_squared_error(y_test, y_pred))
41.56037297819257

Compute and plot the error distribution of a prediction

logpdf = model.predict_logpdf([X_test[0]])
def pdf(yi):
    return np.exp(logpdf([yi]))
plt.plot(y_range, [pdf(yi) for yi in y_range])
plt.axvline(y_test[0])
plt.xlabel('Median Housing Value in $1000s')
plt.ylabel('Probability Density')
Error Distribution

Examples

Documentation

See doc/Reference.pdf.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

peak_engines-0.2.0-cp32-abi3-manylinux1_x86_64.whl (16.0 MB view hashes)

Uploaded CPython 3.2+

peak_engines-0.2.0-cp32-abi3-macosx_10_9_intel.whl (14.3 MB view hashes)

Uploaded CPython 3.2+ macOS 10.9+ intel

peak_engines-0.2.0-cp27-cp27mu-manylinux1_x86_64.whl (16.0 MB view hashes)

Uploaded CPython 2.7mu

peak_engines-0.2.0-cp27-cp27mu-macosx_10_9_intel.whl (14.3 MB view hashes)

Uploaded CPython 2.7mu macOS 10.9+ intel

peak_engines-0.2.0-cp27-cp27m-manylinux1_x86_64.whl (16.0 MB view hashes)

Uploaded CPython 2.7m

peak_engines-0.2.0-cp27-cp27m-macosx_10_9_intel.whl (14.3 MB view hashes)

Uploaded CPython 2.7m macOS 10.9+ intel

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page