Skip to content

Commit cf96862

Browse files
authored
Merge pull request #37 from JuliaStats/sb/interpolations
move from Grid.jl to Interpolations.jl
2 parents 2de1e7b + 50f31d3 commit cf96862

File tree

9 files changed

+32
-24
lines changed

9 files changed

+32
-24
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ os:
33
- linux
44
- osx
55
julia:
6-
- 0.3
76
- 0.4
87
- 0.5
98
- nightly

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[![Build Status](https://travis-ci.org/JuliaStats/KernelDensity.jl.svg?branch=master)](https://travis-ci.org/JuliaStats/KernelDensity.jl)
44
[![Coverage Status](https://coveralls.io/repos/github/JuliaStats/KernelDensity.jl/badge.svg)](https://coveralls.io/github/JuliaStats/KernelDensity.jl)
5-
[![KernelDensity](http://pkg.julialang.org/badges/KernelDensity_0.3.svg)](http://pkg.julialang.org/?pkg=KernelDensity&ver=0.3)
65
[![KernelDensity](http://pkg.julialang.org/badges/KernelDensity_0.4.svg)](http://pkg.julialang.org/?pkg=KernelDensity&ver=0.4)
76
[![KernelDensity](http://pkg.julialang.org/badges/KernelDensity_0.5.svg)](http://pkg.julialang.org/?pkg=KernelDensity)
87

@@ -78,7 +77,7 @@ e.g. `boundary` now takes a tuple of tuples `((xlo,xhi),(ylo,yhi))`.
7877
The KDE objects are stored as gridded density values, with attached
7978
coordinates. These are typically sufficient for plotting (see below), but
8079
intermediate values can be interpolated using the
81-
[Grid.jl](https://github.com/timholy/Grid.jl) package via the `pdf` method
80+
[Interpolations.jl](https://github.com/tlycken/Interpolations.jl) package via the `pdf` method
8281
(extended from Distributions.jl).
8382

8483
```
@@ -89,15 +88,12 @@ pdf(k::BivariateKDE, x, y)
8988
where `x` and `y` are real numbers or arrays.
9089

9190
If you are making multiple calls to `pdf`, it will be more efficient to
92-
construct an intermediate `InterpKDE` to store the intermediate `InterpGrid`
93-
object:
91+
construct an intermediate `InterpKDE` to store the interpolation structure:
9492

9593
```
9694
ik = InterpKDE(k)
9795
pdf(ik, x)
9896
```
9997

100-
`InterpKDE` can also take additional arguments specifying the
101-
`BoundaryCondition` (default=`BCnan`) and `InterpType` (default=
102-
`InterpQuadratic`).
98+
`InterpKDE` will pass any extra arguments to `interpolate`.
10399

REQUIRE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
julia 0.3
1+
julia 0.4
22
StatsBase
33
Distributions
44
Optim
5-
Grid
5+
Interpolations
66
Compat

src/KernelDensity.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module KernelDensity
55
using StatsBase
66
using Distributions
77
using Optim
8-
using Grid
8+
using Interpolations
99
using Compat
1010

1111
import Base: conv
@@ -14,6 +14,8 @@ import Distributions: twoπ, pdf
1414

1515
export kde, kde_lscv, UnivariateKDE, BivariateKDE, InterpKDE, pdf
1616

17+
abstract AbstractKDE
18+
1719
include("univariate.jl")
1820
include("bivariate.jl")
1921
include("interp.jl")

src/bivariate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Store both grid and density for KDE over R2
2-
type BivariateKDE{Rx<:Range,Ry<:Range}
2+
type BivariateKDE{Rx<:Range,Ry<:Range} <: AbstractKDE
33
x::Rx
44
y::Ry
55
density::Matrix{Float64}

src/interp.jl

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1-
type InterpKDE{K,G}
1+
import Interpolations: interpolate
2+
3+
type InterpKDE{K,I} <: AbstractKDE
24
kde::K
3-
grid::G
5+
itp::I
6+
InterpKDE(kde,itp) = new(kde,itp)
47
end
58

6-
function InterpKDE{BC<:BoundaryCondition, IT<:InterpType}(k::UnivariateKDE, bc::Type{BC}=BCnan,it::Type{IT}=InterpQuadratic)
7-
g = CoordInterpGrid(k.x, k.density, bc, it)
8-
InterpKDE(k,g)
9-
end
10-
function InterpKDE{BC<:BoundaryCondition, IT<:InterpType}(k::BivariateKDE, bc::Type{BC}=BCnan,it::Type{IT}=InterpQuadratic)
11-
g = CoordInterpGrid((k.x,k.y),k.density,bc,it)
12-
InterpKDE{typeof(k),typeof(g)}(k,g)
9+
10+
function InterpKDE(kde::UnivariateKDE, opts...)
11+
itp_u = interpolate(kde.density, opts...)
12+
itp = scale(itp_u, kde.x)
13+
InterpKDE{typeof(kde),typeof(itp)}(kde, itp)
1314
end
15+
InterpKDE(kde::UnivariateKDE) = InterpKDE(kde, BSpline(Quadratic(Line())), OnGrid())
1416

1517

16-
pdf(ik::InterpKDE,x...) = ik.grid[x...]
18+
function InterpKDE(kde::BivariateKDE, opts...)
19+
itp_u = interpolate(kde.density,opts...)
20+
itp = scale(itp_u, kde.x, kde.y)
21+
InterpKDE{typeof(kde),typeof(itp)}(kde, itp)
22+
end
23+
InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Line())), OnGrid())
24+
25+
pdf(ik::InterpKDE,x::Real...) = ik.itp[x...]
26+
pdf(ik::InterpKDE,xs::AbstractVector) = [ik.itp[x] for x in xs]
27+
pdf(ik::InterpKDE,xs::AbstractVector,ys::AbstractVector) = [ik.itp[x,y] for x in xs, y in ys]
1728

1829
pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x)
1930
pdf(k::BivariateKDE,x,y) = pdf(InterpKDE(k),x,y)

src/univariate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Store both grid and density for KDE over the real line
2-
type UnivariateKDE{R<:Range}
2+
type UnivariateKDE{R<:Range} <: AbstractKDE
33
x::R
44
density::Vector{Float64}
55
end

test/interp.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Base.Test
22
using KernelDensity
3-
using Grid
43

54
X = randn(100)
65
Y = randn(100)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
tests = [
22
"univariate",
33
"bivariate",
4+
"interp",
45
]
56

67
println("Running tests:")

0 commit comments

Comments
 (0)