Repository navigation
feat: fit independent lineage criterion posterior in Rust #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
seonghobae
merged 1 commit into
feat/lineage-pair-criterion-producer
from
feat/lineage-pair-criterion-cpu
Aug 26, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| //! Rust-owned posterior fitting for independently observed TDT link criteria. | ||
| //! | ||
| //! This slice fits the identified independent criterion probability. Event-time | ||
| //! draws remain producer evidence from the temporal model; it does not infer a | ||
| //! date from record order or promote CHRONOS predictions to observed facts. | ||
|
|
||
| use std::collections::BTreeSet; | ||
|
|
||
| use event_core::{ | ||
| CriterionPosteriorError, IndependentCriterionCounts, fit_independent_criterion_posterior, | ||
| }; | ||
|
|
||
| /// Independent criterion counts and temporal draws for one exact pair. | ||
| #[derive(Clone, Debug, Eq, PartialEq)] | ||
| pub struct LineageCriterionObservation { | ||
| /// Opaque exact pair identity. | ||
| pub pair_id: String, | ||
| /// Independently observed positive TDT link outcomes. | ||
| pub successes: u32, | ||
| /// Total independently observed TDT link outcomes. | ||
| pub trials: u32, | ||
| /// Predecessor event-time draws supplied by the temporal model. | ||
| pub predecessor_event_time_draws: Vec<String>, | ||
| /// Successor event-time draws supplied by the temporal model. | ||
| pub successor_event_time_draws: Vec<String>, | ||
| } | ||
|
|
||
| /// Identified posterior output for one exact pair. | ||
| #[derive(Clone, Debug, PartialEq)] | ||
| pub struct LineageCriterionFit { | ||
| /// Opaque exact pair identity. | ||
| pub pair_id: String, | ||
| /// Jeffreys posterior mean. | ||
| pub posterior_mean: f64, | ||
| /// Jeffreys posterior variance. | ||
| pub posterior_variance: f64, | ||
| /// Deterministic posterior quadrature draws. | ||
| pub criterion_draws: Vec<f64>, | ||
| /// Unmodified predecessor event-time draws. | ||
| pub predecessor_event_time_draws: Vec<String>, | ||
| /// Unmodified successor event-time draws. | ||
| pub successor_event_time_draws: Vec<String>, | ||
| } | ||
|
|
||
| /// Fail-closed independent criterion fitting errors. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub enum LineageCriterionFitError { | ||
| /// No exact pair was supplied. | ||
| EmptyInput, | ||
| /// Pair identity was empty, padded, too long, or duplicated. | ||
| InvalidPairIdentity, | ||
| /// Event-time draw counts differ from the requested common draw count. | ||
| TemporalDrawMismatch, | ||
| /// The independently identified scientific estimator rejected the input. | ||
| Criterion(CriterionPosteriorError), | ||
| } | ||
|
|
||
| /// Fit independent TDT link-criterion posteriors without local weights. | ||
| /// | ||
| /// Input order is preserved. Temporal draws are carried without alteration; | ||
| /// record creation timestamps and nearest dates are never substituted. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Fails closed for empty or duplicate pair identities, mixed draw counts, or | ||
| /// invalid independent criterion observations. | ||
| pub fn fit_lineage_criterion_posteriors( | ||
| observations: &[LineageCriterionObservation], | ||
| draw_count: usize, | ||
| ) -> Result<Vec<LineageCriterionFit>, LineageCriterionFitError> { | ||
| if observations.is_empty() { | ||
| return Err(LineageCriterionFitError::EmptyInput); | ||
| } | ||
| let identities = observations | ||
| .iter() | ||
| .map(|observation| observation.pair_id.as_str()) | ||
| .collect::<BTreeSet<_>>(); | ||
| if identities.len() != observations.len() | ||
| || observations.iter().any(|observation| { | ||
| observation.pair_id.is_empty() | ||
| || observation.pair_id.len() > 256 | ||
| || observation.pair_id.trim() != observation.pair_id | ||
| }) | ||
| { | ||
| return Err(LineageCriterionFitError::InvalidPairIdentity); | ||
| } | ||
| observations | ||
| .iter() | ||
| .map(|observation| { | ||
| if observation.predecessor_event_time_draws.len() != draw_count | ||
| || observation.successor_event_time_draws.len() != draw_count | ||
| { | ||
| return Err(LineageCriterionFitError::TemporalDrawMismatch); | ||
| } | ||
| let posterior = fit_independent_criterion_posterior( | ||
| IndependentCriterionCounts { | ||
| successes: observation.successes, | ||
| trials: observation.trials, | ||
| }, | ||
| draw_count, | ||
| ) | ||
| .map_err(LineageCriterionFitError::Criterion)?; | ||
| Ok(LineageCriterionFit { | ||
| pair_id: observation.pair_id.clone(), | ||
| posterior_mean: posterior.mean, | ||
| posterior_variance: posterior.variance, | ||
| criterion_draws: posterior.plausible_values, | ||
| predecessor_event_time_draws: observation.predecessor_event_time_draws.clone(), | ||
| successor_event_time_draws: observation.successor_event_time_draws.clone(), | ||
| }) | ||
| }) | ||
| .collect() | ||
| } | ||
72 changes: 72 additions & 0 deletions
72
crates/analysis_engine/tests/lineage_criterion_estimator.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| //! Deterministic recovery and fail-closed tests for the Rust criterion path. | ||
|
|
||
| use analysis_engine::{ | ||
| LineageCriterionFitError, LineageCriterionObservation, fit_lineage_criterion_posteriors, | ||
| }; | ||
| use event_core::CriterionPosteriorError; | ||
|
|
||
| fn observation(pair_id: &str, successes: u32, trials: u32) -> LineageCriterionObservation { | ||
| LineageCriterionObservation { | ||
| pair_id: pair_id.into(), | ||
| successes, | ||
| trials, | ||
| predecessor_event_time_draws: vec!["2026-01-01T00:00:00Z".into(); 32], | ||
| successor_event_time_draws: vec!["2026-01-02T00:00:00Z".into(); 32], | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn rust_path_recovers_known_independent_pair_probabilities() { | ||
| let truth_counts = [ | ||
| (0.1_f64, 1_000_u32), | ||
| (0.25, 2_500), | ||
| (0.5, 5_000), | ||
| (0.75, 7_500), | ||
| (0.9, 9_000), | ||
| ]; | ||
| let observations = truth_counts | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(index, (_, successes))| observation(&format!("pair-{index}"), *successes, 10_000)) | ||
| .collect::<Vec<_>>(); | ||
| let fits = fit_lineage_criterion_posteriors(&observations, 32).expect("identified fits"); | ||
| let rmse = fits | ||
| .iter() | ||
| .zip(truth_counts) | ||
| .map(|(fit, (truth, _))| (fit.posterior_mean - truth).powi(2)) | ||
| .sum::<f64>() | ||
| / 5.0; | ||
| assert!(rmse.sqrt() < 0.000_05); | ||
| assert_eq!( | ||
| fits[0].predecessor_event_time_draws, | ||
| observations[0].predecessor_event_time_draws | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn rust_path_rejects_identity_draw_and_criterion_failures() { | ||
| assert_eq!( | ||
| fit_lineage_criterion_posteriors(&[], 32), | ||
| Err(LineageCriterionFitError::EmptyInput) | ||
| ); | ||
|
|
||
| let duplicate = vec![observation("pair", 1, 2), observation("pair", 1, 2)]; | ||
| assert_eq!( | ||
| fit_lineage_criterion_posteriors(&duplicate, 32), | ||
| Err(LineageCriterionFitError::InvalidPairIdentity) | ||
| ); | ||
|
|
||
| let mut mixed = observation("pair", 1, 2); | ||
| mixed.successor_event_time_draws.pop(); | ||
| assert_eq!( | ||
| fit_lineage_criterion_posteriors(&[mixed], 32), | ||
| Err(LineageCriterionFitError::TemporalDrawMismatch) | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| fit_lineage_criterion_posteriors(&[observation("pair", 3, 2)], 32), | ||
| Err(LineageCriterionFitError::Criterion( | ||
| CriterionPosteriorError::SuccessesExceedTrials | ||
| )) | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: Event-time and criterion draw counts share one parameter
fit_lineage_criterion_posteriorsuses a singledraw_countboth to require that the event-time draw arrays have that length and as the criterion quadrature point count. The doc calls it a 'common draw count', so callers cannot request different sizes for the two.Was this helpful? React with 👍 or 👎 to provide feedback.