From 0d31a4f1ab78786b227ba75065ed61184828d5ff Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 23 Sep 2023 22:33:25 +0100 Subject: [PATCH] add converstion between lognormal form and 90% c.i. --- README.md | 5 +++-- squiggle.c | 25 +++++++++++++++++++++++-- squiggle.h | 6 ++++++ test/test | Bin 26768 -> 26992 bytes 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cda8453..607f1f1 100644 --- a/README.md +++ b/README.md @@ -345,9 +345,10 @@ It emits one warning about something I already took care of, so by default I've - [x] Add prototypes - [x] Use named structs - [x] Add to header file - - [x] Provide example + - [x] Provide example algebra + - [x] Add conversion between 90% ci and parameters. + - [ ] Use that conversion in conjuction with small algebra. - [ ] Test results - - [ ] Add conversion between 90% ci and parameters. - [x] Move to own file? Or signpost in file? => signposted in file. - [ ] Disambiguate sample_laplace--successes vs failures || successes vs total trials as two distinct and differently named functions - [ ] Write twitter thread. diff --git a/squiggle.c b/squiggle.c index 3bde4da..9151ccd 100644 --- a/squiggle.c +++ b/squiggle.c @@ -12,7 +12,8 @@ #define EXIT_ON_ERROR 0 #define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__) -const double PI = 3.14159265358979323846; // M_PI in gcc gnu99 +#define PI 3.14159265358979323846 // M_PI in gcc gnu99 +#define NORMAL95CONFIDENCE 1.6448536269514722 // # Key functionality // Define the minimum number of functions needed to do simple estimation @@ -97,7 +98,6 @@ inline double sample_normal_from_95_confidence_interval(double low, double high, // 5. If we want a 90% confidence interval from high to low, // we can set mean = (high + low)/2; the midpoint, and L = high-low, // Normal([high + low]/2, [high - low]/(2 * 1.6448536269514722)) - const double NORMAL95CONFIDENCE = 1.6448536269514722; double mean = (high + low) / 2.0; double std = (high - low) / (2.0 * NORMAL95CONFIDENCE); return sample_normal(mean, std, seed); @@ -504,3 +504,24 @@ lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params }; return result; } + +lognormal_params convert_ci_to_lognormal_params(struct c_i x) +{ + double loghigh = logf(x.high); + double loglow = logf(x.low); + double logmean = (loghigh + loglow) / 2.0; + double logstd = (loghigh - loglow) / (2.0 * NORMAL95CONFIDENCE); + lognormal_params result = { .logmean = logmean, .logstd = logstd}; + return result; +} + +struct c_i convert_lognormal_params_to_ci(lognormal_params y) +{ + double h = y.logstd * NORMAL95CONFIDENCE; + double loghigh = y.logmean + h; + double loglow = y.logmean - h; + struct c_i result = { .low=exp(loglow), .high=exp(loghigh)}; + return result; + +} + diff --git a/squiggle.h b/squiggle.h index 50ba120..8a93325 100644 --- a/squiggle.h +++ b/squiggle.h @@ -72,4 +72,10 @@ typedef struct lognormal_params_t { } lognormal_params; lognormal_params algebra_product_lognormals(lognormal_params a, lognormal_params b); + +lognormal_params convert_ci_to_lognormal_params(struct c_i x); + + +struct c_i convert_lognormal_params_to_ci(lognormal_params y); + #endif diff --git a/test/test b/test/test index 997dac559550c351a828b6b62a4b9fab32828658..38c233622051b0b6240eba387cfbdae11e40ea05 100755 GIT binary patch delta 3410 zcma)83s6+o8NTPj>VmSn^2qWKcOwsZ$y4N^rqJ+M5H$$OL*yY3p`s8lifIyRbrQ)) z#QtX5WNb#QHMGsN$ZCs8+u)-PR@>sU8jaGZb+xntY9x;F^gH+7%{X?Z?VaI%|M|ZE z{OA1V|Ib~zPP36StSe0JsoEZK)&1QlNqOFlB2O0W-ZohswoSI@vMlCS#?)~cy(j<0 z`^)hUN39=U+1haL-iIp&9fA6QOEc}SxxDY?Ru>{kwoZ?`!FG!|kjb&n(F?v7w#{Dd zTQZ3qq`sgKwvDa^H8Be<433f?O(P!WXK-D=Zp>)P2-c4oOLU*nPlEf{6e9qq zA(8T;K;cDcp_5rGEeIWE36we`QZ`Qgh90qZ&Ulfropd@Zimj(7VUetWBEq}fdi^8` zp>@4bIkreV{Vu~=8=BZ5tt9PEgH%(<=OE(Xe`O}Z3CDs~Nam?2@sykNa;(=pR>%sV`0 zEFY*dh8-qTkhi^NW}(b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