Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ihmm_behav_states
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sebastian Bruijns
ihmm_behav_states
Commits
81148d84
Commit
81148d84
authored
2 years ago
by
SebastianBruijns
Browse files
Options
Downloads
Patches
Plain Diff
made gibbs_sample.py look pretty
parent
fb9c974e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test_codes/pymc_compare/gibbs_sample.py
+21
-10
21 additions, 10 deletions
test_codes/pymc_compare/gibbs_sample.py
with
21 additions
and
10 deletions
test_codes/pymc_compare/gibbs_sample.py
+
21
−
10
View file @
81148d84
"""
Perform a Gibbs sampling using my function of the test data.
Also perform maximum likelihood estimation of the same weights.
"""
import
numpy
as
np
import
pyhsmm.basic.distributions
as
distributions
from
scipy.optimize
import
minimize
import
pickle
n_samples
=
100000
# Data Params
T
=
16
n_inputs
=
3
step_size
=
0.2
# Sampling params
n_samples
=
100000
# Setup
Q
=
np
.
tile
(
np
.
eye
(
n_inputs
),
(
T
,
1
,
1
))
sample
=
pickle
.
load
(
open
(
'
test_data
'
,
'
rb
'
))
learn
=
distributions
.
Dynamic_GLM
(
n_inputs
=
n_inputs
,
T
=
T
,
P_0
=
4
*
np
.
eye
(
n_inputs
),
Q
=
Q
*
step_size
,
prior_mean
=
np
.
zeros
(
n_inputs
))
def
wrapper
(
w
,
t
):
learn
.
weights
=
np
.
tile
(
w
,
(
T
,
1
))
return
-
np
.
sum
(
learn
.
log_likelihood
(
sample
[
t
],
t
))
# Draw samples
samples
=
[]
pseudo_samples
=
[]
for
_
in
range
(
n_samples
):
if
_
%
1000
==
0
:
print
(
_
)
learn
.
resample
(
sample
)
samples
.
append
(
learn
.
weights
.
copy
())
def
wrapper
(
w
,
t
):
"""
Reshape weight vector w into the correct shape, then compute the max ll estimate for the desired time t.
"""
learn
.
weights
=
np
.
tile
(
w
,
(
T
,
1
))
return
-
np
.
sum
(
learn
.
log_likelihood
(
sample
[
t
],
t
))
# Compute max ll estimates
LL_weights
=
np
.
zeros
((
T
,
n_inputs
))
for
t
in
range
(
T
):
f
=
lambda
w
:
wrapper
(
w
,
t
)
LL_weights
[
t
]
=
minimize
(
f
,
np
.
zeros
(
n_inputs
)).
x
LL_weights
[
t
]
=
minimize
(
lambda
w
:
wrapper
(
w
,
t
),
np
.
zeros
(
n_inputs
)).
x
# save everything
pickle
.
dump
((
samples
,
LL_weights
),
open
(
'
gibbs_posterior
'
,
'
wb
'
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment