Skip to content

Commit b08ace3

Browse files
committed
Small tooltip change and first part of feature functions script
1 parent 8fc375a commit b08ace3

File tree

5 files changed

+97
-4
lines changed

5 files changed

+97
-4
lines changed

examples/approx-prior-sample.jl

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/feature-functions.jl

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/features/Project.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[deps]
2+
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
3+
AbstractGPsMakie = "7834405d-1089-4985-bd30-732a30b92057"
4+
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
5+
KernelSpectralDensities = "027d52a2-76e5-4228-9bfe-bc7e0f5a8348"
6+
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

examples/features/script.jl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

src/features.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ abstract type AbstractRFF end
66

77
"""
88
ShiftedRFF([rng::AbstractRNG], S::SpectralDensity, l::Int)
9-
Random Fourier feature function with a random shift, projecting an input x into l dimensionional feature space.
9+
Random Fourier feature function with a random shift, projecting an input `x` into `l` dimensionional feature space.
1010
1111
# Definition
1212
@@ -51,7 +51,7 @@ end
5151

5252
"""
5353
DoubleRFF([rng::AbstractRNG], S::SpectralDensity, l::Int)
54-
Random Fourier feature function with cos and sin terms, projecting an input x into l dimensionional feature space.
54+
Random Fourier feature function with cos and sin terms, projecting an input `x` into `l` dimensionional feature space.
5555
5656
# Definition
5757

0 commit comments

Comments
 (0)