@@ -66,8 +66,20 @@ function kde_range(boundary::(@compat Tuple{Real,Real}), npoints::Int)
66
66
lo: step: hi
67
67
end
68
68
69
+ immutable UniformWeights
70
+ w
71
+
72
+ UniformWeights (n) = new (1 / n)
73
+ end
74
+
75
+ Base. sum (x:: UniformWeights ) = 1.
76
+ Base. getindex (x:: UniformWeights , i) = x. w
77
+
78
+ typealias Weights Union{UniformWeights, RealVector, WeightVec}
79
+
80
+
69
81
# tabulate data for kde
70
- function tabulate (data:: RealVector , weights:: RealVector , midpoints:: Range )
82
+ function tabulate (data:: RealVector , weights:: Weights , midpoints:: Range )
71
83
npoints = length (midpoints)
72
84
s = step (midpoints)
73
85
@@ -90,8 +102,7 @@ function tabulate(data::RealVector, weights::RealVector, midpoints::Range)
90
102
end
91
103
92
104
function tabulate (data:: RealVector , midpoints:: Range )
93
- weights = ones (data)
94
- tabulate (data, weights, midpoints)
105
+ tabulate (data, UniformWeights (length (data)), midpoints)
95
106
end
96
107
97
108
# convolve raw KDE with kernel
@@ -122,33 +133,28 @@ function conv(k::UnivariateKDE, dist::UnivariateDistribution)
122
133
UnivariateKDE (k. x, dens)
123
134
end
124
135
125
- function uniformweights (data)
126
- n = length (data)
127
- fill (1 / n, n)
128
- end
129
-
130
136
# main kde interface methods
131
- function kde (data:: RealVector , weights:: RealVector , midpoints:: Range , dist:: UnivariateDistribution )
137
+ function kde (data:: RealVector , weights:: Weights , midpoints:: Range , dist:: UnivariateDistribution )
132
138
k = tabulate (data, weights, midpoints)
133
139
conv (k,dist)
134
140
end
135
141
136
142
function kde (data:: RealVector , dist:: UnivariateDistribution ;
137
- boundary:: (@compat Tuple{Real,Real}) = kde_boundary (data,std (dist)), npoints:: Int = 2048 , weights= uniformweights ( data))
143
+ boundary:: (@compat Tuple{Real,Real}) = kde_boundary (data,std (dist)), npoints:: Int = 2048 , weights= UniformWeights ( length ( data) ))
138
144
139
145
midpoints = kde_range (boundary,npoints)
140
146
kde (data,weights,midpoints,dist)
141
147
end
142
148
143
149
function kde (data:: RealVector , midpoints:: Range ;
144
- bandwidth= default_bandwidth (data), kernel= Normal, weights= uniformweights ( data))
150
+ bandwidth= default_bandwidth (data), kernel= Normal, weights= UniformWeights ( length ( data) ))
145
151
bandwidth > 0.0 || error (" Bandwidth must be positive" )
146
152
dist = kernel_dist (kernel,bandwidth)
147
153
kde (data,weights,midpoints,dist)
148
154
end
149
155
150
156
function kde (data:: RealVector ; bandwidth= default_bandwidth (data), kernel= Normal,
151
- npoints:: Int = 2048 , boundary:: (@compat Tuple{Real,Real}) = kde_boundary (data,bandwidth), weights= uniformweights ( data))
157
+ npoints:: Int = 2048 , boundary:: (@compat Tuple{Real,Real}) = kde_boundary (data,bandwidth), weights= UniformWeights ( length ( data) ))
152
158
bandwidth > 0.0 || error (" Bandwidth must be positive" )
153
159
dist = kernel_dist (kernel,bandwidth)
154
160
kde (data,dist;boundary= boundary,npoints= npoints,weights= weights)
0 commit comments