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