From 8ae4020a88a0f2192274e6d6543b692220e65e52 Mon Sep 17 00:00:00 2001 From: KunoiSayami Date: Fri, 1 Jul 2022 02:40:08 +0800 Subject: feat(script): Swift normal distribution lower to zero Signed-off-by: KunoiSayami --- exp_3.cpp | 62 ++++++++++++++++++------------------------------- normal_distribution.cpp | 8 ++++++- 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/exp_3.cpp b/exp_3.cpp index bda5681..26e14e5 100644 --- a/exp_3.cpp +++ b/exp_3.cpp @@ -10,6 +10,7 @@ constexpr size_t length = 1048576; constexpr size_t sample_length = 1000; #define CALC_HEIGHT +#define CALC_CDF_DIFF #ifdef CALC_HEIGHT constexpr int block_size = 2; @@ -62,16 +63,27 @@ long double sample_cdf(long double x) { (long double)(sample_vector.size() - 1); } -int infer_offset(double x) { +int infer_offset(long double x) { return int(sample_cdf(x) * double(population_vector.size())); } +#ifdef CALC_CDF_DIFF +int infer_offset_from_cdf(long double cdf) { + return int(cdf * double(population_vector.size())); +} // const double M_SQRT1_2 = sqrt(0.5); constexpr long long s_avg = 2147483648; constexpr long long s_stddev = 2147483648; -double goal_cdf(double x) { return erfc(-x * M_SQRT1_2) * 0.5; } -double stat_cdf(double x) { return goal_cdf((x - s_avg) / s_stddev); } +long double goal_cdf(long double x) { return std::erfc(-x * M_SQRT1_2) * 0.5; } +long double stat_cdf(long double x) { return goal_cdf((x - s_avg) / s_stddev); } + +long double finite_population_cdf(long double x) { + auto it = upper_bound(population_vector.begin(), population_vector.end(), x); + return (long double)(it - population_vector.begin()) / + population_vector.size(); +} +#endif int main() { @@ -109,45 +121,17 @@ int main() { for (auto iter = population_vector.begin(); iter != population_vector.end(); iter++) { +#ifdef CALC_CDF_DIFF + auto cdf = sample_cdf(*iter); + printf("%d,%d,%.6Lf ", getHeight(infer_offset_from_cdf(cdf)), + getHeight(iter - population_vector.begin()), + std::abs(cdf - stat_cdf(*iter))); +#else printf("%d,%d ", getHeight(infer_offset(*iter)), getHeight(iter - population_vector.begin())); +#endif + fflush(stdout); } puts(""); return 0; - - /*auto ita = set2.begin(), itb = set2.begin(); - itb++; - long double dx = (*itb - *ita); - int cnt1 = 0, cnt2 = 0; - // for (auto it = set2.begin(); it != set2.end(); it++) printf("%lld ", *it); - // printf("\n"); - long double e = 0; - for (auto iter = set1.begin(); iter != set1.end(); iter++, cnt1++) { - if (*iter <= *set2.begin()) { - e += fabs(0.0 - double(cnt1) / (length - 1)); - printf("%.4lf ", 0.0); - continue; - } - while (itb != set2.end() && *iter > *itb) - ita++, itb++, dx = (*itb - *ita), cnt2++; - if (*itb == *set2.end()) { - e += fabs(1.0 - double(cnt1) / (length - 1)); - printf("%.4lf ", 100.0); - continue; - } - // printf("%lld %lld %lld ", *ita, *it, *itb); - long double res = (cnt2 + (*iter - *ita) / dx) / (sample_length - 1); - auto single_e = std::abs(res - (long double)cnt1 / (length - 1)); - auto real_cnt2 = res * (length - 1) + 1; - location.emplace_back(getHeight(cnt1), - getHeight((int)std::round(real_cnt2)), single_e); - e += single_e; - printf("%.4Lf ", res * 100); - } - printf("\n%.4Lf\n", e); - for (auto element : location) { - printf("%d,%d ", std::get<0>(element), std::get<1>(element)); - } - puts(""); - return 0;*/ } \ No newline at end of file diff --git a/normal_distribution.cpp b/normal_distribution.cpp index bc56e2a..002beba 100644 --- a/normal_distribution.cpp +++ b/normal_distribution.cpp @@ -26,11 +26,17 @@ int main() { if (ret.second) { vector.push_back(element); // TODO: add offset - printf("%lld\n", element); + // printf("%lld\n", element); } } assert(vector.size() == length); + auto offset = *set.begin() < 0 ? 0 - *set.begin() : 0; + + for (auto element : vector) { + printf("%lld\n", element + offset); + } + return 0; } \ No newline at end of file -- cgit v1.3.1