Integrate() to deal 2-dimension integration

54 views
Skip to first unread message

可可

unread,
Feb 8, 2025, 8:10:20 AM2/8/25
to Biogeme
Dear Michel, and all,
I set two latent variables when I calibrated the ICLV model. I want to use Integrate() to deal 2-dimension integration. However, I am unsure about the validity of the following code segment:

omega_1 = RandomVariable('omega_1')
omega_2 = RandomVariable('omega_2')
density_1 = dist.normalpdf(omega_1)
density_2 = dist.normalpdf(omega_2)
.......
.......
integral_1 = Integrate(condlike * density_1, 'omega_1')
integral_2 = Integrate(condlike * density_2, 'omega_2')
integral = integral_1 * integral_2
loglike = log(integral)

May I ask if this part of code is correct? My colleagues and I are very concerned about this problem and would appreciate your any response!!

Michel Bierlaire

unread,
Feb 9, 2025, 5:00:08 AM2/9/25
to kek3...@gmail.com, Michel Bierlaire, Biogeme
No, the correct way would be

first_integral = Integrate(condlike * density_1, 'omega_1')
second_integral = Integrate(first_integral * density_2, 'omega_2')

But if you have several integrals, use Monte-Carlo integration.

integral = MonteCarlo(condlike)
loglike = log(integral)
> --
> You received this message because you are subscribed to the Google Groups "Biogeme" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to biogeme+u...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/biogeme/eb4b0373-5762-44bf-a8b2-401402e11786n%40googlegroups.com.

Michel Bierlaire
Transport and Mobility Laboratory
School of Architecture, Civil and Environmental Engineering
EPFL - Ecole Polytechnique Fédérale de Lausanne
http://transp-or.epfl.ch
http://people.epfl.ch/michel.bierlaire

可可

unread,
Feb 11, 2025, 2:13:34 AM2/11/25
to Biogeme
Thank you for your reply, it gives me a better understanding of 2-dimension numerical integration. 

I have another question about calculating the condlike. Assuming that my latent variables are AA and CC, when calculating the condlike do I multiply all P_AA_0X and P_CC_0X directly together? Or do I multiply them separately and calculate condlike_AA and condlike_CC separately?
My current code calculates condlike by multiplying all P_AA_0X and P_CC_0X directly together:

condprob = models.logit (V, av, choice)
condlike = P_AA_01 * P_AA_02 * P_AA_03 * P_CC_01 * P_CC_02 * P_CC_03 * condprob

first_integral = Integrate(condlike * density_1, 'omega_1')
second_integral = Integrate(first_integral * density_2, 'omega_2')
loglike = log(second_integral)


Michel Bierlaire

unread,
Feb 11, 2025, 2:19:38 AM2/11/25
to kek3...@gmail.com, Michel Bierlaire, Biogeme


> On 11 Feb 2025, at 04:08, 可可 <kek3...@gmail.com> wrote:
>
> Thank you for your reply, it gives me a better understanding of 2-dimension numerical integration.
>
> I have another question about calculating the condlike. Assuming that my latent variables are AA and CC, when calculating the condlike do I multiply all P_AA_0X and P_CC_0X directly together? Or do I multiply them separately and calculate condlike_AA and condlike_CC separately?
> My current code calculates condlike by multiplying all P_AA_0X and P_CC_0X directly together:
>
> condprob = models.logit (V, av, choice)
> condlike = P_AA_01 * P_AA_02 * P_AA_03 * P_CC_01 * P_CC_02 * P_CC_03 * condprob
> first_integral = Integrate(condlike * density_1, 'omega_1')
> second_integral = Integrate(first_integral * density_2, 'omega_2')
> loglike = log(second_integral)

Yes, this is correct. "condlike" represents the likelihood, conditional on the random parameters. And the likelihood is the product of the probabilities. So you need to calculate it first, and then integrate.
> To view this discussion visit https://groups.google.com/d/msgid/biogeme/f3bf2ea7-0437-4e85-a62b-8252157ffe84n%40googlegroups.com.

可可

unread,
Feb 12, 2025, 7:10:17 AM2/12/25
to Biogeme
Thank you very much for your answer, which has solved the problem that has been troubling my colleagues and I recently. 

My colleagues and I are still interested in the mathematical principle of 2-dimensional numerical integration in Biogeme. After reading several documents such as bioGaussHermite.h, bioGaussHermite.cc, and bioGhFunction.cc, we think that 2-dimensional numerical integration is to approximate the integral value by giving specific integration points and weights. We think the mathematical formula would be:

123.png

Is our mathematical formula correct?

In addition, we found that bioGaussHermite.h gives the specific integration points and weights for 1-dimensional numerical integration. Will Biogeme still take these  integration points and weights for 2-dimensional numerical integration?

Michel Bierlaire

unread,
Feb 12, 2025, 7:14:43 AM2/12/25
to kek3...@gmail.com, Michel Bierlaire, Biogeme
There is no 2-dimensional integral in Biogeme. This is why you need to "nest" the calls.

The "first_integral" integrates over omega_1, and is a function of omega_2.
The "second integral" integrates that one over omega_2.

This is also why it is better to use Monte-Carlo integration when you have several dimensions.


> On 12 Feb 2025, at 13:06, 可可 <kek3...@gmail.com> wrote:
>
> Thank you very much for your answer, which has solved the problem that has been troubling my colleagues and I recently.
>
> My colleagues and I are still interested in the mathematical principle of 2-dimensional numerical integration in Biogeme. After reading several documents such as bioGaussHermite.h, bioGaussHermite.cc, and bioGhFunction.cc, we think that 2-dimensional numerical integration is to approximate the integral value by giving specific integration points and weights. We think the mathematical formula would be:
>
> To view this discussion visit https://groups.google.com/d/msgid/biogeme/86d2c2ba-d97e-4592-a952-b3f2d094b51fn%40googlegroups.com.
> <123.png>

可可

unread,
Feb 12, 2025, 8:05:37 AM2/12/25
to Biogeme
Thank you for your reply!

So if we rewrite the mathematical formula in this form, will it correspond better to the 2-dimensional numerical integration process of Biogeme?
12.png
Reply all
Reply to author
Forward
0 new messages