- #1

EngWiPy

- 1,368

- 61

I want to find the numerical solution to the following nested integral in Python

[tex]

\frac{K!}{(K-M)!}\int_{x_1=0}^{y}\int_{x_{2}=x_1}^{\max(x_1,\,y-x_1)}\cdots

\int_{x_M=x_{M-1}}^{\max(x_{M-1},\,y-\sum_{m=1}^{M-1}x_m)}

\frac{1}{(1+x_M)^{K-M+2}}\prod_{m=1}^{M-1}

\frac{1}{(1+x_m)^2}\,dx_1\,dx_2\cdots\,dx_M

[/tex]

To that end, I did the following

The above integral is the CDF of a random variable, but the result isn't between 0 and 1. So, I guess there is an error, but where?

Thanks

[tex]

\frac{K!}{(K-M)!}\int_{x_1=0}^{y}\int_{x_{2}=x_1}^{\max(x_1,\,y-x_1)}\cdots

\int_{x_M=x_{M-1}}^{\max(x_{M-1},\,y-\sum_{m=1}^{M-1}x_m)}

\frac{1}{(1+x_M)^{K-M+2}}\prod_{m=1}^{M-1}

\frac{1}{(1+x_m)^2}\,dx_1\,dx_2\cdots\,dx_M

[/tex]

To that end, I did the following

Python:

```
import math
K=4
M=2
du = 0.01
def F(u):
return 1/(1+u)**(K-M+2)
#Define the PDF of U
def f(u):
return 1/((1+u))**2
def G(y, m, prev_lim):
#print(f'h is {h}, and k is {k}')
if m == M:
res = F(y)
else:
res = 0
u = prev_lim
while u < y:
res += G(y-u, m+1, u)*f(u)*du
u += du
return (math.factorial(K)/math.factorial(K-M))*res
print(G(2, 1, 0))
```

The above integral is the CDF of a random variable, but the result isn't between 0 and 1. So, I guess there is an error, but where?

Thanks

Last edited: