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

Add support for fit from list #4

Merged
merged 1 commit into from
Oct 16, 2023
Merged
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
3 changes: 3 additions & 0 deletions src/gsd/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def fit_moments(data: ArrayLike) -> GSDParams:
:param data: A 5d Array of counts of each response.
:return: GSD Parameters
"""

data= jnp.asarray(data)
psi = jnp.dot(data, jnp.arange(1, 6)) / jnp.sum(data)
V = jnp.dot(data, jnp.arange(1, 6) ** 2) / jnp.sum(data) - psi ** 2
return GSDParams(psi=psi, rho=(vmax(psi) - V) / (vmax(psi) - vmin(psi)))
Expand Down Expand Up @@ -65,6 +67,7 @@ def fit_mle(data: ArrayLike, max_iterations: int = 100, log_lr_min: ArrayLike =
:return: An opt state whore params filed contains estimated values of GSD Parameters
"""

data = jnp.asarray(data)
def ll(theta: GSDParams) -> Array:
logits = jax.vmap(log_prob, (None, None, 0), (0))(theta.psi, theta.rho, jnp.arange(1, 6))
return jnp.dot(data, logits) / jnp.sum(data)
Expand Down
8 changes: 8 additions & 0 deletions tests/fit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def test_mle(self):
self.assertAlmostEqual(os.params.rho, 1)


def test_list(self):
# 1 2 3 4 5
data=[0,10,10,0,0.]
_,os = gsd.fit.fit_mle(data)
self.assertAlmostEqual(os.params.psi, 2.5)
self.assertAlmostEqual(os.params.rho, 1)



if __name__ == '__main__':
unittest.main()