Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Jean-Baptiste Delisle
samsam
Commits
7254cf35
Commit
7254cf35
authored
Mar 29, 2021
by
Jean-Baptiste Delisle
Browse files
add tests
parent
b8fbd175
Pipeline
#24835
failed with stages
in 4 minutes and 26 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
test/test_sam.py
0 → 100644
View file @
7254cf35
import
pytest
import
numpy
as
np
from
sam
import
sam
,
covis
,
logprior
,
acf
n
=
4
nsamples
=
100000
def
generate_mucov
(
seed
=
0
):
np
.
random
.
seed
(
seed
)
mu
=
np
.
random
.
normal
(
0
,
10
,
n
)
cov
=
np
.
identity
(
n
)
+
np
.
random
.
normal
(
0
,
1e-2
,
n
**
2
).
reshape
((
n
,
n
))
cov
=
(
cov
+
cov
.
T
)
/
2
return
(
mu
,
cov
)
def
logprob
(
x
,
x_mu
,
x_cov
):
r
=
x
-
x_mu
return
(
-
0.5
*
(
r
@
np
.
linalg
.
inv
(
x_cov
)
@
r
+
np
.
linalg
.
slogdet
(
2
*
np
.
pi
*
x_cov
)[
1
]))
def
test_sam
():
mu
,
cov
=
generate_mucov
()
x0
=
np
.
zeros
(
n
)
samples
,
_
=
sam
(
x0
,
logprob
,
nsamples
=
nsamples
,
x_mu
=
mu
,
x_cov
=
cov
)
samples
=
samples
[
nsamples
//
4
::
10
]
samp_mu
=
np
.
mean
(
samples
,
axis
=
0
)
samp_cov
=
np
.
cov
(
samples
,
rowvar
=
False
)
assert
np
.
max
(
np
.
abs
(
samp_mu
-
mu
))
<
0.1
assert
np
.
max
(
np
.
abs
(
samp_cov
-
cov
))
<
0.1
def
test_covis
():
mu
,
cov
=
generate_mucov
()
mu0
=
mu
+
np
.
random
.
normal
(
0
,
0.1
,
n
)
cov0
=
0.9
*
cov
+
np
.
random
.
normal
(
0
,
1e-3
,
n
**
2
).
reshape
((
n
,
n
))
cov0
=
(
cov0
+
cov0
.
T
)
/
2
_
,
_
,
diags
=
covis
(
mu0
,
cov0
,
logprob
,
nsamples
=
nsamples
,
x_mu
=
mu
,
x_cov
=
cov
)
assert
abs
(
diags
[
'logevidence'
])
<
1e-3
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment