-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored ModelResults into separate classes for different BackEnds
- Loading branch information
Showing
2 changed files
with
28 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,38 @@ | ||
import pandas as pd | ||
import pymc3 as pm | ||
from abc import abstractmethod, ABCMeta | ||
|
||
|
||
class ModelResults(object): | ||
|
||
def __init__(self, model, trace): | ||
__metaclass__ = ABCMeta | ||
|
||
def __init__(self, model): | ||
|
||
self.model = model | ||
self.terms = list(model.terms.values()) | ||
self.trace = trace | ||
self.diagnostics = model._diagnostics | ||
self.n_terms = len(model.terms) | ||
|
||
@abstractmethod | ||
def plot(self): | ||
pass | ||
|
||
@abstractmethod | ||
def summary(self): | ||
pass | ||
|
||
|
||
class PyMC3ModelResults(ModelResults): | ||
|
||
def __init__(self, model, trace): | ||
|
||
self.trace = trace | ||
self.n_samples = len(trace) | ||
self._fixed_terms = [t.name for t in self.terms if t.type_=='fixed'] | ||
self._random_terms = [t.name for t in self.terms if t.type_=='random'] | ||
|
||
def _select_samples(self, fixed, random, names, burn_in): | ||
trace = self.trace[burn_in:] | ||
if names is not None: | ||
names = [] | ||
if fixed: | ||
names.extend(self._fixed_terms) | ||
if random: | ||
names.extend(self._random_terms) | ||
return trace, names | ||
|
||
def plot_trace(self, burn_in=0, fixed=True, random=True, names=None, | ||
**kwargs): | ||
trace, names = self._select_samples(fixed, random, names, burn_in) | ||
return pm.traceplot(trace, varnames=names, **kwargs) | ||
|
||
def summary(self, burn_in=0, fixed=True, random=True, names=None, **kwargs): | ||
trace, names = self._select_samples(fixed, random, names, burn_in) | ||
return pm.summary(trace, varnames=names, **kwargs) | ||
|
||
def plot(self, burn_in=0, names=None, **kwargs): | ||
return pm.traceplot(trace[burn_in:], varnames=names, **kwargs) | ||
|
||
def summary(self, burn_in=0, fixed=True, random=True, names=None, | ||
**kwargs): | ||
return pm.summary(trace[burn_in:], varnames=names, **kwargs) |