finish inverse!

This commit is contained in:
NunoSempere 2023-07-16 00:59:27 +02:00
parent d4f6142734
commit 88b51331ea
2 changed files with 81 additions and 26 deletions

Binary file not shown.

View File

@ -2,6 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <float.h> // FLT_MAX, FLT_MIN #include <float.h> // FLT_MAX, FLT_MIN
#include <limits.h> // INT_MAX
#define VERBOSE 1
// to do: reuse more informative printing from build-your-own-lisp
// Errors // Errors
// https://mccue.dev/pages/7-27-22-c-errors // https://mccue.dev/pages/7-27-22-c-errors
@ -28,70 +32,121 @@ float cdf_uniform_0_1(float x){
} }
} }
float cdf_squared_0_1(float x){
if(x < 0){
return 0;
} else if (x > 1){
return 1;
} else {
return x*x;
}
}
// Inverse cdf // Inverse cdf
struct box inverse_cdf(float (*cdf(float)), float p){ struct box inverse_cdf(float cdf(float), float p){
// given a cdf: [-Inf, Inf] => [0,1] // given a cdf: [-Inf, Inf] => [0,1]
// returns x such that cdf(x) = p // returns x such that cdf(x) = p
// to do: add bounds, add error checking // to do: add bounds, add error checking
// [x] maybe return a struct or smth. // [x] maybe return a struct or smth.
struct box result; struct box result;
float start = -1.0; float low = -1.0;
float end = 1.0; float high = 1.0;
// 1. Make sure that cdf(start) < p < cdf(end) // 1. Make sure that cdf(low) < p < cdf(high)
// [x] to do: do smth with float min and float max? // [x] to do: do smth with float min and float max?
int interval_found = 0; int interval_found = 0;
while((!interval_found) && (start > FLT_MIN/4) && (end < FLT_MAX/4)){ while((!interval_found) && (low > -FLT_MAX/4) && (high < FLT_MAX/4)){
// ^ Using FLT_MIN and FLT_MAX is overkill // ^ Using FLT_MIN and FLT_MAX is overkill
// but it's also the *correct* thing to do. // but it's also the *correct* thing to do.
int start_condition = (*cdf(start) < p); int low_condition = (cdf(low) < p);
int end_condition = (p < *cdf(end)); int high_condition = (p < cdf(high));
if( start_condition && end_condition ){ if( low_condition && high_condition ){
interval_found = 1; interval_found = 1;
}else if(!start_condition){ }else if(!low_condition){
start = start * 2; low = low * 2;
}else if (!end_condition){ }else if (!high_condition){
end = end * 2 ; high = high * 2 ;
} }
} }
if(0){
printf("FLT_MIN = %f, FLT_MAX = %f, INT_MAX = %d\n", -FLT_MAX, FLT_MAX, INT_MAX);
printf("low: %f, high: %f\n", low, high);
printf("interval_found? %d\n", interval_found);
int while_condition = (!interval_found) && (low > FLT_MIN/4) && (high < FLT_MAX/4);
printf("while condition: %i\n", while_condition);
}
if(!interval_found){ if(!interval_found){
result.empty = 1; result.empty = 1;
return result; return result;
} else{ } else{
int convergence_condition = 0; int convergence_condition = 0;
while(!convergence_condition){ int count = 0;
float mid = (end - start)/2; while(!convergence_condition && (count < (INT_MAX/2) )){
int mid_not_new = (mid == start) || (mid == end); if(VERBOSE){
printf("while loop\n");
}
float mid = (high + low)/2;
int mid_not_new = (mid == low) || (mid == high);
if(VERBOSE){
printf("low: %f, high: %f\n", low, high);
printf("mid: %f\n", mid);
}
if(mid_not_new){ if(mid_not_new){
convergence_condition = 1; convergence_condition = 1;
}else{ } else{
float mid_sign = *cdf(mid) - p; float mid_sign = cdf(mid) - p;
if(mid_sign < 0){ if(mid_sign < 0){
start = mid; low = mid;
} else if (mid_sign > 0){ } else if (mid_sign > 0){
end = mid; high = mid;
} else { } else if (mid_sign == 0){
result.content = mid; low = mid;
result.empty = 0; high = mid;
return result;
} }
} }
} }
if(convergence_condition){
result.content = low;
result.empty = 0;
} else{
result.empty = 1;
}
return result; return result;
} }
} }
// sampler based on inverse cdf // sampler based on inverse cdf
// to-do: integrals // to-do: integrals
// main with an example // main with an example
int main(){ int main(){
printf("Hello world");
// Uniform:
struct box result = inverse_cdf(cdf_uniform_0_1, 0.5);
if(result.empty){
printf("Inverse not calculated\n");
exit(1);
}else{
printf("Inverse of the cdf at %f is: %f\n", 0.5, result.content);
}
// Squared cdf
struct box result2 = inverse_cdf(cdf_squared_0_1, 0.5);
if(result2.empty){
printf("Inverse not calculated\n");
exit(1);
}else{
printf("Inverse of the cdf at %f is: %f\n", 0.5, result2.content);
}
return 0; return 0;
} }