|
1 | 1 | # Following the algorithm by William and Seeger, 2001 |
2 | 2 | # Cs is equivalent to X_mm and C to X_mn |
3 | 3 |
|
4 | | -function sampleindex(X::AbstractMatrix, r::Real; obsdim::Integer=defaultobs) |
| 4 | +function sampleindex(X::AbstractVector, r::Real) |
5 | 5 | 0 < r <= 1 || throw(ArgumentError("Sample rate `r` must be in range (0,1]")) |
6 | | - n = size(X, obsdim) |
| 6 | + n = length(X) |
7 | 7 | m = ceil(Int, n * r) |
8 | 8 | S = StatsBase.sample(1:n, m; replace=false, ordered=true) |
9 | 9 | return S |
10 | 10 | end |
11 | 11 |
|
12 | | -function nystrom_sample( |
13 | | - k::Kernel, X::AbstractMatrix, S::Vector{<:Integer}; obsdim::Integer=defaultobs |
14 | | -) |
15 | | - obsdim ∈ [1, 2] || |
16 | | - throw(ArgumentError("`obsdim` should be 1 or 2 (see docs of kernelmatrix))")) |
17 | | - Xₘ = obsdim == 1 ? X[S, :] : X[:, S] |
18 | | - C = kernelmatrix(k, Xₘ, X; obsdim=obsdim) |
| 12 | +@deprecate sampleindex(X::AbstractMatrix, r::Real; obsdim::Integer=defaultobs) sampleindex( |
| 13 | + vec_of_vecs(X; obsdim=obsdim), r |
| 14 | +) false |
| 15 | + |
| 16 | +function nystrom_sample(k::Kernel, X::AbstractVector, S::AbstractVector{<:Integer}) |
| 17 | + Xₘ = @view X[S] |
| 18 | + C = kernelmatrix(k, Xₘ, X) |
19 | 19 | Cs = C[:, S] |
20 | 20 | return (C, Cs) |
21 | 21 | end |
22 | 22 |
|
| 23 | +@deprecate nystrom_sample( |
| 24 | + k::Kernel, X::AbstractMatrix, S::Vector{<:Integer}; obsdim::Integer=defaultobs |
| 25 | +) nystrom_sample(k, vec_of_vecs(X; obsdim=obsdim), S) false |
| 26 | + |
23 | 27 | function nystrom_pinv!(Cs::Matrix{T}, tol::T=eps(T) * size(Cs, 1)) where {T<:Real} |
24 | 28 | # Compute eigendecomposition of sampled component of K |
25 | 29 | QΛQᵀ = LinearAlgebra.eigen!(LinearAlgebra.Symmetric(Cs)) |
@@ -63,38 +67,48 @@ function NystromFact(W::Matrix{<:Real}, C::Matrix{<:Real}) |
63 | 67 | end |
64 | 68 |
|
65 | 69 | @doc raw""" |
66 | | - nystrom(k::Kernel, X::Matrix, S::Vector; obsdim::Int=defaultobs) |
| 70 | + nystrom(k::Kernel, X::AbstractVector, S::AbstractVector{<:Integer}) |
67 | 71 |
|
68 | | -Computes a factorization of Nystrom approximation of the square kernel matrix of data |
69 | | -matrix `X` with respect to kernel `k`. Returns a `NystromFact` struct which stores a |
70 | | -Nystrom factorization satisfying: |
| 72 | +Compute a factorization of a Nystrom approximation of the square kernel matrix |
| 73 | +of data vector `X` with respect to kernel `k`, using indices `S`. |
| 74 | +Returns a `NystromFact` struct which stores a Nystrom factorization satisfying: |
71 | 75 | ```math |
72 | 76 | \mathbf{K} \approx \mathbf{C}^{\intercal}\mathbf{W}\mathbf{C} |
73 | 77 | ``` |
74 | 78 | """ |
75 | | -function nystrom(k::Kernel, X::AbstractMatrix, S::Vector{<:Integer}; obsdim::Int=defaultobs) |
76 | | - C, Cs = nystrom_sample(k, X, S; obsdim=obsdim) |
| 79 | +function nystrom(k::Kernel, X::AbstractVector, S::AbstractVector{<:Integer}) |
| 80 | + C, Cs = nystrom_sample(k, X, S) |
77 | 81 | W = nystrom_pinv!(Cs) |
78 | 82 | return NystromFact(W, C) |
79 | 83 | end |
80 | 84 |
|
81 | 85 | @doc raw""" |
82 | | - nystrom(k::Kernel, X::Matrix, r::Real; obsdim::Int=defaultobs) |
| 86 | + nystrom(k::Kernel, X::AbstractVector, r::Real) |
83 | 87 |
|
84 | | -Computes a factorization of Nystrom approximation of the square kernel matrix of data |
85 | | -matrix `X` with respect to kernel `k` using a sample ratio of `r`. |
| 88 | +Compute a factorization of a Nystrom approximation of the square kernel matrix |
| 89 | +of data vector `X` with respect to kernel `k` using a sample ratio of `r`. |
86 | 90 | Returns a `NystromFact` struct which stores a Nystrom factorization satisfying: |
87 | 91 | ```math |
88 | 92 | \mathbf{K} \approx \mathbf{C}^{\intercal}\mathbf{W}\mathbf{C} |
89 | 93 | ``` |
90 | 94 | """ |
| 95 | +function nystrom(k::Kernel, X::AbstractVector, r::Real) |
| 96 | + S = sampleindex(X, r) |
| 97 | + return nystrom(k, X, S) |
| 98 | +end |
| 99 | + |
| 100 | +function nystrom( |
| 101 | + k::Kernel, X::AbstractMatrix, S::AbstractVector{<:Integer}; obsdim::Int=defaultobs |
| 102 | +) |
| 103 | + return nystrom(k, vec_of_vecs(X; obsdim=obsdim), S) |
| 104 | +end |
| 105 | + |
91 | 106 | function nystrom(k::Kernel, X::AbstractMatrix, r::Real; obsdim::Int=defaultobs) |
92 | | - S = sampleindex(X, r; obsdim=obsdim) |
93 | | - return nystrom(k, X, S; obsdim=obsdim) |
| 107 | + return nystrom(k, vec_of_vecs(X; obsdim=obsdim), r) |
94 | 108 | end |
95 | 109 |
|
96 | 110 | """ |
97 | | - nystrom(CᵀWC::NystromFact) |
| 111 | + kernelmatrix(CᵀWC::NystromFact) |
98 | 112 |
|
99 | 113 | Compute the approximate kernel matrix based on the Nystrom factorization. |
100 | 114 | """ |
|
0 commit comments