About find log_likelihood and log_posterior

Hi,

Recently, i use bayesflow.amortizers.AmortizedPosteriorLikelihood to find the log_likelihood and log_posterior.

For example, i have observed data 5, and i want to find the log_posterior and log_likelihood at 3. I am a little bit confused about how to put those value in the parameter.

I use log_likelihood({“observables”:5,“conditions”:3}) and log_posterior({“parameters”:3,“summary_conditions”: 5}) but the value of outcome looks incorrect. i think i may misunderstand the input dic in this page.

https://bayesflow.org/api/bayesflow.amortizers.html#bayesflow.amortizers.AmortizedPosteriorLikelihood.log_likelihood

May i ask whether i write those input in the right place.
Thanks so much.

Best
wyy

Hi Yuyan, the usage of these functions is the same as you would have it when passing the same data through the networks. For instance, take a look at the last code cell where the function is used:

https://bayesflow.org/_examples/TwoMoons_Bimodal_Posterior.html

Of course, you cannot just pass scalars; everything needs to be a tensor with at least 2 dimensions (i.e., a scalar should be a tensor of shape [1, 1] and so on).

Dear Sir,

Thank you very much for your assistance. I find myself a bit confused regarding the two parameters “summary_conditions” and “direct_conditions.” Can I interpret them as follows: the observed value is the one that does not need to be routed through the summary network, so we place it under “direct_conditions”?

When writing the code, I placed the observed data under “direct_conditions,” but encountered an error. While this value can be placed under other parameters, it seems to cause an error when placed here. Is there any special requirement or consideration for this parameter?

Thank you for your help in clarifying this matter.


Sincerely,
wyy

“Can I interpret them as follows: the observed value is the one that does not need to be routed through the summary network, so we place it under “direct_conditions”?”

That is correct. If you haven’t defined your own configurator, the data is automatically placed under “summary_conditions”. So whenever you have summary conditions, you need to populate this dictionary key, because the summary network will search for it.

Dear sir,

Thank you very much for your assistance. It has provided me with greater clarity, enabling me to identify the issue in my code. I have two questions regarding this matter:

  1. I aim to calculate the log_posterior at point x. In my code, I have defined a summary network for training the model, and I also have observation value (call it obs) . so i should compute the log_posterior as follows: log_posterior({“parameters”: x, “summary_conditions”: obs}). otherwise it will have error, since i have define the summary network, so the only way is put the obs value under summary_conditions.
    My objective is solely to determine the log_posterior at x, given the obs.

  2. When calculating the log_likelihood at point x with observation value (call it obs), this API contains only two parameters. Thus, I write it in the following manner: log_likelihood({“observables”: obs, “conditions”: x}). Yet, I find myself somewhat perplexed. Upon reviewing the GitHub documentation, it states the following:

  • Observables: The variables over which a condition density is learned (i.e., the observables).
  • Conditions: The conditioning variables that are directly passed to the inference network.
    Given that my obs values seem to belong in the observables category,
    but i do not have parameter shoud be the conditioning variables that are directly passed to the inference network ?(equivalent to we don’t have direct_conditions in Q1)?
    how should I arrange x and obs when utilizing the log_likelihood function?

Thank you for your attention to these matters.

Thanks so much
wyy