commit 39ab972fa7bbf9bde0657a1480b2348a4b083cf1
parent 3c49d3d2fd78d955d2557a1311250ad0e965da22
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Tue, 1 Aug 2023 13:05:02 +0200
add burning fat example
Diffstat:
3 files changed, 101 insertions(+), 0 deletions(-)
diff --git a/examples/09_burn_10kg_fat/example b/examples/09_burn_10kg_fat/example
Binary files differ.
diff --git a/examples/09_burn_10kg_fat/example.c b/examples/09_burn_10kg_fat/example.c
@@ -0,0 +1,48 @@
+#include "../../squiggle.h"
+#include <math.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+double sample_minutes_per_day_jumping_rope_needed_to_burn_10kg(uint64_t* seed)
+{
+ double kcal_jumping_rope_minute = sample_to(15, 20, seed);
+ double kcal_jumping_rope_hour = kcal_jumping_rope_minute * 60;
+
+ double kcal_in_kg_of_fat = 7700;
+ double num_kg_of_fat_to_lose = 10;
+
+ double hours_jumping_rope_needed = kcal_in_kg_of_fat * num_kg_of_fat_to_lose / kcal_jumping_rope_hour;
+
+ double days_until_end_of_year = 152; // as of 2023-08-01
+ double hours_per_day = hours_jumping_rope_needed / days_until_end_of_year;
+ double minutes_per_day = hours_per_day * 60;
+ return minutes_per_day;
+}
+
+int main()
+{
+ // set randomness seed
+ uint64_t* seed = malloc(sizeof(uint64_t));
+ *seed = 1000; // xorshift can't start with 0
+
+ int n = 1000 * 1000;
+
+ double* result = malloc(sizeof(double) * n);
+ for (int i = 0; i < n; i++) {
+ result[i] = sample_minutes_per_day_jumping_rope_needed_to_burn_10kg(seed);
+ }
+
+ printf("How many minutes per day do I have to jump rope to lose 10kg of fat by the end of the year?\n");
+ printf("Mean: %f\n", array_mean(result, n));
+ printf("A few samples: [ ");
+ for (int i = 0; i < 9; i++) {
+ printf("%.6f, ", result[i]);
+ }
+ printf("... ]\n");
+
+ struct c_i c_i_90 = get_90_confidence_interval(sample_minutes_per_day_jumping_rope_needed_to_burn_10kg, seed);
+ printf("90%% confidence interval: [%f, %f]\n", c_i_90.low, c_i_90.high);
+
+ free(seed);
+}
diff --git a/examples/09_burn_10kg_fat/makefile b/examples/09_burn_10kg_fat/makefile
@@ -0,0 +1,53 @@
+# Interface:
+# make
+# make build
+# make format
+# make run
+
+# Compiler
+CC=gcc
+# CC=tcc # <= faster compilation
+
+# Main file
+SRC=example.c ../../squiggle.c
+OUTPUT=example
+
+## Dependencies
+MATH=-lm
+
+## Flags
+DEBUG= #'-g'
+STANDARD=-std=c99
+WARNINGS=-Wall
+OPTIMIZED=-O3 #-Ofast
+# OPENMP=-fopenmp
+
+## Formatter
+STYLE_BLUEPRINT=webkit
+FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
+
+## make build
+build: $(SRC)
+ $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT)
+
+format: $(SRC)
+ $(FORMATTER) $(SRC)
+
+run: $(SRC) $(OUTPUT)
+ OMP_NUM_THREADS=1 ./$(OUTPUT) && echo
+
+time-linux:
+ @echo "Requires /bin/time, found on GNU/Linux systems" && echo
+
+ @echo "Running 100x and taking avg time $(OUTPUT)"
+ @t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 1 thread: |" | sed 's|$$|ms|' && echo
+
+## Profiling
+
+profile-linux:
+ echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar"
+ echo "Must be run as sudo"
+ $(CC) $(SRC) $(MATH) -o $(OUTPUT)
+ sudo perf record ./$(OUTPUT)
+ sudo perf report
+ rm perf.data