MCPcopy Create free account
hub / github.com/ml-explore/mlx / test_multivariate_normal

Method test_multivariate_normal

python/tests/test_random.py:112–200  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

110 self.assertTrue(mx.all(a < mx.inf))
111
112 def test_multivariate_normal(self):
113 key = mx.random.key(0)
114 mean = mx.array([0, 0])
115 cov = mx.array([[1, 0], [0, 1]])
116
117 a = mx.random.multivariate_normal(mean, cov, key=key, stream=mx.cpu)
118 self.assertEqual(a.shape, (2,))
119
120 ## Check dtypes
121 for t in [mx.float32]:
122 a = mx.random.multivariate_normal(
123 mean, cov, dtype=t, key=key, stream=mx.cpu
124 )
125 self.assertEqual(a.dtype, t)
126 for t in [
127 mx.int8,
128 mx.int32,
129 mx.int64,
130 mx.uint8,
131 mx.uint32,
132 mx.uint64,
133 mx.float16,
134 mx.bfloat16,
135 ]:
136 with self.assertRaises(ValueError):
137 mx.random.multivariate_normal(
138 mean, cov, dtype=t, key=key, stream=mx.cpu
139 )
140
141 ## Check incompatible shapes
142 with self.assertRaises(ValueError):
143 mean = mx.zeros((2, 2))
144 cov = mx.zeros((2, 2))
145 mx.random.multivariate_normal(mean, cov, shape=(3,), key=key, stream=mx.cpu)
146
147 with self.assertRaises(ValueError):
148 mean = mx.zeros((2))
149 cov = mx.zeros((2, 2, 2))
150 mx.random.multivariate_normal(mean, cov, shape=(3,), key=key, stream=mx.cpu)
151
152 with self.assertRaises(ValueError):
153 mean = mx.zeros((3,))
154 cov = mx.zeros((2, 2))
155 mx.random.multivariate_normal(mean, cov, key=key, stream=mx.cpu)
156
157 with self.assertRaises(ValueError):
158 mean = mx.zeros((2,))
159 cov = mx.zeros((2, 3))
160 mx.random.multivariate_normal(mean, cov, key=key, stream=mx.cpu)
161
162 ## Different shape of mean and cov
163 mean = mx.array([[0, 7], [1, 2], [3, 4]])
164 cov = mx.array([[1, 0.5], [0.5, 1]])
165 a = mx.random.multivariate_normal(mean, cov, shape=(4, 3), stream=mx.cpu)
166 self.assertEqual(a.shape, (4, 3, 2))
167
168 ## Check correcteness of the mean and covariance
169 n_test = int(1e5)

Callers

nothing calls this directly

Calls 1

arrayMethod · 0.60

Tested by

no test coverage detected