1+ # # Random Fourier Features
2+ # One of the reasons to be interested in the spectral density of a kernel is
3+ # that it allows us to approximate a GP Prior.
4+ #
5+ # In this notebook we show the two feature functions implemented in KernelSpectralDensities.jl
6+ # and how to use them.
7+
8+ # ## Load required packages
9+ using KernelSpectralDensities
10+ using AbstractGPs
11+ using StatsBase
12+ # using LinearAlgebra
13+ using AbstractGPsMakie
14+ using CairoMakie
15+
16+ # ## Intro
17+ # We use the AbstractGPs package to define a stationary GP prior,
18+ # in other words, a GP that has not been conditioned on data yet.
19+
20+ ker = SqExponentialKernel ()
21+ S = SpectralDensity (ker, 1 )
22+
23+ gp = GP (ker)
24+
25+ # We can also plot this GP using AbstractGPsMakie.jl, but
26+ # we don't see very much, since we have a simple GP with
27+ # zero mean and a variance of 1.
28+ f = plot (0 : 0.1 : 1 , gp; size= (600 , 400 ))
29+ DisplayAs. PNG (f) # hide #md
30+
31+ # ## Random Fourier Features
32+ # KernelSpectralDensities implements two types of random Fourier features,
33+ # `ShiftedRFF` and `DoubleRFF`.
34+ # A feature function projects its input into a higher-dimensional "features" space.,
35+
36+ # ### ShiftedRFF
37+ # The `ShiftedRFF` feature function is somewhat more common, and has
38+ # been used in papers such as [Efficiently sampling functions from Gaussian process posteriors](https://proceedings.mlr.press/v119/wilson20a.html).
39+ #
40+ # It is defined as
41+ # ```math
42+ # \sqrt{2 / l} \cos(2 π ((w^T x) + b))
43+ # ```
44+ # where `w` is sampled from the spectral density `S`,
45+ # `b` is uniformly sampled from `[0, 2π]`
46+ # and `l` is the number of sampled frequencies, which is also
47+ # the number of features.
48+ #
49+ # We generate a set of 4 feature functions, which we can evaluate
50+ # at any point.
51+ srff = ShiftedRFF (S, 4 )
52+ srff (1.0 )
53+
54+ # If we plot them, we see that each feature function is a harmonic
55+ # with varying frequency and phase.
56+ x = range (0 , 5 ; length= 100 )
57+ f = Figure (; size= (600 , 400 ))
58+ ax = Axis (f[1 , 1 ]; xlabel= " x" , ylabel= " rff(x)" , title= " ShiftedRFF" )
59+ series! (ax, x, reduce (hcat, srff .(x)); labels= [" srff $i " for i in 1 : 4 ])
60+ axislegend (ax; position= :ct )
61+ f
62+ DisplayAs. PNG (f) # hide #md
63+
64+ # ### DoubleRFF
65+ # The `DoubleRFF` feature function is less common, but is theoretically
66+ # equivalent to the `ShiftedRFF` feature function.
67+ #
68+ # It is defined as
69+ # ```math
70+ # \sqrt{1 / l} [\cos(2 π w' x), \sin(2 π w' x)]
71+ # ```
72+ # where `w` is sampled from the spectral density `S`,
73+ # with a total of `l/2` sampled frequencies.
74+ #
75+ # Here, each function is effectively two feature functions in one,
76+ # so specifying `l` will result in `l/2` samples but an `l` dimensional
77+ # feature vector.
78+ #
79+ # We again generate a set of 4 feature functions.
80+ drff = DoubleRFF (S, 4 )
81+ drff (1.0 )
82+
83+ # We plot these features as well
84+ f = Figure (; size= (600 , 400 ))
85+ ax = Axis (f[1 , 1 ]; xlabel= " x" , ylabel= " rff(x)" , title= " Double RFF" )
86+ series! (ax, x, reduce (hcat, drff .(x)); labels= [" drff $i " for i in 1 : 4 ])
87+ axislegend (ax; position= :ct )
88+ f
89+ DisplayAs. PNG (f) # hide #md
0 commit comments