# How to evaluate a complex function between complex bounds

**URL:** <https://discourse.julialang.org/t/how-to-evaluate-a-complex-function-between-complex-bounds/102460>\
**Category:** General Usage\
**Tags:** question, quadgk, integral\
**Created:** [August 3, 2023, 7:37pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-complex-function-between-complex-bounds/102460 "2023-08-03T19:37:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AkchurinDA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akchurinda/32/49367_2.png) [@AkchurinDA](https://discourse.julialang.org/u/AkchurinDA)\
**Post date:** [August 3, 2023, 7:37pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-complex-function-between-complex-bounds/102460/1 "2023-08-03T19:37:19Z")

</div>

I’m having trouble evaluating the following expression that contains an integral with a complex function with complex bounds.

 ![Expession](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc9979247f7f24a1b9e0621434b14f0fea3fae9e.png)

In this expression, \phi() is the PDF of a standard normal distribution, \beta is a number \>0, and \kappa is a vector of both negative and positive numbers of length n - 1. In a particular example that I’m studying, the following values are used for \beta and \kappa:

```julia
β = 2.4660212074327954
κ = [-0.15473970173530405, -0.03989979101500318, -8.945133695399355e-11]

```

I have tried to evaluate the integral using `QuadGK` package, using the following code:

```julia
Integral, _ = quadgk(s -> (1 / s) * exp((s + β)^2 / 2) * prod(1./sqrt.((1 .+ s .* κ))), 0 + 0 * im, 0 + Inf * im)
PoF = pdf(Normal(0, 1), β) * real(im * sqrt(2 / π) * Integral)

```

But I get `DomainError with 0.0 + Inf*im`.

I know that for the particular values of \beta and \kappa above, the whole expression should evaluate to 0.00936. I would appreciate if somebody could help me with this integral.

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [August 3, 2023, 8:48pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-complex-function-between-complex-bounds/102460/2 "2023-08-03T20:48:14Z")

</div>

You can do a substitution s = ix to make your integration variable real. But the real part of your integral (i.e., the imaginary part of the expression inside the Re) diverges, so you have to pull the `real` inside the integral to get something finite. Still, I can’t reproduce the value you’re claiming for the expression. Here’s what I get:

```julia
using Distributions
using QuadGK

β = 2.4660212074327954
κ = [-0.15473970173530405, -0.03989979101500318, -8.945133695399355e-11]

f(s, β, κ) = inv(s) * exp((s + β)^2 / 2) * prod(k -> inv(sqrt(1 + s * k)), κ)
integral, error = quadgk(x -> -real(f(im * x, β, κ)), 0, Inf)
pof = pdf(Normal(0, 1), β) * sqrt(2 / π) * integral
@show pof
@show error
# pof = -0.4946133368632261
# error = 5.552972283529805e-9

```

---

<div class="post-metadata">

**Author:** ![AkchurinDA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akchurinda/32/49367_2.png) [@AkchurinDA](https://discourse.julialang.org/u/AkchurinDA)\
**Post date:** [August 3, 2023, 10:43pm UTC](https://discourse.julialang.org/t/how-to-evaluate-a-complex-function-between-complex-bounds/102460/3 "2023-08-03T22:43:21Z")

</div>

Thanks, @danielwe, at least now I start getting an actual value instead of an error. Still trying to figure out why the expression does not evaluate to the 0.00936.
