Source code for pynance.learn.linreg

.. Copyright (c) 2014, 2015 Marshall Farrier

Linear regression (:mod:`pynance.learn.linreg`)

.. currentmodule:: pynance.learn.linreg

import numpy as np

[docs]def run(features, labels, regularization=0., constfeat=True): """ Run linear regression on the given data. .. versionadded:: 0.5.0 If a regularization parameter is provided, this function is a simplification and specialization of ridge regression, as implemented in `scikit-learn <>`_. Setting `solver` to `'svd'` in :class:`sklearn.linear_model.Ridge` and equating our `regularization` with their `alpha` will yield the same results. Parameters ---------- features : ndarray Features on which to run linear regression. labels : ndarray Labels for the given features. Multiple columns of labels are allowed. regularization : float, optional Regularization parameter. Defaults to 0. constfeat : bool, optional Whether or not the first column of features is the constant feature 1. If True, the first column will be excluded from regularization. Defaults to True. Returns ------- model : ndarray Regression model for the given data. """ n_col = (features.shape[1] if len(features.shape) > 1 else 1) reg_matrix = regularization * np.identity(n_col, dtype='float64') if constfeat: reg_matrix[0, 0] = 0. # return np.linalg.lstsq( + reg_matrix,[0]
[docs]def predict(features, model): """ Generate predictions from features and model. .. versionadded:: 0.5.0 Parameters ---------- features : ndarray Features from which to generate predictions model : ndarray Regression model. Returns ------- predicted : ndarray Predictions generated from features using model. """ return