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
use std::iter::zip;
use super::rand_distr::{Distribution, Normal};
use super::DispersedState;
use crate::linalg::allocator::Allocator;
use crate::linalg::{
Const, DefaultAllocator, DimMin, DimMinimum, DimName, DimSub, OMatrix, OVector,
};
use crate::md::StateParameter;
use crate::{NyxError, State};
pub struct MultivariateNormal<S: State>
where
DefaultAllocator: Allocator<f64, S::Size>
+ Allocator<f64, S::Size, S::Size>
+ Allocator<usize, S::Size, S::Size>
+ Allocator<f64, S::VecLength>
+ Allocator<f64, <S::Size as DimMin<S::Size>>::Output>
+ Allocator<f64, <<S::Size as DimMin<S::Size>>::Output as DimSub<Const<1>>>::Output>
+ Allocator<f64, S::Size, <S::Size as DimMin<S::Size>>::Output>
+ Allocator<f64, <S::Size as DimMin<S::Size>>::Output, S::Size>
+ Allocator<f64, <S::Size as DimSub<Const<1>>>::Output>
+ Allocator<f64, S::Size, <S::Size as DimSub<Const<1>>>::Output>,
<DefaultAllocator as Allocator<f64, S::VecLength>>::Buffer: Send,
S::Size: DimMin<S::Size>,
<S::Size as DimMin<S::Size>>::Output: DimSub<Const<1>>,
S::Size: DimSub<Const<1>>,
{
pub template: S,
pub params: Vec<StateParameter>,
pub mean: OVector<f64, DimMinimum<S::Size, S::Size>>,
pub sqrt_s_v: OMatrix<f64, S::Size, DimMinimum<S::Size, S::Size>>,
pub std_norm_distr: Normal<f64>,
}
impl<S: State> MultivariateNormal<S>
where
DefaultAllocator: Allocator<f64, S::Size>
+ Allocator<f64, S::Size, S::Size>
+ Allocator<usize, S::Size, S::Size>
+ Allocator<f64, S::VecLength>
+ Allocator<f64, <S::Size as DimMin<S::Size>>::Output>
+ Allocator<f64, <<S::Size as DimMin<S::Size>>::Output as DimSub<Const<1>>>::Output>
+ Allocator<f64, S::Size, <S::Size as DimMin<S::Size>>::Output>
+ Allocator<f64, <S::Size as DimMin<S::Size>>::Output, S::Size>
+ Allocator<f64, <S::Size as DimSub<Const<1>>>::Output>
+ Allocator<f64, S::Size, <S::Size as DimSub<Const<1>>>::Output>,
<DefaultAllocator as Allocator<f64, S::VecLength>>::Buffer: Send,
S::Size: DimMin<S::Size>,
<S::Size as DimMin<S::Size>>::Output: DimSub<Const<1>>,
S::Size: DimSub<Const<1>>,
{
pub fn new(
template: S,
params: Vec<StateParameter>,
mean: OVector<f64, DimMinimum<S::Size, S::Size>>,
cov: OMatrix<f64, S::Size, S::Size>,
) -> Result<Self, NyxError> {
match cov.eigenvalues() {
None => return Err(NyxError::CovarianceMatrixNotPsd),
Some(evals) => {
for eigenval in &evals {
if *eigenval < 0.0 {
return Err(NyxError::CovarianceMatrixNotPsd);
}
}
}
};
let svd = cov.svd_unordered(false, true);
if svd.v_t.is_none() {
return Err(NyxError::CovarianceMatrixNotPsd);
}
let sqrt_s = svd.singular_values.map(|x| x.sqrt());
let mut sqrt_s_v_t = svd.v_t.unwrap();
for (i, mut col) in sqrt_s_v_t.column_iter_mut().enumerate() {
col *= sqrt_s[i];
}
Ok(Self {
template,
params,
mean,
sqrt_s_v: sqrt_s_v_t.transpose(),
std_norm_distr: Normal::new(0.0, 1.0).unwrap(),
})
}
pub fn zero_mean(
template: S,
params: Vec<StateParameter>,
cov: OMatrix<f64, S::Size, S::Size>,
) -> Result<Self, NyxError>
where
<S::Size as DimMin<S::Size>>::Output: DimName,
{
Self::new(
template,
params,
OVector::<f64, DimMinimum<S::Size, S::Size>>::zeros(),
cov,
)
}
}
impl<S: State> Distribution<DispersedState<S>> for MultivariateNormal<S>
where
DefaultAllocator: Allocator<f64, S::Size>
+ Allocator<f64, S::Size, S::Size>
+ Allocator<usize, S::Size, S::Size>
+ Allocator<f64, S::VecLength>
+ Allocator<f64, <S::Size as DimMin<S::Size>>::Output>
+ Allocator<f64, <<S::Size as DimMin<S::Size>>::Output as DimSub<Const<1>>>::Output>
+ Allocator<f64, S::Size, <S::Size as DimMin<S::Size>>::Output>
+ Allocator<f64, <S::Size as DimMin<S::Size>>::Output, S::Size>
+ Allocator<f64, <S::Size as DimSub<Const<1>>>::Output>
+ Allocator<f64, S::Size, <S::Size as DimSub<Const<1>>>::Output>,
<DefaultAllocator as Allocator<f64, S::VecLength>>::Buffer: Send,
S::Size: DimMin<S::Size>,
<S::Size as DimMin<S::Size>>::Output: DimSub<Const<1>>,
S::Size: DimSub<Const<1>>,
<S::Size as DimMin<S::Size>>::Output: DimName,
{
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> DispersedState<S> {
let x_rng = OVector::<f64, S::Size>::from_fn(|_, _| self.std_norm_distr.sample(rng));
let x = self.sqrt_s_v.transpose() * x_rng + &self.mean;
let mut state = self.template;
let mut actual_dispersions = Vec::new();
for (delta, param) in zip(&x, &self.params) {
actual_dispersions.push((*param, *delta));
let cur_value = state.value(¶m).unwrap();
state.set_value(¶m, cur_value + delta).unwrap();
}
DispersedState {
state,
actual_dispersions,
}
}
}
#[test]
fn test_multivariate_state() {
use crate::cosmic::{Cosm, Orbit};
use crate::linalg::{Matrix6, Vector6};
use crate::time::Epoch;
use rand_pcg::Pcg64Mcg;
let cosm = Cosm::de438();
let eme2k = cosm.frame("EME2000");
let dt = Epoch::from_gregorian_utc_at_midnight(2021, 1, 31);
let state = Orbit::keplerian(8_191.93, 1e-6, 12.85, 306.614, 314.19, 99.887_7, dt, eme2k);
let mean = Vector6::zeros();
let std_dev = Vector6::new(10.0, 10.0, 10.0, 0.2, 0.2, 0.2);
let cov = Matrix6::from_diagonal(&std_dev);
let orbit_generator = state.disperse(mean, cov).unwrap();
let seed = 0;
let rng = Pcg64Mcg::new(seed);
let cnt_too_far: u16 = orbit_generator
.sample_iter(rng)
.take(1000)
.map(|dispersed_state| {
let mut cnt = 0;
for idx in 0..6 {
let val_std_dev = std_dev[idx];
let cur_val = dispersed_state.state.as_vector().unwrap()[idx];
let nom_val = state.as_vector().unwrap()[idx];
if (cur_val - nom_val).abs() > val_std_dev {
cnt += 1;
}
}
cnt
})
.sum::<u16>();
assert_eq!(
cnt_too_far / 6,
329,
"Should have less than 33% of samples being more than 1 sigma away, got {}",
cnt_too_far
);
}