Skip to content

Commit

Permalink
fix confidence if combined variance
Browse files Browse the repository at this point in the history
  • Loading branch information
skrrb committed Sep 14, 2023
1 parent dad37aa commit 44d7608
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
15 changes: 2 additions & 13 deletions programs/openbook-v2/src/state/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,13 @@ impl Market {

if oracle_a.is_stale(oracle_a_acc.key(), &self.oracle_config, now_slot)
|| oracle_b.is_stale(oracle_b_acc.key(), &self.oracle_config, now_slot)
|| !oracle_a.has_valid_combined_confidence(&oracle_b, &self.oracle_config)
{
return Ok(None);
}

let (price, var) = oracle_a.combine_div_with_var(&oracle_b);

let target_var = self.oracle_config.conf_filter.powi(2);
if var > target_var {
msg!(
"Combined variance too high; value {}, target {}",
var,
target_var
);
Ok(None)
} else {
let price = oracle_a.price / oracle_b.price;
let decimals = (self.quote_decimals as i8) - (self.base_decimals as i8);
let decimal_adj = oracle::power_of_ten_float(decimals);

Ok(I80F48::checked_from_num(price * decimal_adj))
}
}
Expand Down
22 changes: 16 additions & 6 deletions programs/openbook-v2/src/state/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,26 @@ impl OracleState {
}
}

pub fn combine_div_with_var(&self, other: &Self) -> (f64, f64) {
let price = self.price / other.price;

pub fn has_valid_combined_confidence(&self, other: &Self, config: &OracleConfig) -> bool {
// target uncertainty reads
// $ \sigma \approx \frac{A}{B} * \sqrt{(\sigma_A/A)^2 + (\sigma_B/B)^2} $
// but alternatively, to avoid costly operations, we compute the square
let var = ((self.deviation / self.price).powi(2) + (other.deviation / other.price).powi(2))
* price.powi(2);
// Also note that the relative scaled var, i.e. without the \frac{A}{B} factor, is computed
let relative_var =
(self.deviation / self.price).powi(2) + (other.deviation / other.price).powi(2);

let relative_target_var = config.conf_filter.powi(2);

(price, var)
if relative_var > relative_target_var {
msg!(
"Combined confidence too high: computed^2: {}, conf_filter^2: {}",
relative_var,
relative_target_var
);
false
} else {
true
}
}
}

Expand Down

0 comments on commit 44d7608

Please sign in to comment.