diff options
| author | Ethan Werner <mail@ethanwerner.com> | 2026-07-01 09:42:39 +0400 |
|---|---|---|
| committer | Ethan Werner <mail@ethanwerner.com> | 2026-07-01 09:42:39 +0400 |
| commit | 07e18043145f3d91c8ea7ec3ad41b3c905585277 (patch) | |
| tree | 2bdeae46973f4bdfce263679a4008ac768011f28 /lib/beta_pdf.c | |
initial commit: ts time series analysis library v0.1.0
- 354 financial and mathematical time-series indicators
- Uniform C11 API with consistent function signatures
- 349 passing tests with -Werror
- Static and shared library builds
- pkg-config integration
- Python bindings
- Fuzzing and sanitizer support
- Security hardening flags
Diffstat (limited to 'lib/beta_pdf.c')
| -rw-r--r-- | lib/beta_pdf.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/beta_pdf.c b/lib/beta_pdf.c new file mode 100644 index 0000000..a23ed68 --- /dev/null +++ b/lib/beta_pdf.c @@ -0,0 +1,52 @@ +// Beta PDF +// +// beta_pdf - beta probability density +// +// f(x; α, β) = x^(α-1) * (1-x)^(β-1) / B(α, β), for 0 < x < 1 +// +// Inputs: 1 +// Outputs: 1 (PDF value) +// Constants: 2 (alpha, beta) + +#include <math.h> +#include "ts.h" + +typedef struct { + fp_t alpha; + fp_t beta; +} ts_beta_pdf_params_t; + +ts_parameter_t const ts_beta_pdf_p = { .n_inputs = 1, .n_outputs = 1, .n_constants = 2, .params_size = sizeof(ts_beta_pdf_params_t) }; + +uint_t ts_beta_pdf_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( 0 ); +} + +int ts_beta_pdf( + fp_t * const *y, uint_t y_n, + fp_t const * const *x, uint_t x_n, + uint_t n, ts_constant_t const *c +) +{ + TS_PROLOGUE( ts_beta_pdf_p ); + if ( !c ) return -1; + fp_t alpha = ((ts_beta_pdf_params_t *)c)->alpha; + fp_t beta = ((ts_beta_pdf_params_t *)c)->beta; + if (isnan(alpha) || isnan(beta) || alpha <= 0 || beta <= 0) + { + for( uint_t i = 0; i < n; ++i ) y[0][i] = 0; + return -1; + } + fp_t beta_fn = exp( lgamma( alpha ) + lgamma( beta ) - lgamma( alpha + beta ) ); + for( uint_t i = 0; i < n; ++i ) + { + fp_t xi = x[0][i]; + y[0][i] = ( xi > 0 && xi < 1 ) ? pow( xi, alpha - 1.0 ) * pow( 1.0 - xi, beta - 1.0 ) / beta_fn : 0.0; + } + return 0; +} + + + |