-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add __repr__ and __len__ for Fit
Adds a minimally informative __repr__ for the Fit class. Also adds a simple __len__ which reports the number of parameters. Closes #41 Closes #92
- Loading branch information
1 parent
93612c4
commit a7d9290
Showing
4 changed files
with
65 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import stan | ||
|
||
np.random.seed(1) | ||
|
||
program_code = """ | ||
data { | ||
int<lower=0> N; | ||
int<lower=0> p; | ||
matrix[N,p] x; | ||
vector[N] y; | ||
} | ||
parameters { | ||
vector[p] beta; | ||
real<lower=0> sigma; | ||
} | ||
model { | ||
y ~ normal(x * beta, sigma); | ||
} | ||
""" | ||
|
||
n, p = 50, 3 # smaller n than in tests/test_linear_regression.py | ||
X = np.random.normal(size=(n, p)) | ||
X = (X - np.mean(X, axis=0)) / np.std(X, ddof=1, axis=0, keepdims=True) | ||
beta_true = (1, 3, 5) | ||
y = np.dot(X, beta_true) + np.random.normal(size=n) | ||
|
||
data = {"N": n, "p": p, "x": X, "y": y} | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def posterior(): | ||
return stan.build(program_code, data=data, random_seed=1) | ||
|
||
|
||
def test_repr_fit(posterior): | ||
fit = posterior.sample(num_chains=4) | ||
expected = """<stan.Fit>\nParameters:\n beta: (3,)\n sigma: ()\nDraws: 4000""" | ||
assert repr(fit) == expected |