-
Notifications
You must be signed in to change notification settings - Fork 2
/
fweight.jl
94 lines (88 loc) · 2.79 KB
/
fweight.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
fweight(d; typw = :bisquare, alpha = 0)
Computation of weights from distances.
* `d` : Vector of distances.
Keyword arguments:
* `typw` : Define the weight function.
* `alpha` : Parameter of the weight function,
see below.
The returned weight vector is:
* w = f(`d` / q) where f is the weight function
and q the 1-`alpha` quantile of `d`
(Cleveland & Grosse 1991).
Possible values for `typw` are:
* :bisquare: w = (1 - x^2)^2
* :cauchy: w = 1 / (1 + x^2)
* :epan: w = 1 - x^2
* :fair: w = 1 / (1 + x)^2
* :invexp: w = exp(-x)
* :invexp2: w = exp(-x / 2)
* :gauss: w = exp(-x^2)
* :trian: w = 1 - x
* :tricube: w = (1 - x^3)^3
## References
Cleveland, W.S., Grosse, E., 1991. Computational methods for local regression.
Stat Comput 1, 47–62. https://doi.org/10.1007/BF01890836
## Examples
```julia
using CairoMakie, Distributions
d = sort(sqrt.(rand(Chi(1), 1000)))
cols = cgrad(:tab10, collect(1:9)) ;
alpha = 0
f = Figure(size = (600, 500))
ax = Axis(f, xlabel = "d", ylabel = "Weight")
typw = :bisquare
w = fweight(d; typw, alpha)
lines!(ax, d, w, label = String(typw), color = cols[1])
typw = :cauchy
w = fweight(d; typw, alpha)
lines!(ax, d, w, label = String(typw), color = cols[2])
typw = :epan
w = fweight(d; typw, alpha)
lines!(ax, d, w, label = String(typw), color = cols[3])
typw = :fair
w = fweight(d; typw, alpha)
lines!(ax, d, w, label = String(typw), color = cols[4])
typw = :gauss
w = fweight(d; typw, alpha)
lines!(ax, d, w, label = String(typw), color = cols[5])
typw = :trian
w = fweight(d; typw, alpha)
lines!(ax, d, w, label = String(typw), color = cols[6])
typw = :invexp
w = fweight(d; typw, alpha)
lines!(ax, d, w, label = String(typw), color = cols[7])
typw = :invexp2
w = fweight(d; typw, alpha)
lines!(ax, d, w, label = String(typw), color = cols[8])
typw = :tricube
w = fweight(d; typw, alpha)
lines!(ax, d, w, label = String(typw), color = cols[9])
axislegend("Function", position = :lb)
f[1, 1] = ax
f
```
"""
function fweight(d; typw = :bisquare, alpha = 0)
d = vec(abs.(d))
alpha = max(0, min(1, alpha))
zd = d[isnan.(d) .== 0]
q = quantile(zd, 1 - alpha) # = max when alpha = 0
d ./= q # normalization (d = 1 for max when alpha = 0 ==> w = 0)
typw == :bisquare ? w = (1 .- d.^2).^2 : nothing
typw == :cauchy ? w = 1 ./ (1 .+ d.^2) : nothing
typw == :epan ? w = 1 .- d.^2 : nothing
typw == :fair ? w = 1 ./ (1 .+ d).^2 : nothing
#if typw == :inv
# w = 1 ./ d
# w ./= maximum(w[isnan.(w) .== 0])
#end
typw == :invexp ? w = exp.(-d) : nothing
typw == :invexp2 ? w = exp.(-d / 2) : nothing
typw == :gauss ? w = exp.(-d.^2) : nothing
typw == :trian ? w = 1 .- d : nothing
typw == :tricube ? w = (1 .- d.^3).^3 : nothing
w[d .> 1] .= 0
w[isnan.(w)] .= 0
w
end