nyx_space/od/process/solution/smooth.rs
1/*
2 Nyx, blazing fast astrodynamics
3 Copyright (C) 2018-onwards Christopher Rabotin <christopher.rabotin@gmail.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19use crate::linalg::allocator::Allocator;
20use crate::linalg::{DefaultAllocator, DimName};
21use crate::md::trajectory::Interpolatable;
22pub use crate::od::estimate::*;
23pub use crate::od::*;
24use anise::prelude::Almanac;
25use msr::sensitivity::TrackerSensitivity;
26use std::ops::Add;
27
28use super::ODSolution;
29
30impl<StateType, EstType, MsrSize, Trk> ODSolution<StateType, EstType, MsrSize, Trk>
31where
32 StateType: Interpolatable + Add<OVector<f64, <StateType as State>::Size>, Output = StateType>,
33 EstType: Estimate<StateType>,
34 MsrSize: DimName,
35 Trk: TrackerSensitivity<StateType, StateType>,
36 <DefaultAllocator as Allocator<<StateType as State>::VecLength>>::Buffer<f64>: Send,
37 DefaultAllocator: Allocator<<StateType as State>::Size>
38 + Allocator<<StateType as State>::VecLength>
39 + Allocator<MsrSize>
40 + Allocator<MsrSize, <StateType as State>::Size>
41 + Allocator<MsrSize, MsrSize>
42 + Allocator<<StateType as State>::Size, <StateType as State>::Size>
43 + Allocator<<StateType as State>::Size, MsrSize>,
44{
45 /// Smoothes this OD solution, returning a new OD solution and the filter-smoother consistency ratios, with updated **postfit** residuals, and where the ratio now represents the filter-smoother consistency ratio.
46 ///
47 /// Notes:
48 /// 1. Gains will be scrubbed because the smoother process does not recompute the gain.
49 /// 2. Prefit residuals, ratios, and measurement covariances are not updated, as these depend on the filtering process.
50 /// 3. Note: this function consumes the current OD solution to prevent reusing the wrong one.
51 ///
52 ///
53 /// To assess whether the smoothing process improved the solution, compare the RMS of the postfit residuals from the filter and the smoother process.
54 ///
55 /// # Filter-Smoother consistency ratio
56 ///
57 /// The **filter-smoother consistency ratio** is used to evaluate the consistency between the state estimates produced by a filter (e.g., Kalman filter) and a smoother.
58 /// This ratio is called "filter smoother consistency test" in the ODTK MathSpec.
59 ///
60 /// It is computed as follows:
61 ///
62 /// #### 1. Define the State Estimates
63 /// **Filter state estimate**:
64 /// $ \hat{X}_{f,k} $
65 /// This is the state estimate at time step $ k $ from the filter.
66 ///
67 /// **Smoother state estimate**:
68 /// $ \hat{X}_{s,k} $
69 /// This is the state estimate at time step $ k $ from the smoother.
70 ///
71 /// #### 2. Define the Covariances
72 ///
73 /// **Filter covariance**:
74 /// $ P_{f,k} $
75 /// This is the covariance of the state estimate at time step $ k $ from the filter.
76 ///
77 /// **Smoother covariance**:
78 /// $ P_{s,k} $
79 /// This is the covariance of the state estimate at time step $ k $ from the smoother.
80 ///
81 /// #### 3. Compute the Differences
82 ///
83 /// **State difference**:
84 /// $ \Delta X_k = \hat{X}_{s,k} - \hat{X}_{f,k} $
85 ///
86 /// **Covariance difference**:
87 /// $ \Delta P_k = P_{s,k} - P_{f,k} $
88 ///
89 /// #### 4. Calculate the Consistency Ratio
90 /// For each element $ i $ of the state vector, compute the ratio:
91 ///
92 /// $$
93 /// R_{i,k} = \frac{\Delta X_{i,k}}{\sqrt{\Delta P_{i,k}}}
94 /// $$
95 ///
96 /// #### 5. Evaluate Consistency
97 /// - If $ |R_{i,k}| \leq 3 $ for all $ i $ and $ k $, the filter-smoother consistency test is satisfied, indicating good consistency.
98 /// - If $ |R_{i,k}| > 3 $ for any $ i $ or $ k $, the test fails, suggesting potential modeling inconsistencies or issues with the estimation process.
99 ///
100 pub fn smooth(self, almanac: Arc<Almanac>) -> Result<Self, ODError> {
101 let l = self.estimates.len() - 1;
102
103 let mut smoothed = Self {
104 estimates: Vec::with_capacity(self.estimates.len()),
105 residuals: Vec::with_capacity(self.residuals.len()),
106 gains: Vec::with_capacity(self.estimates.len()),
107 filter_smoother_ratios: Vec::with_capacity(self.estimates.len()),
108 devices: self.devices.clone(),
109 measurement_types: self.measurement_types.clone(),
110 };
111
112 // Set the first item of the smoothed estimates to the last estimate (we cannot smooth the very last estimate)
113 smoothed
114 .estimates
115 .push(self.estimates.last().unwrap().clone());
116 smoothed
117 .residuals
118 .push(self.residuals.last().unwrap().clone());
119 smoothed.gains.push(None);
120 smoothed.filter_smoother_ratios.push(None);
121
122 loop {
123 let k = l - smoothed.estimates.len();
124 // Borrow the previously smoothed estimate of the k+1 estimate
125 let sm_est_kp1 = &self.estimates[k + 1];
126 let x_kp1_l = sm_est_kp1.state_deviation();
127 let p_kp1_l = sm_est_kp1.covar();
128 // Borrow the k-th estimate, which we're smoothing with the next estimate
129 let est_k = &self.estimates[k];
130 // Borrow the k+1-th estimate, which we're smoothing with the next estimate
131 let est_kp1 = &self.estimates[k + 1];
132
133 // Compute the STM between both steps taken by the filter
134 // The filter will reset the STM between each estimate it computes, time update or measurement update.
135 // Therefore, the STM is simply the inverse of the one we used previously.
136 // est_kp1 is the estimate that used the STM from time k to time k+1. So the STM stored there
137 // is \Phi_{k \to k+1}. Let's invert that.
138 let phi_kp1_k = &est_kp1
139 .stm()
140 .clone()
141 .try_inverse()
142 .ok_or(ODError::SingularStateTransitionMatrix)?;
143
144 // Compute smoothed state deviation
145 let x_k_l = phi_kp1_k * x_kp1_l;
146 // Compute smoothed covariance
147 let p_k_l = phi_kp1_k * p_kp1_l * phi_kp1_k.transpose();
148 // Store into vector
149 let mut smoothed_est_k = est_k.clone();
150 // Compute the smoothed state deviation
151 smoothed_est_k.set_state_deviation(x_k_l);
152 // Compute the smoothed covariance
153 smoothed_est_k.set_covar(p_k_l);
154 // Recompute the residual if available.
155 if let Some(mut residual) = self.residuals[k + 1].clone() {
156 let tracker = residual
157 .tracker
158 .as_ref()
159 .expect("tracker unset in smoother process");
160
161 let device = smoothed
162 .devices
163 .get_mut(tracker)
164 .expect("unknown tracker in smoother process");
165
166 let new_state_est = smoothed_est_k.state();
167 let epoch = new_state_est.epoch();
168
169 if let Some(computed_meas) =
170 device.measure_instantaneous(new_state_est, None, almanac.clone())?
171 {
172 // Only recompute the computed observation from the update state estimate.
173 residual.computed_obs = computed_meas
174 .observation::<MsrSize>(&residual.msr_types)
175 - device.measurement_bias_vector::<MsrSize>(&residual.msr_types, epoch)?;
176
177 // Update the postfit residual.
178 residual.postfit = &residual.real_obs - &residual.computed_obs;
179
180 // Store the updated data.
181 smoothed.residuals.push(Some(residual));
182 } else {
183 smoothed.residuals.push(None);
184 }
185 } else {
186 smoothed.residuals.push(None);
187 }
188
189 // Compute the filter-smoother consistency ratio.
190 let delta_covar = est_k.covar() - smoothed_est_k.covar();
191 let delta_state =
192 est_k.state().to_state_vector() - smoothed_est_k.state().to_state_vector();
193
194 let fs_ratios = OVector::<f64, <StateType as State>::Size>::from_iterator(
195 delta_state
196 .iter()
197 .enumerate()
198 .map(|(i, dx)| dx / delta_covar[(i, i)].sqrt()),
199 );
200
201 smoothed.estimates.push(smoothed_est_k);
202 smoothed.filter_smoother_ratios.push(Some(fs_ratios));
203 // Set all gains to None.
204 smoothed.gains.push(None);
205
206 if smoothed.estimates.len() == self.estimates.len() {
207 break;
208 }
209 }
210
211 // Note that we have yet to reverse the list, so we print them backward
212 info!(
213 "Smoothed {} estimates (from {} to {})",
214 smoothed.estimates.len(),
215 smoothed.estimates.last().unwrap().epoch(),
216 smoothed.estimates[0].epoch(),
217 );
218
219 // Now, let's add all of the other estimates so that the same indexing can be done
220 // between all the estimates and the smoothed estimates
221 if smoothed.estimates.len() < self.estimates.len() {
222 // Add the estimates that might have been skipped.
223 let mut k = self.estimates.len() - smoothed.estimates.len();
224 loop {
225 smoothed.estimates.push(self.estimates[k].clone());
226 if k == 0 {
227 break;
228 }
229 k -= 1;
230 }
231 }
232
233 // And reverse to maintain the order of estimates
234 smoothed.estimates.reverse();
235 smoothed.residuals.reverse();
236 smoothed.filter_smoother_ratios.reverse();
237
238 Ok(smoothed)
239 }
240}