-
Notifications
You must be signed in to change notification settings - Fork 112
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
Use VB estimates as initial values for MCMC sampling by default #96
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
523c132
Make it possible to use VB estimates as initial values
JaeyeongYang 2928934
Set VB estimates as initial values by default
JaeyeongYang dec556d
Implement to use VB estimates as initial values
JaeyeongYang 759078b
Use VB estimates as initial values by default for Python
JaeyeongYang 17c66b6
Merge branch 'develop' into feature/use-vb-as-initial-values
JaeyeongYang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
|
||
|
||
class TaskModel(metaclass=ABCMeta): | ||
"""HBayesDM TaskModel Base Class. | ||
"""hBayesDM TaskModel Base Class. | ||
|
||
The base class that is inherited by all hBayesDM task-models. Child classes | ||
should implement (i.e. override) the abstract method: `_preprocess_func`. | ||
|
@@ -165,7 +165,12 @@ def _run(self, | |
data_dict = self._preprocess_func( | ||
raw_data, general_info, additional_args) | ||
pars = self._prepare_pars(model_regressor, inc_postpred) | ||
gen_init = self._prepare_gen_init(inits, general_info['n_subj']) | ||
|
||
n_subj = general_info['n_subj'] | ||
if inits == 'vb': | ||
gen_init = self._prepare_gen_init_vb(data_dict, n_subj) | ||
else: | ||
gen_init = self._prepare_gen_init(inits, n_subj) | ||
|
||
model = self._get_model_full_name() | ||
ncore = self._set_number_of_cores(ncore) | ||
|
@@ -423,16 +428,68 @@ def _prepare_pars(self, model_regressor: bool, inc_postpred: bool) -> List: | |
pars += self.postpreds | ||
return pars | ||
|
||
def _prepare_gen_init_vb(self, | ||
data_dict: Dict, | ||
n_subj: int, | ||
) -> Union[str, Callable]: | ||
"""Prepare initial values for the parameters using Variational Bayesian | ||
methods. | ||
|
||
Parameters | ||
---------- | ||
data_dict | ||
Dict holding the data to pass to Stan. | ||
n_subj | ||
Total number of subjects in data. | ||
|
||
Returns | ||
------- | ||
gen_init : Union[str, Callable] | ||
A function that returns initial values for each parameter, based on | ||
the variational Bayesian method. | ||
""" | ||
model = self._get_model_full_name() | ||
sm = self._designate_stan_model(model) | ||
|
||
try: | ||
fit = sm.vb(data=data_dict) | ||
except Exception: | ||
raise RuntimeError( | ||
'Failed to get VB estimates for initial values. ' | ||
'Please re-run the code to try fitting model with VB.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JaeyeongYang |
||
|
||
len_param = len(self.parameters) | ||
dict_vb = { | ||
k: v | ||
for k, v in zip(fit['mean_par_names'], fit['mean_pars']) | ||
if k.startswith('sigma[') or '_pr[' in k | ||
} | ||
|
||
dict_init = {} | ||
dict_init['mu_pr'] = \ | ||
[dict_vb['mu_pr[%d]' % (i + 1)] for i in range(len_param)] | ||
dict_init['sigma'] = \ | ||
[dict_vb['sigma[%d]' % (i + 1)] for i in range(len_param)] | ||
for p in self.parameters: | ||
dict_init['%s_pr' % p] = \ | ||
[dict_vb['%s_pr[%d]' % (p, i + 1)] for i in range(n_subj)] | ||
|
||
def gen_init(): | ||
return dict_init | ||
|
||
return gen_init | ||
|
||
def _prepare_gen_init(self, | ||
inits: Union[str, Sequence[float]], | ||
n_subj: int) -> Union[str, Callable]: | ||
n_subj: int, | ||
) -> Union[str, Callable]: | ||
"""Prepare initial values for the parameters. | ||
|
||
Parameters | ||
---------- | ||
inits | ||
User-defined inits. Can be the strings 'random' or 'fixed', or a | ||
list of float values to use as initial values for the parameters. | ||
User-defined inits. Can be the strings 'random' or 'fixed', | ||
or a list of float values to use as initial values for parameters. | ||
n_subj | ||
Total number of subjects in data. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JaeyeongYang
If you call
self._designate_stan_model(model)
within_prepare_gen_init_vb()
, then_designate_stan_model()
will end up being called twice within the entirety of_run()
. Because (the Stanmodel caching and) user prompting occurs in_designate_stan_model()
, the user will be prompted the line:'Using cached StanModel:', cache_file
a total of two times.