Skip to content

Commit

Permalink
inline and type_stablity improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
m-wells committed May 16, 2019
1 parent ae39071 commit 64fc826
Show file tree
Hide file tree
Showing 11 changed files with 871 additions and 193 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "InterpolatedRejectionSampling"
uuid = "9608d442-472c-11e9-3e14-5b72dbacd115"
desc = "Fast (multivariate) rejection sampling of discrete PDFs using Interpolation.jl"
authors = ["m-wells"]
version = "1.0.1"
version = "1.0.2"

[deps]
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
Expand Down
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ end
```
```
BenchmarkTools.Trial:
memory estimate: 29.86 MiB
allocs estimate: 600029
memory estimate: 12.33 MiB
allocs estimate: 200013
--------------
minimum time: 43.686 ms (4.48% GC)
median time: 46.023 ms (4.69% GC)
mean time: 48.114 ms (7.55% GC)
maximum time: 112.981 ms (51.23% GC)
minimum time: 5.003 ms (0.00% GC)
median time: 8.635 ms (26.59% GC)
mean time: 8.392 ms (21.52% GC)
maximum time: 78.848 ms (88.96% GC)
--------------
samples: 104
samples: 595
evals/sample: 1
```
```julia
Expand All @@ -106,15 +106,15 @@ using BenchmarkTools
```
```
BenchmarkTools.Trial:
memory estimate: 37.66 MiB
allocs estimate: 735487
memory estimate: 43.60 MiB
allocs estimate: 1470663
--------------
minimum time: 27.273 ms (6.16% GC)
median time: 29.676 ms (11.63% GC)
mean time: 31.125 ms (11.67% GC)
maximum time: 100.308 ms (62.43% GC)
minimum time: 37.705 ms (13.28% GC)
median time: 50.029 ms (13.58% GC)
mean time: 51.103 ms (15.15% GC)
maximum time: 144.499 ms (57.30% GC)
--------------
samples: 161
samples: 98
evals/sample: 1
```
```julia
Expand All @@ -123,14 +123,14 @@ using BenchmarkTools
```
```
BenchmarkTools.Trial:
memory estimate: 7.19 MiB
allocs estimate: 143
memory estimate: 7.71 MiB
allocs estimate: 355344
--------------
minimum time: 6.809 ms (0.00% GC)
median time: 7.327 ms (0.00% GC)
mean time: 7.811 ms (5.67% GC)
maximum time: 76.786 ms (87.52% GC)
minimum time: 10.348 ms (0.00% GC)
median time: 13.885 ms (0.00% GC)
mean time: 14.703 ms (8.48% GC)
maximum time: 92.105 ms (83.98% GC)
--------------
samples: 640
samples: 340
evals/sample: 1
```
202 changes: 168 additions & 34 deletions docs/README.ipynb

Large diffs are not rendered by default.

274 changes: 274 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@


```julia
import Pkg
Pkg.activate("../.")
```




"/home/mark/devel/InterpolatedRejectionSampling/Project.toml"




```julia
X = range(0, π, length=10)
Y = range(0, π/4, length=9)
knots = (X,Y)
prob = [sin(x)+tan(y) for x in X, y in Y]
```




10×9 Array{Float64,2}:
0.0 0.0984914 0.198912 0.303347 … 0.668179 0.820679 1.0
0.34202 0.440512 0.540933 0.645367 1.0102 1.1627 1.34202
0.642788 0.741279 0.8417 0.946134 1.31097 1.46347 1.64279
0.866025 0.964517 1.06494 1.16937 1.5342 1.6867 1.86603
0.984808 1.0833 1.18372 1.28815 1.65299 1.80549 1.98481
0.984808 1.0833 1.18372 1.28815 … 1.65299 1.80549 1.98481
0.866025 0.964517 1.06494 1.16937 1.5342 1.6867 1.86603
0.642788 0.741279 0.8417 0.946134 1.31097 1.46347 1.64279
0.34202 0.440512 0.540933 0.645367 1.0102 1.1627 1.34202
1.22465e-16 0.0984914 0.198912 0.303347 0.668179 0.820679 1.0




```julia
using PyPlot
imshow(transpose(prob);
extent = (knots[1][1], knots[1][end], knots[2][1], knots[2][end]),
aspect = "auto",
origin = "lower")
ax = gca()
ax.set_xlabel("x-axis [sin(x)]")
ax.set_ylabel("y-axis [tan(y)]")
;
```


![png](output_2_0.png)



```julia
using InterpolatedRejectionSampling
n = 100_000
xy = irsample(knots,prob,n)
hist2D(xy[1,:],xy[2,:],
bins=[X,Y])
ax = gca()
ax.set_xlabel("x-axis [sin(x)]")
ax.set_ylabel("y-axis [tan(y)]")
;
```


![png](output_3_0.png)


## throw away every other x or y


```julia
xy = convert(Matrix{Union{Missing,Float64}}, xy)
for i = 1:n
if iseven(i)
xy[1,i] = missing
else
xy[2,i] = missing
end
end
irsample!(xy,knots,prob)
hist2D(xy[1,:],xy[2,:],
bins=[X,Y])
ax = gca()
ax.set_xlabel("x-axis [sin(x)]")
ax.set_ylabel("y-axis [tan(y)]")
;
```


![png](output_5_0.png)


## Benchmarking


```julia
using BenchmarkTools
for i = 1:n
if iseven(i)
xy[1,i] = missing
else
xy[2,i] = missing
end
end
@benchmark irsample!(xy,knots,prob)
```




BenchmarkTools.Trial:
memory estimate: 12.33 MiB
allocs estimate: 200013
--------------
minimum time: 5.003 ms (0.00% GC)
median time: 8.635 ms (26.59% GC)
mean time: 8.392 ms (21.52% GC)
maximum time: 78.848 ms (88.96% GC)
--------------
samples: 595
evals/sample: 1




```julia
using BenchmarkTools
@benchmark irsample(knots,prob,n)
```




BenchmarkTools.Trial:
memory estimate: 43.60 MiB
allocs estimate: 1470663
--------------
minimum time: 37.705 ms (13.28% GC)
median time: 50.029 ms (13.58% GC)
mean time: 51.103 ms (15.15% GC)
maximum time: 144.499 ms (57.30% GC)
--------------
samples: 98
evals/sample: 1




```julia
using BenchmarkTools
@benchmark irsample(X,sin.(X),n)
```




BenchmarkTools.Trial:
memory estimate: 7.71 MiB
allocs estimate: 355344
--------------
minimum time: 10.348 ms (0.00% GC)
median time: 13.885 ms (0.00% GC)
mean time: 14.703 ms (8.48% GC)
maximum time: 92.105 ms (83.98% GC)
--------------
samples: 340
evals/sample: 1



# A more aggresive example


```julia
X = range(0, 5, length=10)
Y = range(0, 5, length=9)
knots = (X,Y)
prob = [exp10(x)*exp(y) for x in X, y in Y]
prob ./= maximum(prob)
```




10×9 Array{Float64,2}:
6.73795e-8 1.25881e-7 2.35177e-7 … 5.35261e-6 1.0e-5
2.42149e-7 4.52394e-7 8.45184e-7 1.92363e-5 3.59381e-5
8.70239e-7 1.62582e-6 3.03743e-6 6.91317e-5 0.000129155
3.12748e-6 5.8429e-6 1.0916e-5 0.000248446 0.000464159
1.12396e-5 2.09983e-5 3.923e-5 0.00089287 0.0016681
4.03929e-5 7.54639e-5 0.000140985 … 0.00320881 0.00599484
0.000145165 0.000271203 0.000506674 0.0115319 0.0215443
0.000521695 0.000974654 0.00182089 0.0414433 0.0774264
0.00187487 0.00350273 0.00654395 0.14894 0.278256
0.00673795 0.0125881 0.0235177 0.535261 1.0




```julia
using PyPlot
imshow(transpose(prob);
extent = (knots[1][1], knots[1][end], knots[2][1], knots[2][end]),
aspect = "auto",
origin = "lower")
ax = gca()
ax.set_xlabel("x-axis [exp10(x)]")
ax.set_ylabel("y-axis [exp(y)]")
;
```


![png](output_12_0.png)



```julia
xy = Matrix{Union{Missing,Float64}}(missing,2,100_000)
irsample!(xy,knots,prob)

hist2D(xy[1,:],xy[2,:], bins=[X,Y])

ax = gca()
ax.set_xlabel("x-axis [exp10(x)]")
ax.set_ylabel("y-axis [exp(y)]")
```


![png](output_13_0.png)





PyObject Text(24.000000000000007, 0.5, 'y-axis [exp(y)]')




```julia
xy = Matrix{Union{Missing,Float64}}(missing,2,1000)
prob = [exp10(x)*exp(y) for x in X, y in Y]
prob ./= maximum(prob)
irsample!(xy,knots,prob)
@benchmark irsample!(xy,knots,prob)
```




BenchmarkTools.Trial:
memory estimate: 131.70 KiB
allocs estimate: 2011
--------------
minimum time: 52.181 μs (0.00% GC)
median time: 64.942 μs (0.00% GC)
mean time: 115.768 μs (28.73% GC)
maximum time: 88.642 ms (99.88% GC)
--------------
samples: 10000
evals/sample: 1




```julia

```
Binary file added docs/output_12_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/output_13_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/output_3_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/output_5_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 64fc826

Please sign in to comment.