add mean and std for arrays.

This commit is contained in:
NunoSempere 2023-07-22 19:36:43 +02:00
parent 57adb08057
commit f9c64426d7
3 changed files with 23 additions and 5 deletions

View File

@ -77,9 +77,8 @@ Behaviour on error can be toggled by the `EXIT_ON_ERROR` variable. This library
## To do list ## To do list
- [ ] Rename functions to something more self-explanatory, e.g,. sample_sample_sample_unit_normal.
- [ ] Have some more complicated & realistic example - [ ] Have some more complicated & realistic example
- [ ] Add summarization functions, like mean, std, 90% ci (or all c.i.?) - [ ] Add summarization functions: 90% ci (or all c.i.?)
- [ ] Publish online - [ ] Publish online
- [ ] Support all distribution functions in <https://www.squiggle-language.com/docs/Api/Dist> - [ ] Support all distribution functions in <https://www.squiggle-language.com/docs/Api/Dist>
- [ ] Support all distribution functions in <https://www.squiggle-language.com/docs/Api/Dist>, and do so efficiently - [ ] Support all distribution functions in <https://www.squiggle-language.com/docs/Api/Dist>, and do so efficiently
@ -105,3 +104,5 @@ Behaviour on error can be toggled by the `EXIT_ON_ERROR` variable. This library
- [x] Explain nested functions - [x] Explain nested functions
- [x] Explain exit on error - [x] Explain exit on error
- [x] Explain individual examples - [x] Explain individual examples
- [x] Rename functions to something more self-explanatory, e.g,. `sample_unit_normal`.
- [x] Add summarization functions: mean, std

View File

@ -76,11 +76,11 @@ float sample_to(float low, float high, uint32_t* seed)
// Array helpers // Array helpers
float array_sum(float* array, int length) float array_sum(float* array, int length)
{ {
float output = 0.0; float sum = 0.0;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
output += array[i]; sum += array[i];
} }
return output; return sum;
} }
void array_cumsum(float* array_to_sum, float* array_cumsummed, int length) void array_cumsum(float* array_to_sum, float* array_cumsummed, int length)
@ -91,6 +91,21 @@ void array_cumsum(float* array_to_sum, float* array_cumsummed, int length)
} }
} }
float array_mean(float* array, int length){
float sum = array_sum(array, length);
return sum / length;
}
float array_std(float* array, int length){
float mean = array_mean(array, length);
float std = 0.0;
for (int i = 0; i < length; i++) {
std += pow(array[i] - mean, 2.0);
}
std=sqrt(std/length);
return std;
}
// Mixture function // Mixture function
float sample_mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint32_t* seed) float sample_mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint32_t* seed)
{ {

View File

@ -20,6 +20,8 @@ float sample_to(float low, float high, uint32_t* seed);
// Array helpers // Array helpers
float array_sum(float* array, int length); float array_sum(float* array, int length);
void array_cumsum(float* array_to_sum, float* array_cumsummed, int length); void array_cumsum(float* array_to_sum, float* array_cumsummed, int length);
float array_mean(float* array, int length);
float array_std(float* array, int length);
// Mixture function // Mixture function
float sample_mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint32_t* seed); float sample_mixture(float (*samplers[])(uint32_t*), float* weights, int n_dists, uint32_t* seed);