Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding sampling functionality to linearregression model #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [1.1.4] - 2018-07-21
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I'm not sure if you're familiar with semantic versioning; the last number is reserved for bug fixes.
This is not a bug fix, so the version should be a minor version change, e.g. 1.2.0

### Added
- sample method for LinearRegression model

## [1.1.3] - 2018-05-25
### Fixed
- HLR fit method sets shared vars if no minibatch_size given
Expand Down
4 changes: 2 additions & 2 deletions docs/api/modules.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
api
pymc3_models
============

.. toctree::
:maxdepth: 4

pymc3_models.models
pymc3_models
5 changes: 4 additions & 1 deletion docs/api/pymc3_models.models.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
models
pymc3\_models\.models package
=============================

Submodules
----------

pymc3\_models\.models\.HierarchicalLogisticRegression module
------------------------------------------------------------

Expand Down
29 changes: 29 additions & 0 deletions docs/api/pymc3_models.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pymc3\_models package
=====================

Subpackages
-----------

.. toctree::

pymc3_models.models

Submodules
----------

pymc3\_models\.exc module
-------------------------

.. automodule:: pymc3_models.exc
:members:
:undoc-members:
:show-inheritance:


Module contents
---------------

.. automodule:: pymc3_models
:members:
:undoc-members:
:show-inheritance:
25 changes: 21 additions & 4 deletions pymc3_models/models/LinearRegression.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ def fit(self, X, y, inference_type='advi', minibatch_size=None, inference_args=N

return self

def predict(self, X, return_std=False):

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only need one blank line between methods per PEP8.

def sample(self, X, samples=2000):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about calling this something like sample_posterior? I think it could help to be a bit more explicit.

"""
Predicts values of new data with a trained Linear Regression model
samples the conditional posterior estimates

Parameters
----------
X : numpy array, shape [n_samples, n_features]

return_std : Boolean flag of whether to return standard deviations with mean values. Defaults to False.
samples : number of draws to make for each point
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add in the default value to this docstring? Like I do in the predict method below with return_std.

"""

if self.trace is None:
raise PyMC3ModelsError('Run fit on the model before predict.')

Expand All @@ -116,6 +116,23 @@ def predict(self, X, return_std=False):

ppc = pm.sample_ppc(self.trace, model=self.cached_model, samples=2000)

return ppc

def predict(self, X, return_std=False, samples=2000):
"""
Predicts values of new data with a trained Linear Regression model

Parameters
----------
X : numpy array, shape [n_samples, n_features]

return_std : Boolean flag of whether to return standard deviations with mean values. Defaults to False.

samples: numberof draws to make for each input
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: number of. Please also add the default value.

"""

ppc = self.sample(X, samples)

if return_std:
return ppc['y'].mean(axis=0), ppc['y'].std(axis=0)
else:
Expand Down