Compare commits

...

2 Commits

Author SHA1 Message Date
Vecna 659b8fa16c Refactor analysis for efficiency 2024-05-22 16:04:52 -04:00
Vecna 6b6836dbae Simulation: Give user chance to use bridges each day 2024-05-21 22:35:42 -04:00
2 changed files with 239 additions and 198 deletions

View File

@ -309,35 +309,44 @@ impl Analyzer for NormalAnalyzer {
let alpha = 1.0 - confidence; let alpha = 1.0 - confidence;
let (bridge_ips_mean, bridge_ips_sd) = Self::mean_and_std_dev(bridge_ips); // Evaluate based on negative reports
let (negative_reports_mean, negative_reports_sd) = Self::mean_and_std_dev(negative_reports); let (negative_reports_mean, negative_reports_sd) = Self::mean_and_std_dev(negative_reports);
// Model negative reports separately
let bip_normal = Normal::new(bridge_ips_mean, bridge_ips_sd);
let nr_normal = Normal::new(negative_reports_mean, negative_reports_sd); let nr_normal = Normal::new(negative_reports_mean, negative_reports_sd);
if negative_reports_sd > 0.0 {
// If we have 0 standard deviation, we need another way to
// evaluate each variable
let bip_test = if bridge_ips_sd > 0.0 {
bip_normal.unwrap().cdf(bridge_ips_today as f64) < alpha
} else {
// Consider the bridge blocked if its usage dropped by more
// than 1 bin. (Note that the mean is the exact value
// because we had no deviation.)
(bridge_ips_today as f64) < bridge_ips_mean - 8.0
};
let nr_test = if negative_reports_sd > 0.0 {
// We use CCDF because more negative reports is worse. // We use CCDF because more negative reports is worse.
(1.0 - nr_normal.unwrap().cdf(negative_reports_today as f64)) < alpha if (1.0 - nr_normal.unwrap().cdf(negative_reports_today as f64)) < alpha {
return true;
}
} else { } else {
// If the standard deviation is 0, we need another option.
// Consider the bridge blocked negative reports increase by // Consider the bridge blocked negative reports increase by
// more than 1 after a long static period. (Note that the // more than 1 after a long static period. (Note that the
// mean is the exact value because we had no deviation.) // mean is the exact value because we had no deviation.)
(negative_reports_today as f64) > negative_reports_mean + 1.0 if (negative_reports_today as f64) > negative_reports_mean + 1.0 {
}; return true;
}
}
// Return true if any test concluded the bridge is blocked // Evaluate based on bridge stats
bip_test || nr_test let (bridge_ips_mean, bridge_ips_sd) = Self::mean_and_std_dev(bridge_ips);
let bip_normal = Normal::new(bridge_ips_mean, bridge_ips_sd);
if bridge_ips_sd > 0.0 {
if bip_normal.unwrap().cdf(bridge_ips_today as f64) < alpha {
return true;
}
} else {
// If the standard deviation is 0, we need another option.
// Consider the bridge blocked if its usage dropped by more
// than 1 bin. (Note that the mean is the exact value
// because we had no deviation.)
if (bridge_ips_today as f64) < bridge_ips_mean - 8.0 {
return true;
}
}
// If none of the tests concluded that the bridge is blocked,
// return false
false
} }
/// Evaluate invite-only bridge with lv3+ users submitting positive reports /// Evaluate invite-only bridge with lv3+ users submitting positive reports
@ -357,19 +366,29 @@ impl Analyzer for NormalAnalyzer {
let alpha = 1.0 - confidence; let alpha = 1.0 - confidence;
// Model bridge IPs and positive reports with multivariate // Evaluate based on negative reports. It is better to compute
// normal distribution // negative reports test first because the positive test may be
let (mean_vec, cov_mat) = Self::stats(&[bridge_ips, positive_reports]); // expensive.
let mvn = MultivariateNormal::new(mean_vec, cov_mat);
// Model negative reports separately
let (negative_reports_mean, negative_reports_sd) = Self::mean_and_std_dev(negative_reports); let (negative_reports_mean, negative_reports_sd) = Self::mean_and_std_dev(negative_reports);
let nr_normal = Normal::new(negative_reports_mean, negative_reports_sd); let nr_normal = Normal::new(negative_reports_mean, negative_reports_sd);
if negative_reports_sd > 0.0 {
// We use CCDF because more negative reports is worse.
if (1.0 - nr_normal.unwrap().cdf(negative_reports_today as f64)) < alpha {
return true;
}
} else {
// Consider the bridge blocked negative reports increase by
// more than 1 after a long static period. (Note that the
// mean is the exact value because we had no deviation.)
if (negative_reports_today as f64) > negative_reports_mean + 1.0 {
return true;
}
}
// If we have 0 standard deviation or a covariance matrix that // Evaluate based on bridge stats and positive reports.
// is not positive definite, we need another way to evaluate let (mean_vec, cov_mat) = Self::stats(&[bridge_ips, positive_reports]);
// each variable let mvn = MultivariateNormal::new(mean_vec, cov_mat);
let positive_test = if mvn.is_ok() { if mvn.is_ok() {
let mvn = mvn.unwrap(); let mvn = mvn.unwrap();
// Estimate the CDF by integrating the PDF by hand with step // Estimate the CDF by integrating the PDF by hand with step
@ -380,27 +399,27 @@ impl Analyzer for NormalAnalyzer {
cdf += mvn.pdf(&DVector::from_vec(vec![bip as f64, pr as f64])); cdf += mvn.pdf(&DVector::from_vec(vec![bip as f64, pr as f64]));
} }
} }
cdf < alpha if cdf < alpha {
return true;
}
} else { } else {
// Ignore positive reports and compute as in stage 2 // If we have 0 standard deviation or a covariance matrix
self.stage_two( // that is not positive definite, we need another way to
// evaluate each variable. Ignore positive reports and
// compute as in stage 2
if self.stage_two(
confidence, confidence,
bridge_ips, bridge_ips,
bridge_ips_today, bridge_ips_today,
negative_reports, negative_reports,
negative_reports_today, negative_reports_today,
) ) {
}; return true;
let nr_test = if negative_reports_sd > 0.0 { }
// We use CCDF because more negative reports is worse.
(1.0 - nr_normal.unwrap().cdf(negative_reports_today as f64)) < alpha
} else {
// Consider the bridge blocked negative reports increase by
// more than 1 after a long static period. (Note that the
// mean is the exact value because we had no deviation.)
(negative_reports_today as f64) > negative_reports_mean + 1.0
}; };
positive_test || nr_test // If none of the tests concluded that the bridge is blocked,
// return false
false
} }
} }

View File

@ -26,6 +26,9 @@ pub struct User {
// Does the user submit reports to Troll Patrol? // Does the user submit reports to Troll Patrol?
submits_reports: bool, submits_reports: bool,
// How likely is this user to use bridges on a given day?
prob_use_bridges: f64,
} }
impl User { impl User {
@ -63,12 +66,17 @@ impl User {
cc cc
}; };
// Randomly determine how likely this user is to use bridges on
// a given day
let prob_use_bridges = rng.gen_range(0.0..=1.0);
Self { Self {
censor: censor, censor: censor,
country: cc, country: cc,
primary_cred: cred, primary_cred: cred,
secondary_cred: None, secondary_cred: None,
submits_reports: submits_reports, submits_reports: submits_reports,
prob_use_bridges: prob_use_bridges,
} }
} }
@ -127,12 +135,17 @@ impl User {
cc cc
}; };
// Randomly determine how likely this user is to use bridges on
// a given day
let prob_use_bridges = rng.gen_range(0.0..=1.0);
Ok(Self { Ok(Self {
censor: censor, censor: censor,
country: cc, country: cc,
primary_cred: friend_cred, primary_cred: friend_cred,
secondary_cred: None, secondary_cred: None,
submits_reports: submits_reports, submits_reports: submits_reports,
prob_use_bridges: prob_use_bridges,
}) })
} }
@ -179,6 +192,10 @@ impl User {
// newly invited friends and a vector of fingerprints of successfully // newly invited friends and a vector of fingerprints of successfully
// contacted bridges. // contacted bridges.
pub async fn daily_tasks(&mut self, state: &State) -> (Vec<User>, Vec<[u8; 20]>) { pub async fn daily_tasks(&mut self, state: &State) -> (Vec<User>, Vec<[u8; 20]>) {
// Probabilistically decide if the user should use bridges today
let mut rng = rand::thread_rng();
let num: f64 = rng.gen_range(0.0..1.0);
if num < self.prob_use_bridges {
// Download bucket to see if bridge is still reachable // Download bucket to see if bridge is still reachable
// (We assume that this step can be done even if the user can't actually // (We assume that this step can be done even if the user can't actually
// talk to the LA.) // talk to the LA.)
@ -187,7 +204,8 @@ impl User {
// Can we level up the main credential? // Can we level up the main credential?
let can_level_up = reachcred.is_some() let can_level_up = reachcred.is_some()
&& (level == 0 && eligible_for_trust_promotion(&state.net, &self.primary_cred).await && (level == 0
&& eligible_for_trust_promotion(&state.net, &self.primary_cred).await
|| level > 0 && eligible_for_level_up(&state.net, &self.primary_cred).await); || level > 0 && eligible_for_level_up(&state.net, &self.primary_cred).await);
// Can we migrate the main credential? // Can we migrate the main credential?
@ -288,7 +306,8 @@ impl User {
let cred = trust_migration( let cred = trust_migration(
&state.net, &state.net,
&second_cred, &second_cred,
&trust_promotion(&state.net, &second_cred, get_lox_pub(&state.la_pubkeys)).await, &trust_promotion(&state.net, &second_cred, get_lox_pub(&state.la_pubkeys))
.await,
get_lox_pub(&state.la_pubkeys), get_lox_pub(&state.la_pubkeys),
get_migration_pub(&state.la_pubkeys), get_migration_pub(&state.la_pubkeys),
) )
@ -354,5 +373,8 @@ impl User {
} }
(new_friends, connections) (new_friends, connections)
} else {
(Vec::<User>::new(), Vec::<[u8; 20]>::new())
}
} }
} }