Skip to content

Commit

Permalink
improve erf precision
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelDkhn committed Aug 9, 2024
1 parent c5945fd commit 8329774
Show file tree
Hide file tree
Showing 3 changed files with 40,023 additions and 717 deletions.
43 changes: 20 additions & 23 deletions packages/orion-algo/src/algo/cdf.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,9 @@ pub fn cdf<
};

// Calculate: 0.5 * (1.0 + erf((x_val - loc_val) / (scale_val * sqrt(2.0))))
let sqrt_2 = FixedTrait::sqrt(FixedTrait::TWO());
let x_minus_loc = *x_val - loc_val;
let scale_times_sqrt_2 = scale_val * sqrt_2;
let division_result = x_minus_loc / scale_times_sqrt_2;
let erf_result = FixedTrait::erf(division_result);
let one_plus_erf = FixedTrait::ONE() + erf_result;
let calc = FixedTrait::HALF() * one_plus_erf;
let calc = FixedTrait::HALF()
* (FixedTrait::ONE()
+ ((*x_val - loc_val) / (scale_val * FixedTrait::TWO().sqrt())).erf());

res_data.append(calc);
},
Expand All @@ -123,26 +119,27 @@ pub fn cdf<
mod tests {
use super::cdf;
use orion_numbers::{F64, F64Impl, f64::helpers::{assert_relative_span, assert_relative}};
// #[test]
// fn test_cdf_loc_scale_are_none() {
// let x: Span<F64> = array![F64Impl::ONE(), F64Impl::HALF(), F64Impl::ZERO()].span();
#[test]
fn test_cdf_loc_scale_are_none() {
let x: Span<F64> = array![F64Impl::ONE(), F64Impl::HALF(), F64Impl::ZERO()].span();

// let res = cdf(x, Option::None, Option::None);
// let expected: Span<felt252> = array![3613548169, 2969808657, 2147483648].span();
let res = cdf(x, Option::None, Option::None);
let expected: Span<felt252> = array![3613548169, 2969808657, 2147483648].span();

// assert_relative_span(res, expected, 'res != expected', Option::None);
// }
// #[test]
// fn test_cdf_loc_scale_are_some() {
// let x: Span<F64> = array![F64Impl::ONE(), F64Impl::HALF(), F64Impl::ZERO()].span();
assert_relative_span(res, expected, 'res != expected', Option::Some(4294967));
}

#[test]
fn test_cdf_loc_scale_are_some() {
let x: Span<F64> = array![F64Impl::ONE(), F64Impl::HALF(), F64Impl::ZERO()].span();

// let loc: Span<F64> = array![F64Impl::HALF(), F64Impl::HALF(), F64Impl::HALF()].span();
let loc: Span<F64> = array![F64Impl::HALF(), F64Impl::HALF(), F64Impl::HALF()].span();

// let scale: Span<F64> = array![F64Impl::HALF(), F64Impl::HALF(), F64Impl::HALF()].span();
let scale: Span<F64> = array![F64Impl::HALF(), F64Impl::HALF(), F64Impl::HALF()].span();

// let res = cdf(x, Option::Some(loc), Option::Some(scale));
// let expected = array![3613548169, 2147483648, 681419126].span();
let res = cdf(x, Option::Some(loc), Option::Some(scale));
let expected = array![3613548169, 2147483648, 681419126].span();

// assert_relative_span(res, expected, 'res != expected', Option::None);
// }
assert_relative_span(res, expected, 'res != expected', Option::Some(4294967));
}
}
13 changes: 7 additions & 6 deletions packages/orion-numbers/src/f64/erf.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(crate) fn erf(x: F64) -> F64 {
#[cfg(test)]
mod tests {
use super::{F64, F64Impl, erf};
use orion_numbers::f64::helpers::assert_precise;

#[test]
#[available_gas(1000000000)]
Expand All @@ -50,11 +51,11 @@ mod tests {
let f4_erf: F64 = erf(f4);
let f5_erf: F64 = erf(f5);
let f6_erf: F64 = erf(f6);
assert(f1_erf.d == 3619372346, 'f1_erf does no work!');
assert(f2_erf.d == 674082374, 'f2_erf does no work!');
assert(f3_erf.d == 2310257026, 'f3_erf does no work!');
assert(f4_erf.d == 4274876577, 'f4_erf does no work!');
assert(f5_erf.d == 4294967296, 'f5_erf does no work!');
assert(f6_erf.d == 4294967296, 'f6_erf does no work!');
assert_precise(f1_erf, 3619372346, 'f1_erf does no work!', Option::Some(4294967));
assert_precise(f2_erf, 645545024, 'f2_erf does no work!', Option::Some(4294967));
assert_precise(f3_erf, 2310257026, 'f3_erf does no work!', Option::Some(4294967));
assert_precise(f4_erf, 4274876577, 'f4_erf does no work!', Option::Some(4294967));
assert_precise(f5_erf, 4294967296, 'f5_erf does no work!', Option::Some(4294967));
assert_precise(f6_erf, 4294967296, 'f6_erf does no work!', Option::Some(4294967));
}
}
Loading

0 comments on commit 8329774

Please sign in to comment.