Skip to content
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

Add normalize option to gausshermite #122

Merged
merged 6 commits into from
Sep 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/gausshermite.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
@doc raw"""
gausshermite(n::Integer) -> x, w # nodes, weights
gausshermite(n::Integer; probabilist = false) -> x, w # nodes, weights
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
gausshermite(n::Integer; probabilist = false) -> x, w # nodes, weights
gausshermite(n::Integer; normalize = false) -> x, w # nodes, weights

Maybe, some other name of the keyword argument would be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made your suggested changes and added tests.


Return nodes `x` and weights `w` of [Gauss-Hermite quadrature](https://en.wikipedia.org/wiki/Gauss%E2%80%93Hermite_quadrature).

```math
\int_{-\infty}^{+\infty} f(x) \exp(-x^2) dx \approx \sum_{i=1}^{n} w_i f(x_i)
```

The option `probabilist=true` instead computes
```math
\int_{-\infty}^{+\infty} f(x) \phi(x) dx \approx \sum_{i=1}^{n} w_i f(x_i),
```
where $\phi$ is the standard normal density function.

# Examples
```jldoctest
julia> x, w = gausshermite(3);
Expand All @@ -19,10 +25,10 @@ julia> I ≈ 3(√π)/4
true
```
"""
function gausshermite(n::Integer)
function gausshermite(n::Integer; probabilist = false)
x,w = unweightedgausshermite(n)
w .*= exp.(-x.^2)
x, w
probabilist ? ( sqrt(2.0) * x, w / sqrt( π ) ) : ( x, w )
end

function unweightedgausshermite(n::Integer)
Expand Down