fix constant name 95=>90

This commit is contained in:
NunoSempere 2023-09-27 14:10:40 +01:00
parent 015d33adca
commit b1a58f9b74
4 changed files with 88 additions and 9 deletions

56
scratchpad/makefile Normal file
View File

@ -0,0 +1,56 @@
# Interface:
# make
# make build
# make format
# make run
# Compiler
CC=gcc
# CC=tcc # <= faster compilation
# Main file
SRC=scratchpad.c ../squiggle.c
OUTPUT=scratchpad
## 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)
./$(OUTPUT)
verify: $(SRC) $(OUTPUT)
./$(OUTPUT) | grep "NOT passed" -A 2 --group-separator='' || true
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

BIN
scratchpad/scratchpad Executable file

Binary file not shown.

20
scratchpad/scratchpad.c Normal file
View File

@ -0,0 +1,20 @@
#include "../squiggle.h"
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
// set randomness seed
uint64_t* seed = malloc(sizeof(uint64_t));
*seed = 1000; // xorshift can't start with a seed of 0
for (int i = 0; i < 100; i++) {
double draw = sample_unit_uniform(seed);
printf("%f\n", draw);
}
free(seed);
}

View File

@ -13,7 +13,7 @@
#define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__) #define PROCESS_ERROR(error_msg) process_error(error_msg, EXIT_ON_ERROR, __FILE__, __LINE__)
#define PI 3.14159265358979323846 // M_PI in gcc gnu99 #define PI 3.14159265358979323846 // M_PI in gcc gnu99
#define NORMAL95CONFIDENCE 1.6448536269514722 #define NORMAL90CONFIDENCE 1.6448536269514722
// # Key functionality // # Key functionality
// Define the minimum number of functions needed to do simple estimation // Define the minimum number of functions needed to do simple estimation
@ -23,9 +23,12 @@
uint64_t xorshift32(uint32_t* seed) uint64_t xorshift32(uint32_t* seed)
{ {
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" // Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs"
// See <https://stackoverflow.com/questions/53886131/how-does-xorshift32-works> // See:
// https://en.wikipedia.org/wiki/Xorshift // <https://en.wikipedia.org/wiki/Xorshift>
// Also some drama: <https://www.pcg-random.org/posts/on-vignas-pcg-critique.html>, <https://prng.di.unimi.it/> // <https://stackoverflow.com/questions/53886131/how-does-xorshift32-works>,
// Also some drama:
// <https://www.pcg-random.org/posts/on-vignas-pcg-critique.html>,
// <https://prng.di.unimi.it/>
uint64_t x = *seed; uint64_t x = *seed;
x ^= x << 13; x ^= x << 13;
x ^= x >> 17; x ^= x >> 17;
@ -53,8 +56,8 @@ double sample_unit_uniform(uint64_t* seed)
double sample_unit_normal(uint64_t* seed) double sample_unit_normal(uint64_t* seed)
{ {
// See: <https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform> // // See: <https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform>
double u1 = sample_unit_uniform(seed); // double u1 = sample_unit_uniform(seed);
double u2 = sample_unit_uniform(seed); double u2 = sample_unit_uniform(seed);
double z = sqrtf(-2.0 * log(u1)) * sin(2 * PI * u2); double z = sqrtf(-2.0 * log(u1)) * sin(2 * PI * u2);
return z; return z;
@ -96,7 +99,7 @@ inline double sample_normal_from_90_confidence_interval(double low, double high,
// we can set mean = (high + low)/2; the midpoint, and L = high-low, // we can set mean = (high + low)/2; the midpoint, and L = high-low,
// Normal([high + low]/2, [high - low]/(2 * 1.6448536269514722)) // Normal([high + low]/2, [high - low]/(2 * 1.6448536269514722))
double mean = (high + low) / 2.0; double mean = (high + low) / 2.0;
double std = (high - low) / (2.0 * NORMAL95CONFIDENCE); double std = (high - low) / (2.0 * NORMAL90CONFIDENCE);
return sample_normal(mean, std, seed); return sample_normal(mean, std, seed);
} }
@ -507,14 +510,14 @@ lognormal_params convert_ci_to_lognormal_params(ci x)
double loghigh = logf(x.high); double loghigh = logf(x.high);
double loglow = logf(x.low); double loglow = logf(x.low);
double logmean = (loghigh + loglow) / 2.0; double logmean = (loghigh + loglow) / 2.0;
double logstd = (loghigh - loglow) / (2.0 * NORMAL95CONFIDENCE); double logstd = (loghigh - loglow) / (2.0 * NORMAL90CONFIDENCE);
lognormal_params result = { .logmean = logmean, .logstd = logstd}; lognormal_params result = { .logmean = logmean, .logstd = logstd};
return result; return result;
} }
ci convert_lognormal_params_to_ci(lognormal_params y) ci convert_lognormal_params_to_ci(lognormal_params y)
{ {
double h = y.logstd * NORMAL95CONFIDENCE; double h = y.logstd * NORMAL90CONFIDENCE;
double loghigh = y.logmean + h; double loghigh = y.logmean + h;
double loglow = y.logmean - h; double loglow = y.logmean - h;
ci result = { .low=exp(loglow), .high=exp(loghigh)}; ci result = { .low=exp(loglow), .high=exp(loghigh)};