psyphy 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- psyphy/__init__.py +126 -0
- psyphy/data/__init__.py +15 -0
- psyphy/data/dataset.py +303 -0
- psyphy/data/io.py +92 -0
- psyphy/inference/__init__.py +39 -0
- psyphy/inference/base.py +47 -0
- psyphy/inference/langevin.py +89 -0
- psyphy/inference/laplace.py +56 -0
- psyphy/inference/map_optimizer.py +245 -0
- psyphy/model/__init__.py +46 -0
- psyphy/model/base.py +77 -0
- psyphy/model/covariance_field.py +518 -0
- psyphy/model/likelihood.py +701 -0
- psyphy/model/noise.py +44 -0
- psyphy/model/prior.py +215 -0
- psyphy/model/wppm.py +531 -0
- psyphy/posterior/__init__.py +34 -0
- psyphy/posterior/parameter_posterior.py +87 -0
- psyphy/posterior/posterior.py +75 -0
- psyphy/posterior/predictive_posterior.py +287 -0
- psyphy/trial_placement/__init__.py +38 -0
- psyphy/trial_placement/grid.py +56 -0
- psyphy/trial_placement/sobol.py +65 -0
- psyphy/utils/__init__.py +19 -0
- psyphy/utils/math.py +104 -0
- psyphy-0.0.2.dist-info/METADATA +139 -0
- psyphy-0.0.2.dist-info/RECORD +29 -0
- psyphy-0.0.2.dist-info/WHEEL +4 -0
- psyphy-0.0.2.dist-info/licenses/LICENSE.md +21 -0
psyphy/model/noise.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
import jax
|
|
6
|
+
import jax.random as jr
|
|
7
|
+
|
|
8
|
+
"""
|
|
9
|
+
Noise models for observer's internal representations.
|
|
10
|
+
|
|
11
|
+
Implements:
|
|
12
|
+
- GaussianNoise: default (ellipsoidal isoperformance contours)
|
|
13
|
+
- StudentTNoise: heavier-tailed observer variability
|
|
14
|
+
- Extensible for other families (e.g., skewed)
|
|
15
|
+
|
|
16
|
+
NoiseModel objects plug into WPPM to define trial likelihoods.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class GaussianNoise:
|
|
22
|
+
sigma: float = 1.0
|
|
23
|
+
|
|
24
|
+
def log_prob(self, residual: float) -> float:
|
|
25
|
+
_ = residual
|
|
26
|
+
return -0.5
|
|
27
|
+
|
|
28
|
+
def sample_standard(self, key: jax.Array, shape: tuple[int, ...]) -> jax.Array:
|
|
29
|
+
"""Sample from standard Gaussian (mean=0, var=1)."""
|
|
30
|
+
return jr.normal(key, shape)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass
|
|
34
|
+
class StudentTNoise:
|
|
35
|
+
df: float = 3.0
|
|
36
|
+
scale: float = 1.0
|
|
37
|
+
|
|
38
|
+
def log_prob(self, residual: float) -> float:
|
|
39
|
+
_ = residual
|
|
40
|
+
return -0.5
|
|
41
|
+
|
|
42
|
+
def sample_standard(self, key: jax.Array, shape: tuple[int, ...]) -> jax.Array:
|
|
43
|
+
"""Sample from standard Student-t (df=self.df)."""
|
|
44
|
+
return jr.t(key, self.df, shape)
|
psyphy/model/prior.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""
|
|
2
|
+
prior.py
|
|
3
|
+
--------
|
|
4
|
+
|
|
5
|
+
Prior distributions for WPPM parameters
|
|
6
|
+
|
|
7
|
+
Hyperparameters:
|
|
8
|
+
* variance_scale : global scaling factor for covariance magnitude
|
|
9
|
+
* decay_rate : smoothness controlling spatial variation
|
|
10
|
+
* extra_embedding_dims : embedding dimension for basis expansions
|
|
11
|
+
|
|
12
|
+
Connections
|
|
13
|
+
-----------
|
|
14
|
+
- WPPM calls Prior.sample_params() to initialize model parameters
|
|
15
|
+
- WPPM adds Prior.log_prob(params) to task log-likelihoods to form the log posterior
|
|
16
|
+
- Prior will generate structured parameters for basis expansions
|
|
17
|
+
and decay_rate-controlled smooth covariance fields
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from dataclasses import dataclass
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
import jax.numpy as jnp
|
|
26
|
+
import jax.random as jr
|
|
27
|
+
|
|
28
|
+
Params = dict[str, jnp.ndarray]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass
|
|
32
|
+
class Prior:
|
|
33
|
+
"""
|
|
34
|
+
Prior distribution over WPPM parameters
|
|
35
|
+
|
|
36
|
+
Parameters
|
|
37
|
+
----------
|
|
38
|
+
input_dim : int
|
|
39
|
+
Dimensionality of the model space (same as WPPM.input_dim)
|
|
40
|
+
basis_degree : int | None, default=None
|
|
41
|
+
Degree of Chebyshev basis for Wishart process.
|
|
42
|
+
If set, uses Wishart mode with W coefficients.
|
|
43
|
+
variance_scale : float, default=1.0
|
|
44
|
+
Prior variance for degree-0 (constant) coefficient in Wishart mode.
|
|
45
|
+
Controls overall scale of covariances.
|
|
46
|
+
decay_rate : float, default=0.5
|
|
47
|
+
Geometric decay rate for prior variance over higher-degree coefficients.
|
|
48
|
+
Prior variance for degree-d coefficient = variance_scale * (decay_rate^d).
|
|
49
|
+
Smaller decay_rate -> stronger smoothness prior.
|
|
50
|
+
extra_embedding_dims : int, default=0
|
|
51
|
+
Additional latent dimensions in U matrices beyond input dimensions.
|
|
52
|
+
Allows richer ellipsoid shapes in Wishart mode.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
input_dim: int = 2
|
|
56
|
+
basis_degree: int = 4 # | None = None
|
|
57
|
+
variance_scale: float = 4e-3
|
|
58
|
+
decay_rate: float = 0.4
|
|
59
|
+
extra_embedding_dims: int = 1
|
|
60
|
+
|
|
61
|
+
def __post_init__(self):
|
|
62
|
+
"""Validate and normalize parameters."""
|
|
63
|
+
if self.basis_degree is not None and self.basis_degree < 0:
|
|
64
|
+
raise ValueError("basis_degree must be non-negative or None")
|
|
65
|
+
|
|
66
|
+
def _compute_basis_degree_grid(self) -> jnp.ndarray:
|
|
67
|
+
"""
|
|
68
|
+
Compute total degree for each basis function coefficient.
|
|
69
|
+
|
|
70
|
+
For 2D: basis functions are products φ_i(x_1) * φ_j(x_2)
|
|
71
|
+
Total degree of φ_ij is i + j.
|
|
72
|
+
|
|
73
|
+
Note: For basis_degree=d, we have (d+1) basis functions [T_0, ..., T_d] per dimension,
|
|
74
|
+
so the grid has shape (d+1, d+1) for 2D.
|
|
75
|
+
|
|
76
|
+
Returns
|
|
77
|
+
-------
|
|
78
|
+
basis_degrees : jnp.ndarray
|
|
79
|
+
Array of total degrees, shape (degree+1, degree+1) for 2D
|
|
80
|
+
"""
|
|
81
|
+
if self.basis_degree is None:
|
|
82
|
+
return jnp.array([])
|
|
83
|
+
|
|
84
|
+
if self.input_dim == 2:
|
|
85
|
+
# 2D: degree[i,j] = i + j for i,j in 0..basis_degree
|
|
86
|
+
basis_degrees = (
|
|
87
|
+
jnp.arange(self.basis_degree + 1)[:, None]
|
|
88
|
+
+ jnp.arange(self.basis_degree + 1)[None, :]
|
|
89
|
+
)
|
|
90
|
+
elif self.input_dim == 3:
|
|
91
|
+
# 3D: degree[i,j,k] = i + j + k
|
|
92
|
+
basis_degrees = (
|
|
93
|
+
jnp.arange(self.basis_degree + 1)[:, None, None]
|
|
94
|
+
+ jnp.arange(self.basis_degree + 1)[None, :, None]
|
|
95
|
+
+ jnp.arange(self.basis_degree + 1)[None, None, :]
|
|
96
|
+
)
|
|
97
|
+
else:
|
|
98
|
+
raise NotImplementedError(
|
|
99
|
+
f"Wishart process only supports 2D and 3D. Got input_dim={self.input_dim}"
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
return basis_degrees
|
|
103
|
+
|
|
104
|
+
def _compute_W_prior_variances(self) -> jnp.ndarray:
|
|
105
|
+
"""
|
|
106
|
+
Compute prior variances for W coefficients with smoothness prior.
|
|
107
|
+
|
|
108
|
+
Prior variance = variance_scale * (decay_rate^total_degree)
|
|
109
|
+
This enforces smoothness: higher frequency components have lower variance.
|
|
110
|
+
|
|
111
|
+
Returns
|
|
112
|
+
-------
|
|
113
|
+
variances : jnp.ndarray
|
|
114
|
+
Prior variances, shape matches basis degree grid
|
|
115
|
+
"""
|
|
116
|
+
basis_degrees = self._compute_basis_degree_grid()
|
|
117
|
+
return self.variance_scale * (self.decay_rate**basis_degrees)
|
|
118
|
+
|
|
119
|
+
def sample_params(self, key: Any) -> Params:
|
|
120
|
+
"""
|
|
121
|
+
Sample initial parameters from the prior.
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
Returns {"W": shape (degree+1, degree+1, input_dim, embedding_dim)}
|
|
125
|
+
for 2D, where embedding_dim = input_dim + extra_embedding_dims
|
|
126
|
+
|
|
127
|
+
Note: The 3rd dimension is input_dim (output space dimension).
|
|
128
|
+
This matches the einsum in _compute_sqrt:
|
|
129
|
+
U = einsum("ijde,ij->de", W, phi) where d indexes input_dim.
|
|
130
|
+
|
|
131
|
+
Parameters
|
|
132
|
+
----------
|
|
133
|
+
key : JAX random key
|
|
134
|
+
|
|
135
|
+
Returns
|
|
136
|
+
-------
|
|
137
|
+
params : dict
|
|
138
|
+
Parameter dictionary
|
|
139
|
+
"""
|
|
140
|
+
if self.basis_degree is None:
|
|
141
|
+
raise ValueError(
|
|
142
|
+
"'basis_degree' is None; please set "
|
|
143
|
+
"`Prior.basis_degree` to an integer >0."
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
# Basis function coefficients W
|
|
147
|
+
variances = self._compute_W_prior_variances()
|
|
148
|
+
embedding_dim = self.input_dim + self.extra_embedding_dims
|
|
149
|
+
|
|
150
|
+
if self.input_dim == 2:
|
|
151
|
+
# Sample W ~ Normal(0, variances) for each matrix entry
|
|
152
|
+
# Shape: (degree+1, degree+1, input_dim, embedding_dim)
|
|
153
|
+
# Note: degree+1 to match number of basis functions [T_0, ..., T_degree]
|
|
154
|
+
W = jnp.sqrt(variances)[:, :, None, None] * jr.normal(
|
|
155
|
+
key,
|
|
156
|
+
shape=(
|
|
157
|
+
self.basis_degree + 1,
|
|
158
|
+
self.basis_degree + 1,
|
|
159
|
+
self.input_dim,
|
|
160
|
+
embedding_dim,
|
|
161
|
+
),
|
|
162
|
+
)
|
|
163
|
+
elif self.input_dim == 3:
|
|
164
|
+
# Shape: (degree+1, degree+1, degree+1, input_dim, embedding_dim)
|
|
165
|
+
W = jnp.sqrt(variances)[:, :, :, None, None] * jr.normal(
|
|
166
|
+
key,
|
|
167
|
+
shape=(
|
|
168
|
+
self.basis_degree + 1,
|
|
169
|
+
self.basis_degree + 1,
|
|
170
|
+
self.basis_degree + 1,
|
|
171
|
+
self.input_dim,
|
|
172
|
+
embedding_dim,
|
|
173
|
+
),
|
|
174
|
+
)
|
|
175
|
+
else:
|
|
176
|
+
raise NotImplementedError(
|
|
177
|
+
f"Wishart process only supports 2D and 3D. Got input_dim={self.input_dim}"
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
return {"W": W}
|
|
181
|
+
|
|
182
|
+
def log_prob(self, params: Params) -> jnp.ndarray:
|
|
183
|
+
"""
|
|
184
|
+
Compute log prior density (up to a constant)
|
|
185
|
+
|
|
186
|
+
Gaussian prior on W with smoothness via decay_rate
|
|
187
|
+
log p(W) = Σ_ij log N(W_ij | 0, σ_ij^2) where σ_ij^2 = prior variance
|
|
188
|
+
|
|
189
|
+
Parameters
|
|
190
|
+
----------
|
|
191
|
+
params : dict
|
|
192
|
+
Parameter dictionary
|
|
193
|
+
|
|
194
|
+
Returns
|
|
195
|
+
-------
|
|
196
|
+
log_prob : float
|
|
197
|
+
Log prior probability (up to normalizing constant)
|
|
198
|
+
"""
|
|
199
|
+
|
|
200
|
+
if "W" in params:
|
|
201
|
+
# Wishart mode
|
|
202
|
+
W = params["W"]
|
|
203
|
+
variances = self._compute_W_prior_variances()
|
|
204
|
+
|
|
205
|
+
# Gaussian log probability for each entry
|
|
206
|
+
# log N(x | 0, σ^2) = -0.5 * (x^2/σ^2 + log(2πσ^2))
|
|
207
|
+
# Up to constant: -0.5 * x^2/σ^2
|
|
208
|
+
|
|
209
|
+
if self.input_dim == 2:
|
|
210
|
+
# Each W[i,j,:,:] ~ Normal(0, variance[i,j] * I)
|
|
211
|
+
return -0.5 * jnp.sum((W**2) / (variances[:, :, None, None] + 1e-10))
|
|
212
|
+
elif self.input_dim == 3:
|
|
213
|
+
return -0.5 * jnp.sum((W**2) / (variances[:, :, :, None, None] + 1e-10))
|
|
214
|
+
|
|
215
|
+
raise ValueError("params must contain weights 'W'")
|