Delete traj_measure.py
Browse files- traj_measure.py +0 -104
traj_measure.py
DELETED
|
@@ -1,104 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import copy
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
class Measure:
|
| 6 |
-
def sample(self, samples=100000, varn=2, points=100):
|
| 7 |
-
# Must be overridden
|
| 8 |
-
pass
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
class BaseMeasure(Measure):
|
| 12 |
-
def __init__(
|
| 13 |
-
self, mu0=0.0, sigma0=1.0, mu1=0.0, sigma1=1.0, q=0.1, q0=0.5, device="cpu"
|
| 14 |
-
):
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
Parameters
|
| 18 |
-
----------
|
| 19 |
-
mu0 : mean of normal distribution of initial state, optional
|
| 20 |
-
The default is 0.0.
|
| 21 |
-
sigma0 : standard deviation of normal distribution of initial state, optional
|
| 22 |
-
The default is 1.0.
|
| 23 |
-
mu1 : DOUBLE, optional
|
| 24 |
-
mean of normal distribution of total variation. The default is 0.0.
|
| 25 |
-
sigma1 : standard deviation of normal distribution of total variation, optional
|
| 26 |
-
The default is 1.0.
|
| 27 |
-
q : DOUBLE, optional
|
| 28 |
-
probability of change of sign in derivative. The default is 0.1.
|
| 29 |
-
q0 : DOUBLE, optional
|
| 30 |
-
probability of initial sign of derivative. The default is 0.5.
|
| 31 |
-
device : 'cpu' or 'cuda', optional
|
| 32 |
-
device on which to run the algorithm. The default is 'cpu'.
|
| 33 |
-
|
| 34 |
-
Returns
|
| 35 |
-
-------
|
| 36 |
-
None.
|
| 37 |
-
|
| 38 |
-
"""
|
| 39 |
-
self.mu0 = mu0
|
| 40 |
-
self.sigma0 = sigma0
|
| 41 |
-
self.mu1 = mu1
|
| 42 |
-
self.sigma1 = sigma1
|
| 43 |
-
self.q = q
|
| 44 |
-
self.q0 = q0
|
| 45 |
-
self.device = device
|
| 46 |
-
|
| 47 |
-
def sample(self, samples=100000, varn=2, points=100):
|
| 48 |
-
"""
|
| 49 |
-
Samples a set of trajectories from the basic measure space, with parameters
|
| 50 |
-
passed to the sampler
|
| 51 |
-
|
| 52 |
-
Parameters
|
| 53 |
-
----------
|
| 54 |
-
points : INT, optional
|
| 55 |
-
number of points per trajectory, including initial one. The default is 1000.
|
| 56 |
-
samples : INT, optional
|
| 57 |
-
number of trajectories. The default is 100000.
|
| 58 |
-
varn : INT, optional
|
| 59 |
-
number of variables per trajectory. The default is 2.
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
Returns
|
| 63 |
-
-------
|
| 64 |
-
signal : samples x varn x points double pytorch tensor
|
| 65 |
-
The sampled signals.
|
| 66 |
-
|
| 67 |
-
"""
|
| 68 |
-
if self.device == "cuda" and not torch.cuda.is_available():
|
| 69 |
-
raise RuntimeError("GPU card or CUDA library not available!")
|
| 70 |
-
|
| 71 |
-
# generate unif RN
|
| 72 |
-
signal = torch.rand(samples, varn, points, device=self.device)
|
| 73 |
-
# first point is special - set to zero for the moment, and set one point to 1
|
| 74 |
-
signal[:, :, 0] = 0.0
|
| 75 |
-
signal[:, :, -1] = 1.0
|
| 76 |
-
# sorting each trajectory
|
| 77 |
-
signal, _ = torch.sort(signal, 2)
|
| 78 |
-
# computing increments and storing them in points 1 to end
|
| 79 |
-
signal[:, :, 1:] = signal[:, :, 1:] - signal[:, :, :-1]
|
| 80 |
-
# generate initial state, according to a normal distribution
|
| 81 |
-
signal[:, :, 0] = self.mu0 + self.sigma0 * torch.randn(signal[:, :, 0].size())
|
| 82 |
-
|
| 83 |
-
# sampling change signs from bernoulli in -1, 1
|
| 84 |
-
derivs = (1 - self.q) * torch.ones(samples, varn, points, device=self.device)
|
| 85 |
-
derivs = 2 * torch.bernoulli(derivs) - 1
|
| 86 |
-
# sampling initial derivative
|
| 87 |
-
derivs[:, :, 0] = self.q0
|
| 88 |
-
derivs[:, :, 0] = 2 * torch.bernoulli(derivs[:, :, 0]) - 1
|
| 89 |
-
# taking the cumulative product along axis 2
|
| 90 |
-
derivs = torch.cumprod(derivs, 2)
|
| 91 |
-
|
| 92 |
-
# sampling total variation
|
| 93 |
-
totvar = torch.pow(
|
| 94 |
-
self.mu1 + self.sigma1 * torch.randn(samples, varn, 1, device=self.device),
|
| 95 |
-
2,
|
| 96 |
-
)
|
| 97 |
-
# multiplying total variation and derivatives and making initial point non-invasive
|
| 98 |
-
derivs = derivs * totvar
|
| 99 |
-
derivs[:, :, 0] = 1.0
|
| 100 |
-
|
| 101 |
-
# computing trajectories by multiplying and then doing a cumulative sum
|
| 102 |
-
signal = signal * derivs
|
| 103 |
-
signal = torch.cumsum(signal, 2)
|
| 104 |
-
return signal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|