File size: 9,876 Bytes
b84a0b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import torch
import math


class BasisFunctions(object):
    def __init__(self):
        pass

    def __len__(self):
        """Number of basis functions."""
        pass

    def evaluate(self, t):
        pass

    def integrate_t2_times_psi(self, a, b):
        """Compute integral int_a^b (t**2) * psi(t)."""
        pass

    def integrate_t_times_psi(self, a, b):
        """Compute integral int_a^b t * psi(t)."""
        pass

    def integrate_psi(self, a, b):
        """Compute integral int_a^b psi(t)."""
        pass


class PowerBasisFunctions(BasisFunctions):
    """Function phi(t) = t**degree."""
    def __init__(self, degree):
        self.degree = degree.unsqueeze(0)

    def __len__(self):
        """Number of basis functions."""
        return self.degree.size(1)

    def evaluate(self, t):
        return t**self.degree

    def integrate_t2_times_psi(self, a, b):
        """Compute integral int_a^b (t**2) * psi(t)."""
        return (b**(self.degree + 3) - a**(self.degree + 3)) / (self.degree + 3)

    def integrate_t_times_psi(self, a, b):
        """Compute integral int_a^b t * psi(t)."""
        return (b**(self.degree + 2) - a**(self.degree + 2)) / (self.degree + 2)

    def integrate_psi(self, a, b):
        """Compute integral int_a^b psi(t)."""
        return (b**(self.degree + 1) - a**(self.degree + 1)) / (self.degree + 1)

    def __repr__(self):
        return f"PowerBasisFunction(degree={self.degree})"


class SineBasisFunctions(BasisFunctions):
    """Function phi(t) = sin(omega*t)."""
    def __init__(self, omega):
        self.omega = omega.unsqueeze(0)

    def __repr__(self):
        return f"SineBasisFunction(omega={self.omega})"

    def __len__(self):
        """Number of basis functions."""
        return self.omega.size(1)

    def evaluate(self, t):
        return torch.sin(self.omega*t)

    def integrate_t2_times_psi(self, a, b):
        """Compute integral int_a^b (t**2) * psi(t)."""
        # The antiderivative of (t**2)*sin(omega*t) is
        # ((2-(t**2)*(omega**2))*cos(omega*t) + 2*omega*t*sin(omega*t)) / omega**3.  # noqa
        return ((2-(b**2)*(self.omega**2))*torch.cos(self.omega*b)
                + 2*self.omega*b*torch.sin(self.omega*b)
                - (2-(a**2)*(self.omega**2))*torch.cos(self.omega*a)
                - 2*self.omega*a*torch.sin(self.omega*a)
                ) / (self.omega**3)

    def integrate_t_times_psi(self, a, b):
        """Compute integral int_a^b t * psi(t)."""
        # The antiderivative of t*sin(omega*t) is
        # (sin(omega*t) - omega*t*cos(omega*t)) / omega**2.
        return (torch.sin(self.omega*b) - self.omega*b*torch.cos(self.omega*b)
                - torch.sin(self.omega*a) + self.omega*a*torch.cos(self.omega*a)
                ) / (self.omega**2)

    def integrate_psi(self, a, b):
        """Compute integral int_a^b psi(t)."""
        # The antiderivative of sin(omega*t) is -cos(omega*t)/omega.
        return (-torch.cos(self.omega*b) + torch.cos(self.omega*a)) / self.omega


class CosineBasisFunctions(BasisFunctions):
    """Function phi(t) = cos(omega*t)."""
    def __init__(self, omega):
        self.omega = omega.unsqueeze(0)

    def __repr__(self):
        return f"CosineBasisFunction(omega={self.omega})"

    def __len__(self):
        """Number of basis functions."""
        return self.omega.size(1)

    def evaluate(self, t):
        return torch.cos(self.omega*t)

    def integrate_t2_times_psi(self, a, b):
        """Compute integral int_a^b (t**2) * psi(t)."""
        # The antiderivative of (t**2)*cos(omega*t) is
        # (((t**2)*(omega**2)-2)*cos(omega*t) + 2*omega*t*sin(omega*t)) / omega**3.  # noqa
        return (((b**2)*(self.omega**2)-2)*torch.sin(self.omega*b)
                + 2*self.omega*b*torch.cos(self.omega*b)
                - ((a**2)*(self.omega**2)-2)*torch.sin(self.omega*a)
                - 2*self.omega*a*torch.cos(self.omega*a)
                ) / (self.omega**3)

    def integrate_t_times_psi(self, a, b):
        """Compute integral int_a^b t * psi(t)."""
        # The antiderivative of t*cos(omega*t) is
        # (cos(omega*t) + omega*t*sin(omega*t)) / omega**2.
        return (torch.cos(self.omega*b) + self.omega*b*torch.sin(self.omega*b)
                - torch.cos(self.omega*a) - self.omega*a*torch.sin(self.omega*a)
                ) / (self.omega**2)

    def integrate_psi(self, a, b):
        """Compute integral int_a^b psi(t)."""
        # The antiderivative of cos(omega*t) is sin(omega*t)/omega.
        return (torch.sin(self.omega*b) - torch.sin(self.omega*a)) / self.omega


class GaussianBasisFunctions(BasisFunctions):
    """Function phi(t) = Gaussian(t; mu, sigma_sq)."""
    def __init__(self, mu, sigma):
        self.mu = mu.unsqueeze(0)
        self.sigma = sigma.unsqueeze(0)

    def __repr__(self):
        return f"GaussianBasisFunction(mu={self.mu}, sigma={self.sigma})"

    def __len__(self):
        """Number of basis functions."""
        return self.mu.size(1)

    def _phi(self, t):
        return 1. / math.sqrt(2 * math.pi) * torch.exp(-.5 * t**2)

    def _Phi(self, t):
        return .5 * (1 + torch.erf(t / math.sqrt(2)))

    def _integrate_product_of_gaussians(self, mu, sigma_sq):
        sigma = torch.sqrt(self.sigma ** 2 + sigma_sq)
        return self._phi((mu - self.mu) / sigma) / sigma

    def evaluate(self, t):
        return self._phi((t - self.mu) / self.sigma) / self.sigma

    def batch_evaluate(self, t):
        t_ = t.repeat(self.mu.size(0),1) - self.mu.repeat(t.size(0),1).transpose(1,0)
        t_ = t_ / self.sigma.repeat((t.size(0),1)).transpose(1,0)
        return (self._phi(t_) / self.sigma.repeat((t.size(0),1)).transpose(1,0)).transpose(0,1)

    def integrate_t2_times_psi(self, a, b):
        """Compute integral int_a^b (t**2) * psi(t)."""
        return (self.mu**2 + self.sigma**2) * (
            self._Phi((b - self.mu) / self.sigma) - self._Phi((a - self.mu) / self.sigma)
        ) - (
            self.sigma * (b + self.mu) * self._phi((b - self.mu) / self.sigma)
        ) + (
            self.sigma * (a + self.mu) * self._phi((a - self.mu) / self.sigma)
        )

    def integrate_t_times_psi(self, a, b):
        """Compute integral int_a^b t * psi(t)."""
        return self.mu * (
            self._Phi((b - self.mu) / self.sigma) - self._Phi((a - self.mu) / self.sigma)
        ) - self.sigma * (
            self._phi((b - self.mu) / self.sigma) - self._phi((a - self.mu) / self.sigma)
        )

    def integrate_psi(self, a, b):
        """Compute integral int_a^b psi(t)."""
        return self._Phi((b - self.mu) / self.sigma) - self._Phi((a - self.mu) / self.sigma)

    def integrate_t2_times_psi_gaussian(self, mu, sigma_sq):
        """Compute integral int N(t; mu, sigma_sq) * t**2 * psi(t)."""
        S_tilde = self._integrate_product_of_gaussians(mu, sigma_sq)
        mu_tilde = (
            self.mu * sigma_sq + mu * self.sigma ** 2
        ) / (
            self.sigma ** 2 + sigma_sq
        )
        sigma_sq_tilde = ((self.sigma ** 2) * sigma_sq) / (self.sigma ** 2 + sigma_sq)
        return S_tilde * (mu_tilde ** 2 + sigma_sq_tilde)

    def integrate_t_times_psi_gaussian(self, mu, sigma_sq):
        """Compute integral int N(t; mu, sigma_sq) * t * psi(t)."""
        S_tilde = self._integrate_product_of_gaussians(mu, sigma_sq)
        mu_tilde = (
            self.mu * sigma_sq + mu * self.sigma ** 2
        ) / (
            self.sigma ** 2 + sigma_sq
        )
        return S_tilde * mu_tilde

    def integrate_psi_gaussian(self, mu, sigma_sq):
        """Compute integral int N(t; mu, sigma_sq) * psi(t)."""
        return self._integrate_product_of_gaussians(mu, sigma_sq)


class RetangularBasisFunctions(BasisFunctions):
    """Function phi(t) = Gaussian(t; mu, sigma_sq)."""
    def __init__(self, mu, sigma):
        self.mu = mu.unsqueeze(0)
        self.width = sigma.unsqueeze(0)

    def __repr__(self):
        return f"GaussianBasisFunction(mu={self.mu}, sigma={self.sigma})"

    def __len__(self):
        """Number of basis functions."""
        return self.mu.size(1)

    def batch_evaluate(self, t):
        """
        Evaluate multiple time points against all rectangular basis functions.
        Args:
            t: Tensor of time values to evaluate, shape (num_points,).
        Returns:
            Tensor of evaluations, shape (num_basis, num_points).
        """
        t = t.repeat(self.mu.size(0),1)  # Shape: (1, num_points)
        mu = self.mu.repeat(t.size(0),1).transpose(1,0)  # Shape: (num_basis, 1)
        width = self.width.repeat(t.size(0),1).transpose(1,0)  # Shape: (num_basis, 1)
        return ((t >= (mu - width / 2)) & (t < (mu + width / 2))).float().transpose(0,1)
    
    def _Phi(self, t):
        """
        Compute the step function for a single value of t.
        Args:
            t: A scalar or tensor of time values.
        Returns:
            Tensor of values indicating presence in each basis function's range.
        """
        lower_bounds = self.mu - self.width / 2
        upper_bounds = self.mu + self.width / 2
        return ((t >= lower_bounds) & (t < upper_bounds)).float()

    def evaluate(self, t):
        """
        Evaluate the rectangular basis functions at a single point or array of points.
        Args:
            t: A scalar or 1D tensor of time values.
        Returns:
            Tensor of shape (num_basis,) for scalar input, or (num_basis, num_points) for tensor input.
        """
        if t.ndim == 0:  # Scalar input
            return self._Phi(t)
        else:  # Tensor input
          # Shape: (1, num_points)
            lower_bounds = (self.mu - self.width / 2)  # Shape: (num_basis, 1)
            upper_bounds = (self.mu + self.width / 2)  # Shape: (num_basis, 1)
            return ((t >= lower_bounds) & (t < upper_bounds)).float()