added better error messages!
This commit is contained in:
		
							parent
							
								
									75689c506d
								
							
						
					
					
						commit
						aca99a3386
					
				|  | @ -1,24 +1,19 @@ | |||
| #include <float.h> // FLT_MAX, FLT_MIN | ||||
| #include <limits.h> // INT_MAX | ||||
| #include <math.h> | ||||
| #include <stdint.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #define VERBOSE 1 | ||||
| #include <float.h> // FLT_MAX, FLT_MIN | ||||
| #include <stdio.h> | ||||
| #include <math.h> // erf, sqrt | ||||
| 
 | ||||
| #define EXIT_ON_ERROR 0 | ||||
| 
 | ||||
| // Errors
 | ||||
| // https://mccue.dev/pages/7-27-22-c-errors
 | ||||
| // Huh, first time I've really needed a struct
 | ||||
| // https://en.wikipedia.org/wiki/Struct_(C_programming_language)
 | ||||
| // https://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)Structs.html
 | ||||
| // https://stackoverflow.com/questions/9653072/return-a-struct-from-a-function-in-c
 | ||||
| // options:
 | ||||
| //   - exit
 | ||||
| //   - pass structs
 | ||||
| // to do: reuse more informative printing from build-your-own-lisp?
 | ||||
| // [ ] to do: reuse more informative printing from build-your-own-lisp?
 | ||||
| // Another option could be to exit on error. Maybe let the user decide?
 | ||||
| struct box { | ||||
|     int empty; | ||||
|     float content; | ||||
| 		char * error_msg; | ||||
| }; | ||||
| 
 | ||||
| // Example cdf
 | ||||
|  | @ -48,23 +43,23 @@ float cdf_normal_0_1(float x) | |||
| { | ||||
|     float mean = 0; | ||||
|     float std = 1; | ||||
|     return 0.5 * (1 + erf((x - mean) / (std * sqrt(2)))); | ||||
|     return 0.5 * (1 + erf((x - mean) / (std * sqrt(2)))); // erf from math.h
 | ||||
| } | ||||
| 
 | ||||
| // Inverse cdf
 | ||||
| struct box inverse_cdf(float cdf(float), float p) | ||||
| { | ||||
|     // given a cdf: [-Inf, Inf] => [0,1]
 | ||||
|     // returns x such that cdf(x) = p
 | ||||
|     // [x] to do: add bounds, add error checking
 | ||||
|     // [x] maybe return a struct or smth.
 | ||||
|     // returns a box with either
 | ||||
| 		// x such that cdf(x) = p
 | ||||
| 		// or an error
 | ||||
| 		// if EXIT_ON_ERROR is set to 1, it exits instead of providing an error
 | ||||
| 
 | ||||
|     struct box result; | ||||
|     float low = -1.0; | ||||
|     float high = 1.0; | ||||
| 
 | ||||
|     // 1. Make sure that cdf(low) < p < cdf(high)
 | ||||
|     // [x] to do: do smth with float min and float max?
 | ||||
|     int interval_found = 0; | ||||
|     while ((!interval_found) && (low > -FLT_MAX / 4) && (high < FLT_MAX / 4)) { | ||||
|         // ^ Using FLT_MIN and FLT_MAX is overkill
 | ||||
|  | @ -80,17 +75,18 @@ struct box inverse_cdf(float cdf(float), float p) | |||
|             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) { | ||||
|         result.empty = 1; | ||||
|         return result; | ||||
| 		if (!interval_found) { | ||||
| 			  if(EXIT_ON_ERROR){ | ||||
| 					printf("Interval containing the target value not found, in function inverse_cdf, in %s (%d)", __FILE__, __LINE__); | ||||
| 					exit(1); | ||||
| 				}else{ | ||||
| 					char error_msg[200]; | ||||
| 					snprintf(error_msg, 200, "Interval containing the target value not found in function inverse_cdf, in %s (%d)", __FILE__, __LINE__); | ||||
| 					result.empty = 1; | ||||
| 					result.error_msg = error_msg; | ||||
| 					return result; | ||||
| 				} | ||||
|     } else { | ||||
| 
 | ||||
|         int convergence_condition = 0; | ||||
|  | @ -123,8 +119,18 @@ struct box inverse_cdf(float cdf(float), float p) | |||
|             result.content = low; | ||||
|             result.empty = 0; | ||||
|         } else { | ||||
|             result.empty = 1; | ||||
|         } | ||||
| 						if(EXIT_ON_ERROR){ | ||||
| 							printf("Search process did not converge, in function inverse_cdf, in %s (%d)", __FILE__, __LINE__); | ||||
| 							exit(1); | ||||
| 						}else{ | ||||
| 							char error_msg[200]; | ||||
| 							snprintf(error_msg, 200, "Search process did not converge, in function inverse_cdf, in %s (%d)", __FILE__, __LINE__); | ||||
| 							result.empty = 1; | ||||
| 							result.error_msg = error_msg; | ||||
| 							return result; | ||||
| 						} | ||||
| 								result.empty = 1; | ||||
| 					} | ||||
| 
 | ||||
|         return result; | ||||
|     } | ||||
|  | @ -146,13 +152,12 @@ uint32_t xorshift32(uint32_t* seed) | |||
| } | ||||
| 
 | ||||
| // Distribution & sampling functions
 | ||||
| 
 | ||||
| float rand_0_to_1(uint32_t* seed) | ||||
| { | ||||
|     return ((float)xorshift32(seed)) / ((float)UINT32_MAX); | ||||
| } | ||||
| 
 | ||||
| // sampler based on inverse cdf
 | ||||
| // Sampler based on inverse cdf
 | ||||
| struct box sampler(float cdf(float), uint32_t* seed) | ||||
| { | ||||
|     struct box result; | ||||
|  | @ -161,16 +166,15 @@ struct box sampler(float cdf(float), uint32_t* seed) | |||
|     return result; | ||||
| } | ||||
| 
 | ||||
| // ~~[-] to-do: integrals => beta distribution~~
 | ||||
| // => instead, use this incomplete beta function implementation, based on continuous fractions:
 | ||||
| // to do: add beta. 
 | ||||
| // for the cdf, use this incomplete beta function implementation, based on continuous fractions:
 | ||||
| // <https://codeplea.com/incomplete-beta-function-c>
 | ||||
| // <https://github.com/codeplea/incbeta>
 | ||||
| 
 | ||||
| // main with an example
 | ||||
| int main() | ||||
| { | ||||
| 
 | ||||
|     // Uniform:
 | ||||
|     // Get the inverse cdf of a [0,1] uniform distribution at 0.5
 | ||||
|     struct box result_1 = inverse_cdf(cdf_uniform_0_1, 0.5); | ||||
|     char* name_1 = "cdf_uniform_0_1"; | ||||
|     if (result_1.empty) { | ||||
|  | @ -180,7 +184,7 @@ int main() | |||
|         printf("Inverse of %s at %f is: %f\n", name_1, 0.5, result_1.content); | ||||
|     } | ||||
| 
 | ||||
|     // Squared cdf
 | ||||
|     // Get the inverse cdf of a [0,1] squared distribution at 0.5
 | ||||
|     struct box result_2 = inverse_cdf(cdf_squared_0_1, 0.5); | ||||
|     char* name_2 = "cdf_squared_0_1"; | ||||
|     if (result_2.empty) { | ||||
|  | @ -190,7 +194,7 @@ int main() | |||
|         printf("Inverse of %s at %f is: %f\n", name_2, 0.5, result_2.content); | ||||
|     } | ||||
| 
 | ||||
|     // Normal cdf
 | ||||
| 		// Get the inverse of a normal(0,1) cdf distribution
 | ||||
|     struct box result_3 = inverse_cdf(cdf_normal_0_1, 0.5); | ||||
|     char* name_3 = "cdf_normal_0_1"; | ||||
|     if (result_3.empty) { | ||||
|  | @ -200,6 +204,7 @@ int main() | |||
|         printf("Inverse of %s at %f is: %f\n", name_3, 0.5, result_3.content); | ||||
|     } | ||||
| 
 | ||||
| 		// Use the sampler on a normal(0,1)
 | ||||
|     // set randomness seed
 | ||||
|     uint32_t* seed = malloc(sizeof(uint32_t)); | ||||
|     *seed = 1000; // xorshift can't start with 0
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user