/
_periodic_kernel.py
179 lines (162 loc) · 6.14 KB
/
_periodic_kernel.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
# Standard library imports
import math
from typing import Dict, Tuple
# First-party imports
from gluonts.distribution.distribution import getF, softplus
from gluonts.model.common import Tensor
# Relative imports
from . import Kernel, KernelOutputDict
class PeriodicKernel(Kernel):
r"""
Computes a covariance matrix based on the Periodic kernel
between inputs :math:`\mathbf{x_1}` and :math:`\mathbf{x_2}`:
:math:`k_{\text{Per}}(\mathbf{x_1}, \mathbf{x_2}) = \theta_0 \exp \left
(\frac{-2\sin^2(\theta_2 \pi \|\mathbf{x_1} - \mathbf{x_2}\|)}
{\theta_1^2} \right)`,
where :math:`\theta_0` is the amplitude parameter,
:math:`\theta_1` is the length scale parameter and
:math:`\theta_2` is the frequency parameter.
"""
# noinspection PyMethodOverriding,PyPep8Naming
def __init__(
self,
amplitude: Tensor,
length_scale: Tensor,
frequency: Tensor,
F=None,
) -> None:
"""
Parameters
----------
amplitude : Tensor
Periodic kernel amplitude hyper-parameter of shape (batch_size, 1, 1).
length_scale : Tensor
Periodic kernel length scale hyper-parameter of of shape (batch_size, 1, 1).
frequency : Tensor
Periodic kernel hyper-parameter of shape (batch_size, 1, 1).
F : ModuleType
A module that can either refer to the Symbol API or the NDArray
API in MXNet.
"""
self.F = F if F else getF(amplitude)
self.amplitude = amplitude
self.length_scale = length_scale
self.frequency = frequency
# noinspection PyMethodOverriding,PyPep8Naming
def kernel_matrix(self, x1: Tensor, x2: Tensor) -> Tensor:
"""
Parameters
--------------------
x1 : Tensor
Feature data of shape (batch_size, history_length, num_features).
x2 : Tensor
Feature data of shape (batch_size, history_length, num_features).
Returns
--------------------
Tensor
Periodic kernel matrix of shape (batch_size, history_length, history_length).
"""
self._compute_square_dist(self.F, x1, x2)
return self.F.broadcast_mul(
self.amplitude,
self.F.exp(
self.F.broadcast_div(
-2
* self.F.sin(
self.F.broadcast_mul(
self.frequency,
math.pi
* self.F.sqrt(self.F.abs(self.square_dist)),
)
)
** 2,
self.length_scale ** 2,
)
),
)
class PeriodicKernelOutput(KernelOutputDict):
args_dim: Dict[str, int] = {
"amplitude": 1,
"length_scale": 1,
"frequency": 1,
}
kernel_cls: type = PeriodicKernel
# noinspection PyMethodOverriding,PyPep8Naming
def gp_params_scaling(
self, F, past_target: Tensor, past_time_feat: Tensor
) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
"""
This function returns the scales for the GP Periodic Kernel hyper-parameters by using the standard deviations
of the past_target and past_time_features.
Parameters
----------
F : ModuleType
A module that can either refer to the Symbol API or the NDArray
API in MXNet.
past_target : Tensor
Training time series values of shape (batch_size, context_length).
past_time_feat : Tensor
Training features of shape (batch_size, context_length, num_features).
Returns
-------
Tuple
Three scaled GP hyper-parameters for the Periodic Kernel and scaled model noise hyper-parameter.
Each is a Tensor of shape (batch_size, 1, 1).
"""
axis = 1
sigma_scaling = (
self.compute_std(F, past_target, axis=axis) / math.sqrt(2)
).expand_dims(axis=axis)
amplitude_scaling = sigma_scaling ** 2
length_scale_scaling = F.broadcast_mul(
F.mean(self.compute_std(F, past_time_feat, axis=axis)),
F.ones_like(amplitude_scaling),
)
# TODO: Define scaling for the frequency
frequency_scaling = F.ones_like(amplitude_scaling)
return (
amplitude_scaling,
length_scale_scaling,
frequency_scaling,
sigma_scaling,
)
# noinspection PyMethodOverriding,PyPep8Naming
@classmethod
def domain_map(
cls, F, amplitude: Tensor, length_scale: Tensor, frequency: Tensor
) -> Tuple[Tensor, Tensor, Tensor]:
"""
This function applies the softmax to the Periodic Kernel hyper-parameters.
Parameters
----------
F : ModuleType
A module that can either refer to the Symbol API or the NDArray
API in MXNet.
amplitude : Tensor
Periodic kernel amplitude hyper-parameter of shape (batch_size, 1, 1).
length_scale : Tensor
Periodic kernel length scale hyper-parameter of of shape (batch_size, 1, 1).
frequency : Tensor
Periodic kernel hyper-parameter of shape (batch_size, 1, 1).
Returns
-------
Tuple
Three GP Periodic kernel hyper-parameters.
Each is a Tensor of shape: (batch_size, 1, 1).
"""
amplitude = softplus(F, amplitude)
length_scale = softplus(F, length_scale)
frequency = softplus(F, frequency)
return amplitude, length_scale, frequency