statime-algo 0.1.1

Full-featured implementation of NTP with NTS support
Documentation
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
use statime_base::{ClockId, DirectedLinkId, Duration, LinkId, TAI, TimeSnapshot, Timestamp};

use crate::{
    AlgoError,
    matrix::Matrix,
    storage::{
        EstimatorLinkStorage, ExternalClockStorage, InternalClockStorage, KalmanStorageBase,
    },
};

#[cfg(not(feature = "std"))]
use crate::float_polyfill::FloatPolyfill;

#[derive(Debug, Clone)]
struct ExternalClockList<L>(L);

impl<L: ExternalClockStorage> ExternalClockList<L> {
    fn new() -> Self {
        ExternalClockList(L::new())
    }

    /// Returns true if the given clock is known as an external clock.
    fn contains(&self, id: ClockId) -> bool {
        self.0.contains(&id)
    }

    /// Add a new external clock to the list.
    fn add(&mut self, id: ClockId) -> Result<(), AlgoError> {
        if self.contains(id) {
            Err(AlgoError::ClockAlreadyExists(id))
        } else {
            self.0.push(id);
            Ok(())
        }
    }

    /// Remove an existing external clock from the list.
    fn remove(&mut self, id: ClockId) -> Result<(), AlgoError> {
        if let Some(pos) = self.0.iter().position(|&x| x == id) {
            self.0.remove(pos);
            Ok(())
        } else {
            Err(AlgoError::UnknownClock(id))
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct ClockInfo {
    id: ClockId,
    base_index: usize,
    wander: f64,
}

impl ClockInfo {
    const SIZE: usize = 2;

    fn offset_index(self) -> usize {
        self.base_index
    }

    fn frequency_index(self) -> usize {
        self.base_index + 1
    }
}

#[derive(Debug, Clone)]
struct ClockInfoList<L>(L);

impl<L: InternalClockStorage> ClockInfoList<L> {
    fn new() -> Self {
        ClockInfoList(L::new())
    }

    /// Checks if the given clock id exists in the current list.
    fn contains(&self, id: ClockId) -> bool {
        self.0.iter().any(|info| info.id == id)
    }

    /// Update the base indices of all clocks that have a base index greater
    /// than `from`, by subtracting `delta` from them.
    fn update_indices(&mut self, from: usize, delta: usize) {
        for info in &mut *self.0 {
            if info.base_index > from {
                info.base_index -= delta;
            }
        }
    }

    /// Remove the clock info for a given id, if it exists.
    ///
    /// Updates the indices for clocks and links where needed.
    ///
    /// Returns the removed clock info.
    fn remove<LL: EstimatorLinkStorage>(
        &mut self,
        id: ClockId,
        link_info: &mut LinkInfoList<LL>,
    ) -> Result<ClockInfo, AlgoError> {
        let removed = if let Some(pos) = self.0.iter().position(|info| info.id == id) {
            Ok(self.0.remove(pos))
        } else {
            Err(AlgoError::UnknownClock(id))
        }?;

        self.update_indices(removed.base_index, ClockInfo::SIZE);
        link_info.update_indices(removed.base_index, ClockInfo::SIZE);

        Ok(removed)
    }

    /// Add a new clock info to the list, if it doesn't already exist.
    fn add(&mut self, info: ClockInfo) -> Result<(), AlgoError> {
        if self.0.iter().any(|existing| existing.id == info.id) {
            Err(AlgoError::ClockAlreadyExists(info.id))
        } else {
            self.0.push(info);
            Ok(())
        }
    }

    /// Iterate over all clocks in the list.
    fn iter(&self) -> impl Iterator<Item = &ClockInfo> {
        self.0.iter()
    }
}

#[derive(Debug, Clone, Copy)]
pub struct LinkInfo {
    id: LinkId,
    index: usize,
    /// Fraction of the link delay that we assume the error increases by every second
    decay_rate: f64,
}

impl LinkInfo {
    const SIZE: usize = 1;
}

#[derive(Debug, Clone)]
struct LinkInfoList<L>(L);

impl<L: EstimatorLinkStorage> LinkInfoList<L> {
    fn new() -> Self {
        LinkInfoList(L::new())
    }

    /// Update the indices of all links that have an index greater than `from`, by subtracting `delta` from them.
    fn update_indices(&mut self, from: usize, delta: usize) {
        for info in &mut *self.0 {
            if info.index > from {
                info.index -= delta;
            }
        }
    }

    /// Remove the link info for a given id, if it exists.
    ///
    /// Updates the indices for links and clocks where needed.
    ///
    /// Returns the removed link info.
    fn remove<CL: InternalClockStorage>(
        &mut self,
        id: LinkId,
        clock_info: &mut ClockInfoList<CL>,
    ) -> Result<LinkInfo, AlgoError> {
        let removed = if let Some(pos) = self.0.iter().position(|info| info.id == id) {
            Ok(self.0.remove(pos))
        } else {
            Err(AlgoError::UnknownLink(id))
        }?;

        self.update_indices(removed.index, LinkInfo::SIZE);
        clock_info.update_indices(removed.index, LinkInfo::SIZE);

        Ok(removed)
    }

    /// Add a new link info to the list, if it doesn't already exist.
    fn add(&mut self, info: LinkInfo) -> Result<(), AlgoError> {
        if self.0.iter().any(|existing| existing.id == info.id) {
            Err(AlgoError::LinkAlreadyExists(info.id))
        } else {
            self.0.push(info);
            Ok(())
        }
    }

    /// Iterate over all links in the list.
    fn iter(&self) -> impl Iterator<Item = &LinkInfo> {
        self.0.iter()
    }
}

/// Represents the state of the estimator at a given point in time.
///
/// Note how mutating methods on this all consume self. This is on purpose,
/// as it makes it easier to reason about the state of the estimator. This
/// does however mean that errors result in the state being lost. In general
/// it is expected that most errors are unrecoverable. Typically one would
/// keep a list of some of the most recent states though, they could be used
/// for error recovery, but also for tracability and debugging.
#[derive(Debug, Clone)]
pub struct EstimatorState<Storage: KalmanStorageBase> {
    time: Timestamp<TAI>,
    state: Matrix<Storage::MatrixStorage>,
    uncertainty: Matrix<Storage::MatrixStorage>,
    clock_info: ClockInfoList<Storage::InternalClockStorage>,
    external_clocks: ExternalClockList<Storage::ExternalClockStorage>,
    link_info: LinkInfoList<Storage::EstimatorLinkStorage>,
}

/// Represents an uncertain value, with a best estimate and an uncertainty (standard deviation).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct UncertainValue {
    /// Best estimate of the value
    pub value: f64,
    /// The variance of the value.
    pub variance: f64,
}

impl UncertainValue {
    /// Add some additional uncertainty to the uncertain value.
    pub fn add_uncertainty(self, extra_uncertainty: f64) -> UncertainValue {
        UncertainValue {
            value: self.value,
            // Addition formula for uncertainty...
            variance: (self.variance + extra_uncertainty.powi(2)),
        }
    }

    pub(crate) fn uncertainty(&self) -> f64 {
        self.variance.sqrt()
    }
}

/// Convert from a tuple of (value, uncertainty) to an `UncertainValue`.
impl From<(f64, f64)> for UncertainValue {
    fn from(value: (f64, f64)) -> Self {
        UncertainValue {
            value: value.0,
            variance: value.1.powi(2),
        }
    }
}

impl<Storage: KalmanStorageBase> EstimatorState<Storage> {
    /// Create a new empty estimator state at the given timestamp.
    ///
    /// This state has no clocks or links contained in it.
    #[must_use]
    pub fn empty(time: Timestamp<TAI>) -> Self {
        Self {
            time,
            state: Matrix::zero(0, 1),
            uncertainty: Matrix::zero(0, 0),
            clock_info: ClockInfoList::new(),
            external_clocks: ExternalClockList::new(),
            link_info: LinkInfoList::new(),
        }
    }

    /// Progress the estimator state to the new timestamp.
    ///
    /// # Errors
    /// Returns an error if the new time provided is before the current time of the estimator.
    pub fn progress_time(mut self, new_time: Timestamp<TAI>) -> Result<Self, AlgoError> {
        let delta_t = new_time - self.time;

        // time should not move backwards
        if delta_t < Duration::ZERO {
            return Err(AlgoError::NonMonotonicTimeProgression {
                from: self.time,
                to: new_time,
            });
        }

        // no time change, return state as is
        if new_time == self.time {
            return Ok(self);
        }

        let delta_t = delta_t.as_seconds();

        let mut update = Matrix::identity(self.state.rows());
        let mut noise = Matrix::zero(self.state.rows(), self.state.rows());

        // For each clock, we need to determine a value for the update and noise matrices.
        for clock_info in self.clock_info.iter() {
            update[(clock_info.offset_index(), clock_info.frequency_index())] = delta_t;
            // We need to square wander as we store it in units of ppm per second,
            // which is a standard deviation (effectively).
            // The powers of time that contribute to clock process noise can be
            // derived from imposing the random-walk requirement on the frequency,
            // which gives linear relationship for the frequency variance increase,
            // and then requiring that two updates for shorter intervals give the same
            // result as one update for the sum of those intervals.
            noise[(clock_info.offset_index(), clock_info.offset_index())] =
                delta_t.powi(3) * clock_info.wander.powi(2) / 3.;
            noise[(clock_info.offset_index(), clock_info.frequency_index())] =
                delta_t.powi(2) * clock_info.wander.powi(2) / 2.;
            noise[(clock_info.frequency_index(), clock_info.offset_index())] =
                delta_t.powi(2) * clock_info.wander.powi(2) / 2.;
            noise[(clock_info.frequency_index(), clock_info.frequency_index())] =
                delta_t * clock_info.wander.powi(2);
        }

        for link_info in self.link_info.iter() {
            noise[(link_info.index, link_info.index)] =
                delta_t * ((link_info.decay_rate * self.state[(link_info.index, 0)]).powi(2));
        }

        self.time = new_time;
        self.state = &update * &self.state;
        self.uncertainty = &update * &self.uncertainty * update.transpose() + noise;

        self.validate_state()
    }

    /// Absorb a change in frequency of a clock.
    ///
    /// This function assumes steering happens at the current estimator time.
    ///
    /// # Errors
    /// Returns an error if the steered clock is unknown to the estimator.
    pub fn absorb_frequency_steer(
        mut self,
        steered_clock: ClockId,
        frequency_change: f64,
    ) -> Result<Self, AlgoError> {
        let clock_info = self.get_clock_info(steered_clock)?;
        let frequency_index = clock_info.frequency_index();
        self.state[(frequency_index, 0)] += frequency_change;
        self.validate_state()
    }

    /// Absorb a step change in the phase of a clock.
    ///
    /// This function assumes steering happens at the current estimator time.
    ///
    /// # Errors
    /// Returns an error if the steered clock is unknown to the estimator.
    pub fn absorb_offset_change(
        mut self,
        steered_clock: ClockId,
        offset_change: f64,
    ) -> Result<Self, AlgoError> {
        let clock_info = self.get_clock_info(steered_clock)?;
        let offset_index = clock_info.offset_index();
        self.state[(offset_index, 0)] += offset_change;
        self.validate_state()
    }

    /// Absorb a step change in the phase of a clock which is also used for the estimator time.
    ///
    /// This function assumes steering happens at the current estimator time.
    ///
    /// # Errors
    /// Returns an error if the steered clock is unknown to the estimator.
    pub fn absorb_system_clock_offset_change(
        mut self,
        steered_clock: ClockId,
        offset_change: Duration,
    ) -> Result<Self, AlgoError> {
        let clock_info = self.get_clock_info(steered_clock)?;
        let offset_index = clock_info.offset_index();
        self.state[(offset_index, 0)] += offset_change.as_seconds();
        self.time += offset_change;
        self.validate_state()
    }

    /// Predicted the outcome of a measurement on the given link in the given
    /// direction.
    ///
    /// Assumes the measurement happens at the time the estimator state is
    /// currently set to.
    ///
    /// # Errors
    /// Returns an error if either of the clock ids, or the link is unknown.
    /// Also returns an error if both referenced clocks are external.
    pub fn prediction(
        &self,
        direction: DirectedLinkId,
        delay_link: bool,
    ) -> Result<UncertainValue, AlgoError> {
        let measurement_projection = self.measurement_projection(direction, delay_link)?;

        Ok(UncertainValue {
            value: (&measurement_projection * &self.state)[(0, 0)],
            variance: (&measurement_projection
                * &self.uncertainty
                * measurement_projection.transpose())[(0, 0)],
        })
    }

    /// Add a new measurement to the estimator state.
    ///
    /// Assumes the measurements happens at the time the estimator state is
    /// currently set to.
    ///
    /// # Errors
    /// Returns an error if either of the clock ids, or the link is unknown.
    /// Also returns an error if both referenced clocks are external.
    pub fn measurement(
        mut self,
        direction: DirectedLinkId,
        offset: UncertainValue,
        delay_link: bool,
        system_clock: ClockId,
    ) -> Result<(Self, f64), AlgoError> {
        let measurement_projection = self.measurement_projection(direction, delay_link)?;

        let expected = &measurement_projection * &self.state;
        let difference = Matrix::<Storage::MatrixStorage>::from(offset.value) - expected;
        // The uncertainty of the difference between measurement and prediction is the sum of
        // the uncertainty of the measurement, and the uncertainty on the prediction. The
        // prediction uncertainty can be shown to follow from multiplying the state uncertainty
        // from both sides by the measurement projection. Intuitively this is because the
        // uncertainty is sort of a square of the state.
        let difference_covariance =
            &measurement_projection * &self.uncertainty * measurement_projection.transpose()
                + Matrix::from(offset.variance);

        // Intuitively, the multiplication with the measurement gives the contribution
        // for each part of the state to the uncertainty of the measurement prediction.
        // The division then normalizes that to weights on how large the change to each
        // part of the state needs to be. This makes sense because where our prediction
        // has more uncertainty from, the measurement should weigh more.
        let update_strength =
            &self.uncertainty * measurement_projection.transpose() / difference_covariance[(0, 0)];

        // This is simply using the strength we calculated before to update the state
        self.state = &self.state + &update_strength * difference;

        // However I don't have a good intuition why this would be its uncertainty. It
        // is derived well on wikipedia, and when having questions I would suggest looking
        // at its page on kalman filters.
        let prev_step_proportionality =
            Matrix::identity(self.state.rows()) - &update_strength * measurement_projection;
        self.uncertainty = (&prev_step_proportionality
            * &self.uncertainty
            * prev_step_proportionality.transpose()
            + &update_strength * offset.variance * update_strength.transpose())
        .symmetrize()?;

        let clock_info = self.get_clock_info(system_clock)?;
        let importance_contrib = update_strength[(clock_info.offset_index(), 0)].abs();

        Ok((self.validate_state()?, importance_contrib))
    }

    /// Provides the matrix that projects the state and uncertainty matrices to a
    /// prediction for a measurment on the given link in the given direction.
    ///
    /// # Errors
    /// Returns an error if either of the clock ids, or the link is unknown.
    /// Also returns an error if both referenced clocks are external.
    fn measurement_projection(
        &self,
        direction: DirectedLinkId,
        delay_link: bool,
    ) -> Result<Matrix<Storage::MatrixStorage>, AlgoError> {
        let mut measurement_projection = Matrix::zero(1, self.state.rows());
        let from = direction.from_clock();
        let to = direction.to_clock();
        let from_external = self.external_clocks.contains(from);
        let to_external = self.external_clocks.contains(to);
        if from_external && to_external {
            return Err(AlgoError::BothClocksExternal(from, to));
        }
        if !from_external {
            let from_clock_info = self.get_clock_info(from)?;
            measurement_projection[(0, from_clock_info.offset_index())] = -1.0;
        }
        if !to_external {
            let to_clock_info = self.get_clock_info(to)?;
            measurement_projection[(0, to_clock_info.offset_index())] = 1.0;
        }
        if delay_link {
            let link_delay_info = self.get_link_info(direction.link_id())?;
            measurement_projection[(0, link_delay_info.index)] = 1.0;
        }
        Ok(measurement_projection)
    }

    /// Add an external clock to the estimator state.
    ///
    /// # Errors
    /// Returns an error if the clock is already known to the estimator.
    pub fn add_external_clock(mut self, id: ClockId) -> Result<Self, AlgoError> {
        // check in clock info as well
        if self.clock_info.contains(id) {
            return Err(AlgoError::ClockAlreadyExists(id));
        }

        self.external_clocks.add(id)?;

        Ok(self)
    }

    /// Remove an external clock from the estimator state.
    ///
    /// # Errors
    /// Returns an error if the clock in question is not an external clock known to the estimator.
    pub fn remove_external_clock(mut self, id: ClockId) -> Result<Self, AlgoError> {
        self.external_clocks.remove(id)?;

        Ok(self)
    }

    /// Add a new clock to the estimator state.'
    ///
    /// To add a new clock you must provide the initial values for the offset,
    /// frequency and wander of the clock.
    ///
    /// # Errors
    /// Returns an erorr if the clock in question is already known to the estimator.
    pub fn add_clock(
        mut self,
        id: ClockId,
        initial_offset: UncertainValue,
        initial_frequency: UncertainValue,
        initial_wander: f64,
    ) -> Result<Self, AlgoError> {
        // check in external clocks as well
        if self.external_clocks.contains(id) {
            return Err(AlgoError::ClockAlreadyExists(id));
        }

        let new_clock_info = ClockInfo {
            id,
            base_index: self.state.rows(),
            wander: initial_wander,
        };

        self.clock_info.add(new_clock_info)?;
        self.state = self
            .state
            .extend_vec([initial_offset.value, initial_frequency.value])?;
        self.uncertainty = self.uncertainty.extend([
            [initial_offset.variance, 0.0],
            [0.0, initial_frequency.variance],
        ]);

        self.validate_state()
    }

    /// Remove a clock from the estimator state.
    ///
    /// # Errors
    /// Returns an error if the clock in question is not known to the estimator.
    pub fn remove_clock(mut self, id: ClockId) -> Result<Self, AlgoError> {
        let clock_info = self.clock_info.remove(id, &mut self.link_info)?;

        self.state = self
            .state
            .splice_vec(clock_info.base_index, ClockInfo::SIZE)?;
        self.uncertainty = self
            .uncertainty
            .splice_square(clock_info.base_index, ClockInfo::SIZE)?;

        self.validate_state()
    }

    /// Add a new link to the estimator state.
    ///
    /// The decay rate is the amount the uncertainty on the link delay increases every second on this link.
    ///
    /// # Errors
    /// Returns an error if the link in question is already known or if any of
    /// the clocks in the link are unknown to the estimator.
    pub fn add_link(
        mut self,
        id: LinkId,
        initial_delay: UncertainValue,
        decay_rate: f64,
    ) -> Result<Self, AlgoError> {
        if !self.is_known_clock(id.first_clock()) {
            return Err(AlgoError::UnknownClock(id.first_clock()));
        }

        if !self.is_known_clock(id.second_clock()) {
            return Err(AlgoError::UnknownClock(id.second_clock()));
        }

        let new_link_info = LinkInfo {
            id,
            index: self.state.rows(),
            decay_rate,
        };

        self.link_info.add(new_link_info)?;
        self.state = self.state.extend_vec([initial_delay.value])?;
        self.uncertainty = self.uncertainty.extend([[initial_delay.variance]]);

        self.validate_state()
    }

    /// Remove a link from the estimator state.
    ///
    /// # Errors
    /// Returns an error if the link in question is unknown.
    pub fn remove_link(mut self, id: LinkId) -> Result<Self, AlgoError> {
        let removed_info = self.link_info.remove(id, &mut self.clock_info)?;
        self.state = self.state.splice_vec(removed_info.index, LinkInfo::SIZE)?;
        self.uncertainty = self
            .uncertainty
            .splice_square(removed_info.index, LinkInfo::SIZE)?;

        self.validate_state()
    }

    /// Get the current offset of a clock in the state, along with the uncertainty of that offset.
    ///
    /// # Errors
    /// Returns an error if the clock in question is unknown.
    pub fn clock_offset(&self, id: ClockId) -> Result<UncertainValue, AlgoError> {
        let clock_info = self.get_clock_info(id)?;
        Ok(UncertainValue {
            value: self.state[(clock_info.offset_index(), 0)],
            variance: self.uncertainty[(clock_info.offset_index(), clock_info.offset_index())],
        })
    }

    /// Get the current frequency of a clock in the state, along with the uncertainty of that frequency.
    ///
    /// # Errors
    /// Returns an error if the clock in question is unknown.
    pub fn clock_frequency(&self, id: ClockId) -> Result<UncertainValue, AlgoError> {
        let clock_info = self.get_clock_info(id)?;
        Ok(UncertainValue {
            value: self.state[(clock_info.frequency_index(), 0)],
            variance: self.uncertainty
                [(clock_info.frequency_index(), clock_info.frequency_index())],
        })
    }

    pub fn clock_snapshot(&self, id: ClockId) -> Result<TimeSnapshot, AlgoError> {
        let clock_info = self.get_clock_info(id)?;
        Ok(TimeSnapshot {
            precision: Duration::from_seconds_nanos(0, 1),
            root_delay: Duration::ZERO,
            root_variance_base_time: self.time,
            root_variance_base: self.uncertainty
                [(clock_info.offset_index(), clock_info.offset_index())],
            root_variance_linear: 2.0
                * self.uncertainty[(clock_info.offset_index(), clock_info.frequency_index())],
            root_variance_quadratic: self.uncertainty
                [(clock_info.frequency_index(), clock_info.frequency_index())],
            root_variance_cubic: clock_info.wander.powi(2),
            leap_indicator: None,
            accumulated_steps: Duration::ZERO,
            accumulated_steps_threshold: None,
        })
    }

    /// Get an estimated duration until the error on this clock hits a given bound
    pub fn time_to_error_bound_exceedance(
        &self,
        id: ClockId,
        bound: Duration,
    ) -> Result<Duration, AlgoError> {
        // 2^16 seconds, which is the largest power of 2 number of seconds smaller
        // than a single day.
        const ABSOLUTE_MAX_EXCEEDANCE_TIME: f64 = 65_536.0;
        // How precise we want to calculate the duration.
        const PRECISION: f64 = 1e-3;

        let clock_info = self.get_clock_info(id)?;
        let target = bound.as_seconds().powi(2);
        // We do a binary search on an approximation of the error of the clock after progressing time.
        // For this we use the expansion of the progress time function, which combines the wander, frequency,
        // and offset uncertainties, together with their correlations.
        let c1 = self.uncertainty[(clock_info.offset_index(), clock_info.offset_index())];
        // We force this component up to ensure all components are positive, and therefore the
        // estimate is always increasing. This will allow us to do binary searching later.
        let c2 =
            self.uncertainty[(clock_info.offset_index(), clock_info.frequency_index())].max(0.0);
        let c3 = self.uncertainty[(clock_info.frequency_index(), clock_info.frequency_index())];
        let c4 = clock_info.wander.powi(2);
        let eval = |t: f64| c1 + c2 * t + c3 * t.powi(2) + c4 * t.powi(3);
        let mut low = 0.0;
        let mut high = 1.0;
        // Find a sufficiently high upper bound
        while eval(high) < target {
            if high >= ABSOLUTE_MAX_EXCEEDANCE_TIME {
                return Ok(Duration::from_f64_seconds(ABSOLUTE_MAX_EXCEEDANCE_TIME));
            }

            low = high;
            high *= 2.0;
        }

        while high - low > PRECISION * low {
            let mid = high.midpoint(low);
            if eval(mid) < target {
                low = mid;
            } else {
                high = mid;
            }
        }

        Ok(Duration::from_f64_seconds(low))
    }

    /// Get the current delay of a link in the state, along with the uncertainty of that delay.
    ///
    /// # Errors
    /// Returns an error if the link in question is unknown.
    #[cfg(test)]
    pub fn link_delay(&self, id: LinkId) -> Result<UncertainValue, AlgoError> {
        let link_info = self.get_link_info(id)?;
        Ok(UncertainValue {
            value: self.state[(link_info.index, 0)],
            variance: self.uncertainty[(link_info.index, link_info.index)],
        })
    }

    /// Is a given clock internal
    #[must_use]
    pub fn is_internal_clock(&self, id: ClockId) -> bool {
        self.clock_info.iter().any(|info| info.id == id)
    }

    /// Is a given clock external
    #[must_use]
    pub fn is_external_clock(&self, id: ClockId) -> bool {
        self.external_clocks.contains(id)
    }

    /// Is a given clock known to the estimator
    #[must_use]
    pub fn is_known_clock(&self, id: ClockId) -> bool {
        self.is_internal_clock(id) || self.is_external_clock(id)
    }

    pub(crate) fn current_time(&self) -> Timestamp<TAI> {
        self.time
    }

    fn get_clock_info(&self, id: ClockId) -> Result<&ClockInfo, AlgoError> {
        self.clock_info
            .iter()
            .find(|info| info.id == id)
            .ok_or(AlgoError::UnknownClock(id))
    }

    fn get_link_info(&self, id: LinkId) -> Result<&LinkInfo, AlgoError> {
        self.link_info
            .iter()
            .find(|info| info.id == id)
            .ok_or(AlgoError::UnknownLink(id))
    }

    /// Check the state does not contain any NaN's
    fn validate_state(self) -> Result<Self, AlgoError> {
        if self.state.contains_nan() || self.uncertainty.contains_nan() {
            Err(AlgoError::InternalError)
        } else {
            Ok(self)
        }
    }
}

#[cfg(all(test, feature = "std"))]
#[allow(clippy::float_cmp, reason = "Test code")]
mod tests {
    use crate::storage::StdKalmanStorage;

    use super::*;

    #[test]
    fn test_add_clock() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 1.0).into(), (2.0, 3.0).into(), 1e-8)
            .unwrap();
        assert_eq!(state.clock_offset(clock_1).unwrap().value, 0.0);
        assert_eq!(state.clock_offset(clock_1).unwrap().variance, 1.0);
        assert_eq!(state.clock_frequency(clock_1).unwrap().value, 2.0);
        assert_eq!(state.clock_frequency(clock_1).unwrap().variance, 9.0);

        assert_eq!(
            state.clone().add_external_clock(clock_1).unwrap_err(),
            AlgoError::ClockAlreadyExists(clock_1)
        );
        let state = state.add_external_clock(clock_2).unwrap();

        assert_eq!(
            state
                .add_clock(clock_2, (0.0, 1.0).into(), (2.0, 3.0).into(), 1e-8)
                .unwrap_err(),
            AlgoError::ClockAlreadyExists(clock_2)
        );
    }

    #[test]
    fn test_clock_removal() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();
        let clock_3 = ClockId::new();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 1.0).into(), (0.0, 1.0).into(), 1e-8)
            .unwrap()
            .add_external_clock(clock_2)
            .unwrap();

        // remove non-existing clock should fail
        assert_eq!(
            state.clone().remove_clock(clock_3).unwrap_err(),
            AlgoError::UnknownClock(clock_3)
        );

        // remove existing clock via external clock removal should fail
        assert_eq!(
            state.clone().remove_external_clock(clock_1).unwrap_err(),
            AlgoError::UnknownClock(clock_1)
        );

        // remove existing external clock via internal clock removal should fail
        assert_eq!(
            state.clone().remove_clock(clock_2).unwrap_err(),
            AlgoError::UnknownClock(clock_2)
        );

        // removing the existing clocks should succeed
        state
            .remove_clock(clock_1)
            .unwrap()
            .remove_external_clock(clock_2)
            .unwrap();
    }

    #[test]
    fn test_time_evolve() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();

        let link_1 = LinkId::new(clock_1, clock_2).unwrap();
        let link_2 = LinkId::new(clock_1, clock_2).unwrap();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.0).into(), (1e-6, 0.0).into(), 1e-8)
            .unwrap()
            .add_clock(clock_2, (0.0, 1e-5).into(), (-1e-6, 1e-7).into(), 0.0)
            .unwrap()
            .add_link(link_1, (0.5, 0.2).into(), 0.0)
            .unwrap()
            .add_link(link_2, (2.0, 0.0).into(), 0.1)
            .unwrap()
            .progress_time(Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(100, 0))
            .unwrap();
        assert_eq!(state.clock_frequency(clock_1).unwrap().value, 1e-6);
        // Random walk noise, so frequency deviation is sqrt(time_interval)*wander.
        assert_almost_eq!(state.clock_frequency(clock_1).unwrap().variance, 1e-14);
        // Pre-existing frequency offset should cause phase offset.
        assert_almost_eq!(state.clock_offset(clock_1).unwrap().value, 1e-4);
        // Random walk noise in the derivative, so the integral gives an
        // additional factor of time compared to the frequency deviation.
        // The factor sqrt(3) follows from the structure of how updates work.
        assert_almost_eq!(state.clock_offset(clock_1).unwrap().variance, 1e-10 / 3.0);

        let state = state.remove_clock(clock_1).unwrap();

        assert_eq!(state.link_delay(link_1).unwrap().value, 0.5);
        assert_almost_eq!(state.link_delay(link_1).unwrap().variance, 0.04);

        let state = state.remove_link(link_1).unwrap();

        assert_eq!(state.clock_frequency(clock_2).unwrap().value, -1e-6);
        assert_almost_eq!(state.clock_frequency(clock_2).unwrap().variance, 1e-14);
        assert_almost_eq!(state.clock_offset(clock_2).unwrap().value, -1e-4);
        assert_almost_eq!(state.clock_offset(clock_2).unwrap().variance, 2e-10);

        assert_uv_almost_eq!(
            state.link_delay(link_2).unwrap(),
            UncertainValue::from((2.0, 2.0))
        );
    }

    #[test]
    fn test_frequency_steering() {
        let clock_1 = ClockId::new();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.0).into(), (1e-6, 0.0).into(), 1e-8)
            .unwrap()
            .absorb_frequency_steer(clock_1, -1e-6)
            .unwrap();

        assert_uv_almost_eq!(
            state.clock_frequency(clock_1).unwrap(),
            UncertainValue::from((0.0, 0.0))
        );
    }

    #[test]
    fn test_clock_offset_steering() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(
            Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(10, 0),
        )
        .add_clock(clock_1, (0.0, 0.0).into(), (1e-6, 0.0).into(), 1e-8)
        .unwrap()
        .add_clock(clock_2, (0.0, 0.0).into(), (1e-6, 0.0).into(), 1e-8)
        .unwrap()
        .absorb_offset_change(clock_2, 1.0)
        .unwrap();

        assert_eq!(
            state.time,
            Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(10, 0)
        );
        assert_uv_almost_eq!(
            state.clock_offset(clock_2).unwrap(),
            UncertainValue::from((1.0, 0.0))
        );

        let state = state
            .absorb_system_clock_offset_change(clock_1, Duration::from_seconds_nanos(-1, 0))
            .unwrap();
        assert_eq!(
            state.time,
            Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(9, 0)
        );
        assert_uv_almost_eq!(
            state.clock_offset(clock_1).unwrap(),
            UncertainValue::from((-1.0, 0.0))
        );
    }

    #[test]
    fn test_progress_time_composes_well() {
        let clock_1 = ClockId::new();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.0).into(), (1e-6, 0.0).into(), 1e-8)
            .unwrap();

        let state_via_intermediate = state
            .clone()
            .progress_time(Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(75, 0))
            .unwrap()
            .progress_time(Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(100, 0))
            .unwrap();
        let state_at_once = state
            .progress_time(Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(100, 0))
            .unwrap();

        assert_uv_almost_eq!(
            state_at_once.clock_offset(clock_1).unwrap(),
            state_via_intermediate.clock_offset(clock_1).unwrap()
        );
        assert_uv_almost_eq!(
            state_at_once.clock_frequency(clock_1).unwrap(),
            state_via_intermediate.clock_frequency(clock_1).unwrap()
        );
    }

    #[test]
    fn test_add_link() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();
        let link_1 = LinkId::new(clock_1, clock_2).unwrap();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.0).into(), (1e-6, 0.0).into(), 1e-8)
            .unwrap()
            .add_clock(clock_2, (0.0, 1e-5).into(), (-1e-6, 1e-7).into(), 0.0)
            .unwrap()
            .add_link(link_1, (1.0, 2.0).into(), 0.0)
            .expect("Failed to add link");
        assert_eq!(state.link_delay(link_1).unwrap().value, 1.0);
        assert_eq!(state.link_delay(link_1).unwrap().variance, 4.0);
    }

    #[test]
    fn test_measure_between_clocks_no_link() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_clock(clock_2, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .measurement(
                LinkId::new(clock_1, clock_2).unwrap().forward(),
                (1.0, 2.0f64.sqrt() * 0.1).into(),
                false,
                clock_1,
            )
            .unwrap()
            .0;

        assert_uv_almost_eq!(
            state.clock_offset(clock_1).unwrap(),
            UncertainValue::from((-0.25, 0.05 * (3.0f64.sqrt())))
        );
        assert_uv_almost_eq!(
            state.clock_offset(clock_2).unwrap(),
            UncertainValue::from((0.25, 0.05 * (3.0f64.sqrt())))
        );
        assert_uv_almost_eq!(
            state.clock_frequency(clock_1).unwrap(),
            UncertainValue::from((0.0, 1e-8))
        );
        assert_uv_almost_eq!(
            state.clock_frequency(clock_2).unwrap(),
            UncertainValue::from((0.0, 1e-8))
        );

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.0).into(), (0.0, 1e-3).into(), 0.0)
            .unwrap()
            .add_clock(clock_2, (0.0, 0.0).into(), (0.0, 1e-3).into(), 0.0)
            .unwrap()
            .progress_time(Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(100, 0))
            .unwrap()
            .measurement(
                LinkId::new(clock_1, clock_2).unwrap().forward(),
                (1.0, 2.0f64.sqrt() * 0.1).into(),
                false,
                clock_1,
            )
            .unwrap()
            .0;

        assert_uv_almost_eq!(
            state.clock_offset(clock_1).unwrap(),
            UncertainValue::from((-0.25, 0.05 * (3.0f64.sqrt())))
        );
        assert_uv_almost_eq!(
            state.clock_offset(clock_2).unwrap(),
            UncertainValue::from((0.25, 0.05 * (3.0f64.sqrt())))
        );
        assert_uv_almost_eq!(
            state.clock_frequency(clock_1).unwrap(),
            UncertainValue::from((-0.0025, 0.0005 * (3.0f64.sqrt())))
        );
        assert_uv_almost_eq!(
            state.clock_frequency(clock_2).unwrap(),
            UncertainValue::from((0.0025, 0.0005 * (3.0f64.sqrt())))
        );
    }

    #[test]
    fn test_measure_between_clocks_with_link() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();
        let link_1 = LinkId::new(clock_1, clock_2).unwrap();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_clock(clock_2, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_link(link_1, (1.0, 0.0).into(), 0.0)
            .unwrap()
            .measurement(
                link_1.forward(),
                (2.0, 2.0f64.sqrt() * 0.1).into(),
                true,
                clock_1,
            )
            .unwrap()
            .0;

        assert_uv_almost_eq!(
            state.clock_offset(clock_1).unwrap(),
            UncertainValue::from((-0.25, 0.05 * (3.0f64.sqrt())))
        );
        assert_uv_almost_eq!(
            state.clock_offset(clock_2).unwrap(),
            UncertainValue::from((0.25, 0.05 * (3.0f64.sqrt())))
        );
        assert_uv_almost_eq!(
            state.clock_frequency(clock_1).unwrap(),
            UncertainValue::from((0.0, 1e-8))
        );
        assert_uv_almost_eq!(
            state.clock_frequency(clock_2).unwrap(),
            UncertainValue::from((0.0, 1e-8))
        );
        assert_uv_almost_eq!(
            state.link_delay(link_1).unwrap(),
            UncertainValue::from((1.0, 0.0))
        );

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.0).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_clock(clock_2, (0.0, 0.0).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_link(link_1, (0.0, 0.1).into(), 0.0)
            .unwrap()
            .measurement(link_1.forward(), (1.0, 0.1).into(), true, clock_1)
            .unwrap()
            .0;

        assert_uv_almost_eq!(
            state.clock_offset(clock_1).unwrap(),
            UncertainValue::from((0.0, 0.0))
        );
        assert_uv_almost_eq!(
            state.clock_offset(clock_2).unwrap(),
            UncertainValue::from((0.0, 0.0))
        );
        assert_uv_almost_eq!(
            state.clock_frequency(clock_1).unwrap(),
            UncertainValue::from((0.0, 1e-8))
        );
        assert_uv_almost_eq!(
            state.clock_frequency(clock_2).unwrap(),
            UncertainValue::from((0.0, 1e-8))
        );
        assert_uv_almost_eq!(
            state.link_delay(link_1).unwrap(),
            UncertainValue::from((0.5, 0.1 / (2.0f64.sqrt())))
        );
    }

    #[test]
    fn test_measure_external_clock_no_link() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_external_clock(clock_2)
            .unwrap()
            .measurement(
                LinkId::new(clock_2, clock_1).unwrap().forward(),
                (1.0, 0.1).into(),
                false,
                clock_1,
            )
            .unwrap()
            .0;

        assert_uv_almost_eq!(
            state.clock_offset(clock_1).unwrap(),
            UncertainValue::from((0.5, 0.1 / (2.0f64.sqrt())))
        );

        assert_uv_almost_eq!(
            state.clock_frequency(clock_1).unwrap(),
            UncertainValue::from((0.0, 1e-8))
        );

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_external_clock(clock_2)
            .unwrap()
            .measurement(
                LinkId::new(clock_1, clock_2).unwrap().forward(),
                (1.0, 0.1).into(),
                false,
                clock_1,
            )
            .unwrap()
            .0;

        assert_uv_almost_eq!(
            state.clock_offset(clock_1).unwrap(),
            UncertainValue::from((-0.5, 0.1 / (2.0f64.sqrt())))
        );

        assert_uv_almost_eq!(
            state.clock_frequency(clock_1).unwrap(),
            UncertainValue::from((0.0, 1e-8))
        );

        assert!(state.remove_external_clock(clock_2).is_ok());
    }

    #[test]
    fn test_negative_time_step() {
        let state = EstimatorState::<StdKalmanStorage<()>>::empty(
            Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(1, 0),
        );
        assert_eq!(
            state
                .clone()
                .progress_time(Timestamp::UNIX_EPOCH)
                .unwrap_err(),
            AlgoError::NonMonotonicTimeProgression {
                from: Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(1, 0),
                to: Timestamp::UNIX_EPOCH
            }
        );
    }

    #[test]
    fn test_invalid_measurements() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();
        let clock_3 = ClockId::new();
        let clock_4 = ClockId::new();
        let clock_5 = ClockId::new();
        let link_1 = LinkId::new(clock_3, clock_1).unwrap();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_external_clock(clock_1)
            .unwrap()
            .add_external_clock(clock_2)
            .unwrap()
            .add_clock(clock_3, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_clock(clock_4, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap();

        assert_eq!(
            state
                .clone()
                .measurement(
                    LinkId::new(clock_1, clock_2).unwrap().forward(),
                    (0.0, 0.1).into(),
                    false,
                    clock_1,
                )
                .unwrap_err(),
            AlgoError::BothClocksExternal(clock_1, clock_2)
        );

        assert_eq!(
            state
                .clone()
                .measurement(
                    LinkId::new(clock_3, clock_5).unwrap().forward(),
                    (0.0, 0.1).into(),
                    false,
                    clock_1,
                )
                .unwrap_err(),
            AlgoError::UnknownClock(clock_5)
        );

        assert_eq!(
            state
                .clone()
                .measurement(
                    LinkId::new(clock_5, clock_3).unwrap().forward(),
                    (0.0, 0.1).into(),
                    false,
                    clock_1,
                )
                .unwrap_err(),
            AlgoError::UnknownClock(clock_5)
        );

        assert_eq!(
            state
                .clone()
                .measurement(link_1.forward(), (0.0, 0.1).into(), true, clock_1)
                .unwrap_err(),
            AlgoError::UnknownLink(link_1)
        );
    }

    #[test]
    fn test_exceedance_bound() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();

        let state = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 5e-4).into(), (0.0, 1e-6).into(), 0.0)
            .unwrap()
            .add_clock(clock_2, (0.0, 0.0).into(), (0.0, 0.0).into(), 1e-9)
            .unwrap();
        // Approximately sqrt(750000) to within precision choice
        assert_almost_eq!(
            state
                .time_to_error_bound_exceedance(clock_1, Duration::from_seconds_nanos(0, 1_000_000))
                .unwrap()
                .as_seconds(),
            866.0
        );
        // Approximately 10000 to within precision choice
        assert_almost_eq!(
            state
                .time_to_error_bound_exceedance(clock_2, Duration::from_seconds_nanos(0, 1_000_000))
                .unwrap()
                .as_seconds(),
            9992.0
        );
        let state = state
            .progress_time(Timestamp::UNIX_EPOCH + Duration::from_seconds_nanos(433, 0))
            .unwrap();
        // at least the remainder
        assert!(
            state
                .time_to_error_bound_exceedance(clock_1, Duration::from_seconds_nanos(0, 1_000_000))
                .unwrap()
                .as_seconds()
                > 433.0
        );
        assert!(
            state
                .time_to_error_bound_exceedance(clock_2, Duration::from_seconds_nanos(0, 1_000_000))
                .unwrap()
                .as_seconds()
                > 9992.0 - 433.0
        );
    }

    #[test]
    fn test_importance_direct() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();

        let (_, importance) = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_external_clock(clock_2)
            .unwrap()
            .measurement(
                LinkId::new(clock_1, clock_2).unwrap().forward(),
                (1.0, 0.1).into(),
                false,
                clock_1,
            )
            .unwrap();

        assert_almost_eq!(importance, 0.5);
    }

    #[test]
    fn test_importance_indirect() {
        let clock_1 = ClockId::new();
        let clock_2 = ClockId::new();
        let clock_3 = ClockId::new();

        let (_, importance) = EstimatorState::<StdKalmanStorage<()>>::empty(Timestamp::UNIX_EPOCH)
            .add_clock(clock_1, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_clock(clock_2, (0.0, 0.1).into(), (0.0, 1e-8).into(), 1e-8)
            .unwrap()
            .add_external_clock(clock_3)
            .unwrap()
            .measurement(
                LinkId::new(clock_1, clock_2).unwrap().forward(),
                (0.0, 0.0).into(),
                false,
                clock_1,
            )
            .unwrap()
            .0
            .measurement(
                LinkId::new(clock_2, clock_3).unwrap().forward(),
                (1.0, 0.1).into(),
                false,
                clock_1,
            )
            .unwrap();

        assert_almost_eq!(importance, 1. / 3.);
    }
}