Skip to content

Commit 7175f33

Browse files
committed
monte carlo integration
1 parent 57f48a4 commit 7175f33

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NumericalAlgorithms"
22
uuid = "3b47cccd-2e0f-43d2-aaa1-0b61cbee3591"
33
authors = ["Murat Koptur <[email protected]>"]
4-
version = "0.1.4"
4+
version = "0.1.5"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

README.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ Install the package with ```add https://github.com/mrtkp9993/NumericalAlgorithms
1515
Currently implemented:
1616

1717
* Root finding algorithms
18-
* Secant method
19-
* Broyden's method
18+
* Secant method
19+
* Broyden's method
2020
* Differentation
21-
* Automatic differentiation via dual numbers
21+
* Automatic differentiation via dual numbers
2222
* Integration
23-
* Composite Simpson - One dim.
24-
* Double Simpson - Two dim.
25-
* [WIP] Monte Carlo Integration
23+
* Composite Simpson - One dim.
24+
* Double Simpson - Two dim.
25+
* Monte Carlo Integration - Arbitrary dimension
2626
* Random Number Generators (RNGs)
27-
* Pseudo-random numbers
28-
* Lagged Fibonacci generator
29-
* RANMAR
30-
* [WIP] Quasi-random numbers
31-
* van der Corput sequences
32-
* Halton sequences
33-
* Faure sequences
34-
* [WIP] Sobol sequences
35-
27+
* Pseudo-random numbers
28+
* Lagged Fibonacci generator
29+
* RANMAR
30+
* Quasi-random numbers
31+
* van der Corput sequences
32+
* Halton sequences
33+
* Faure sequences
34+
* Sobol sequences
3635
* Statistical Tests
37-
* Wald–Wolfowitz runs test
36+
* Wald–Wolfowitz runs test
37+
38+
## Next steps to do
3839

39-
*Todo*
4040
* Optimization
4141
* Differential Equations (ODEs)& Systems of diff. eqs.
4242
* Partial Differential Equations (PDEs)

src/Integration.jl

+18
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,22 @@ function CalcDoubleIntegral(f::Function, a::Real, b::Real, c::Function, d::Funct
7979
t = t * hx / 3.0
8080
println("Approximation to integral: $t")
8181
return t
82+
end
83+
84+
function CalcMonteCarloIntegral(f::Function, an, bn, n::UInt64)
85+
@assert length(an) == length(bn)
86+
rndNums = zeros(n, length(an))
87+
mult = 1
88+
for i = 1:length(an)
89+
ani = an[i]
90+
bni = bn[i]
91+
rndNums[:, i] = uniformSeq(ani, bni, n)
92+
mult *= bni - ani
93+
end
94+
mult /= n
95+
intt = 0
96+
for j = 1:n
97+
intt += f(rndNums[j,:]...)
98+
end
99+
mult * intt
82100
end

src/NumericalAlgorithms.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export C_SQRT1_2, C_SQRTPI, C_SQRT2PI
2222
export primes_1000
2323
export FindRoot1D, FindRootND
2424
export Dual
25-
export CalcSingleIntegral, CalcDoubleIntegral
26-
export LFG, RANMAR, vanderCorputSeq, haltonSeq, faureSeq
25+
export CalcSingleIntegral, CalcDoubleIntegral, CalcMonteCarloIntegral
26+
export LFG, RANMAR, vanderCorputSeq, haltonSeq, faureSeq, sobolSeq
2727
export RunsTest
2828

2929
end # module

src/Random.jl

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ end
1919
"Constructor for LFG with default parameter values"
2020
LFG() = LFG(24, 55, 2^32, [rand(UInt) % 2^32 for i = 1:55], +)
2121
"Constructor for LFG with user supplied values, p, q, m"
22-
LFG(p, q, m) = LPG(p, q, m, [rand(UInt) % m for i = 1:q], +)
22+
LFG(p, q, m) = LFG(p, q, m, [rand(UInt) % m for i = 1:q], +)
2323

2424

2525
function Base.rand(rng::LFG)
@@ -111,4 +111,17 @@ function faureSeq(nb, m, b)
111111
end
112112
end
113113
bn
114+
end
115+
116+
"Sobol sequence"
117+
function sobolSeq()
118+
throw("not implemented yet")
119+
end
120+
121+
"Generate n random numbers with uniform dist. in [a, b]"
122+
function uniformSeq(a, b, n)
123+
rng1 = LFG(9739, 23209, 2^32 - 1)
124+
rndNums = [rand(rng1) for i = 1:n]
125+
rndNums = a .+ ((b - a) / (maximum(rndNums) - minimum(rndNums))) .* (rndNums .- minimum(rndNums))
126+
rndNums
114127
end

0 commit comments

Comments
 (0)