LMMC
Lamina Mathematic core
载入中...
搜索中...
未找到
stdlib_statistics.c
浏览该文件的文档.
1#include "lmmc/stdlib.h"
2
3#include "lmmc/stats.h"
4
5#include "stdlib_internal.h"
6
7static lmmc_status_t lmmc_std_wrap_const_vec(const lmmc_real_t* values,
8 size_t count,
9 lmmc_vec_t* out)
10{
11 if (!values || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
12 if (count == 0) { return LMMC_STATUS_EMPTY_INPUT; }
13 if (!lmmc_std_real_array_is_finite(values, count)) {
15 }
16 out->size = count;
17 out->data = (lmmc_real_t*)values;
18 out->owns_data = 0;
19 return LMMC_STATUS_OK;
20}
21
22lmmc_status_t lmmc_std_stats_mean(const lmmc_real_t* values, size_t count,
23 lmmc_real_t* out)
24{
25 lmmc_vec_t view;
26 lmmc_real_t value;
27 lmmc_status_t status;
28 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
29 status = lmmc_std_wrap_const_vec(values, count, &view);
30 if (status != LMMC_STATUS_OK) { return status; }
31 status = lmmc_vec_mean(&view, &value);
32 if (status != LMMC_STATUS_OK) { return status; }
33 return lmmc_std_store_finite_real(value, out);
34}
35
37 lmmc_real_t* out)
38{
39 lmmc_vec_t view;
40 lmmc_real_t value;
41 lmmc_status_t status;
42 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
43 status = lmmc_std_wrap_const_vec(values, count, &view);
44 if (status != LMMC_STATUS_OK) { return status; }
45 status = lmmc_vec_median(&view, &value);
46 if (status != LMMC_STATUS_OK) { return status; }
47 return lmmc_std_store_finite_real(value, out);
48}
49
50lmmc_status_t lmmc_std_stats_var(const lmmc_real_t* values, size_t count,
51 lmmc_real_t* out)
52{
53 lmmc_vec_t view;
54 lmmc_real_t value;
55 lmmc_status_t status;
56 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
57 status = lmmc_std_wrap_const_vec(values, count, &view);
58 if (status != LMMC_STATUS_OK) { return status; }
59 status = lmmc_vec_variance_sample(&view, &value);
60 if (status != LMMC_STATUS_OK) { return status; }
61 return lmmc_std_store_finite_real(value, out);
62}
63
64lmmc_status_t lmmc_std_stats_std(const lmmc_real_t* values, size_t count,
65 lmmc_real_t* out)
66{
67 lmmc_vec_t view;
68 lmmc_real_t value;
69 lmmc_status_t status;
70 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
71 status = lmmc_std_wrap_const_vec(values, count, &view);
72 if (status != LMMC_STATUS_OK) { return status; }
73 status = lmmc_vec_stddev_sample(&view, &value);
74 if (status != LMMC_STATUS_OK) { return status; }
75 return lmmc_std_store_finite_real(value, out);
76}
77
80{
81 lmmc_vec_t view;
82 lmmc_real_t value;
83 lmmc_status_t status;
84 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
85 if (!lmmc_std_real_is_finite(q)) { return LMMC_STATUS_NUMERICAL_FAILURE; }
86 if (q < (lmmc_real_t)0 || q > (lmmc_real_t)1) {
88 }
89 status = lmmc_std_wrap_const_vec(values, count, &view);
90 if (status != LMMC_STATUS_OK) { return status; }
91 status = lmmc_vec_quantile(&view, q, &value);
92 if (status != LMMC_STATUS_OK) { return status; }
93 return lmmc_std_store_finite_real(value, out);
94}
95
97 const lmmc_real_t* y,
98 size_t count,
99 lmmc_real_t* out)
100{
101 lmmc_vec_t x_view;
102 lmmc_vec_t y_view;
103 lmmc_real_t value;
104 lmmc_status_t status;
105 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
106 status = lmmc_std_wrap_const_vec(x, count, &x_view);
107 if (status != LMMC_STATUS_OK) { return status; }
108 status = lmmc_std_wrap_const_vec(y, count, &y_view);
109 if (status != LMMC_STATUS_OK) { return status; }
110 status = lmmc_vec_covariance_sample(&x_view, &y_view, &value);
111 if (status != LMMC_STATUS_OK) { return status; }
112 return lmmc_std_store_finite_real(value, out);
113}
114
116 const lmmc_real_t* y,
117 size_t count,
118 lmmc_real_t* out)
119{
120 lmmc_vec_t x_view;
121 lmmc_vec_t y_view;
122 lmmc_real_t value;
123 lmmc_status_t status;
124 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
125 status = lmmc_std_wrap_const_vec(x, count, &x_view);
126 if (status != LMMC_STATUS_OK) { return status; }
127 status = lmmc_std_wrap_const_vec(y, count, &y_view);
128 if (status != LMMC_STATUS_OK) { return status; }
129 status = lmmc_vec_correlation_sample(&x_view, &y_view, &value);
130 if (status != LMMC_STATUS_OK) { return status; }
131 return lmmc_std_store_finite_real(value, out);
132}
134 lmmc_real_t*);
137
138static lmmc_status_t lmmc_std_stats_call_dist2(lmmc_std_dist2_fn fn,
139 lmmc_real_t a,
140 lmmc_real_t b,
141 lmmc_real_t* out)
142{
143 lmmc_real_t value;
144 lmmc_status_t status;
145 if (!fn || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
146 if (!lmmc_std_real_is_finite(a) || !lmmc_std_real_is_finite(b)) {
148 }
149 status = fn(a, b, &value);
150 if (status != LMMC_STATUS_OK) { return status; }
151 return lmmc_std_store_finite_real(value, out);
152}
153
154static lmmc_status_t lmmc_std_stats_call_dist3(lmmc_std_dist3_fn fn,
155 lmmc_real_t a,
156 lmmc_real_t b,
157 lmmc_real_t c,
158 lmmc_real_t* out)
159{
160 lmmc_real_t value;
161 lmmc_status_t status;
162 if (!fn || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
163 if (!lmmc_std_real_is_finite(a) || !lmmc_std_real_is_finite(b) ||
164 !lmmc_std_real_is_finite(c)) {
166 }
167 status = fn(a, b, c, &value);
168 if (status != LMMC_STATUS_OK) { return status; }
169 return lmmc_std_store_finite_real(value, out);
170}
171
173 lmmc_real_t stddev,
174 lmmc_real_t* out)
175{
176 return lmmc_std_stats_call_dist3(lmmc_dist_normal_pdf, x, mean, stddev,
177 out);
178}
179
181 lmmc_real_t stddev,
182 lmmc_real_t* out)
183{
184 return lmmc_std_stats_call_dist3(lmmc_dist_normal_cdf, x, mean, stddev,
185 out);
186}
187
189 lmmc_real_t mean,
190 lmmc_real_t stddev,
191 lmmc_real_t* out)
192{
193 return lmmc_std_stats_call_dist3(lmmc_dist_normal_quantile, p, mean,
194 stddev, out);
195}
196
198 lmmc_real_t* out)
199{
200 return lmmc_std_stats_call_dist2(lmmc_dist_t_pdf, x, df, out);
201}
202
204 lmmc_real_t* out)
205{
206 return lmmc_std_stats_call_dist2(lmmc_dist_t_cdf, x, df, out);
207}
208
210 lmmc_real_t* out)
211{
212 return lmmc_std_stats_call_dist2(lmmc_dist_t_quantile, p, df, out);
213}
214
216 lmmc_real_t* out)
217{
218 return lmmc_std_stats_call_dist2(lmmc_dist_chi2_pdf, x, df, out);
219}
220
222 lmmc_real_t* out)
223{
224 return lmmc_std_stats_call_dist2(lmmc_dist_chi2_cdf, x, df, out);
225}
226
228 lmmc_real_t* out)
229{
230 return lmmc_std_stats_call_dist2(lmmc_dist_chi2_quantile, p, df, out);
231}
232
234 lmmc_real_t df2, lmmc_real_t* out)
235{
236 return lmmc_std_stats_call_dist3(lmmc_dist_f_pdf, x, df1, df2, out);
237}
238
240 lmmc_real_t df2, lmmc_real_t* out)
241{
242 return lmmc_std_stats_call_dist3(lmmc_dist_f_cdf, x, df1, df2, out);
243}
244
246 lmmc_real_t df2,
247 lmmc_real_t* out)
248{
249 return lmmc_std_stats_call_dist3(lmmc_dist_f_quantile, p, df1, df2,
250 out);
251}
252
254 lmmc_real_t scale,
255 lmmc_real_t* out)
256{
257 return lmmc_std_stats_call_dist3(lmmc_dist_gamma_pdf, x, shape, scale,
258 out);
259}
260
262 lmmc_real_t scale,
263 lmmc_real_t* out)
264{
265 return lmmc_std_stats_call_dist3(lmmc_dist_gamma_cdf, x, shape, scale,
266 out);
267}
268
270 lmmc_real_t shape,
271 lmmc_real_t scale,
272 lmmc_real_t* out)
273{
274 return lmmc_std_stats_call_dist3(lmmc_dist_gamma_quantile, p, shape,
275 scale, out);
276}
277
279 lmmc_real_t beta,
280 lmmc_real_t* out)
281{
282 return lmmc_std_stats_call_dist3(lmmc_dist_beta_pdf, x, alpha, beta,
283 out);
284}
285
287 lmmc_real_t beta,
288 lmmc_real_t* out)
289{
290 return lmmc_std_stats_call_dist3(lmmc_dist_beta_cdf, x, alpha, beta,
291 out);
292}
293
295 lmmc_real_t alpha,
296 lmmc_real_t beta,
297 lmmc_real_t* out)
298{
299 return lmmc_std_stats_call_dist3(lmmc_dist_beta_quantile, p, alpha,
300 beta, out);
301}
302
304 lmmc_real_t p,
305 lmmc_real_t* out)
306{
307 lmmc_real_t value;
308 lmmc_status_t status;
309 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
310 if (!lmmc_std_real_is_finite(p)) { return LMMC_STATUS_NUMERICAL_FAILURE; }
311 status = lmmc_dist_binomial_pmf(k, n, p, &value);
312 if (status != LMMC_STATUS_OK) { return status; }
313 return lmmc_std_store_finite_real(value, out);
314}
315
317 lmmc_real_t p,
318 lmmc_real_t* out)
319{
320 lmmc_real_t value;
321 lmmc_status_t status;
322 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
323 if (!lmmc_std_real_is_finite(p)) { return LMMC_STATUS_NUMERICAL_FAILURE; }
324 status = lmmc_dist_binomial_cdf(k, n, p, &value);
325 if (status != LMMC_STATUS_OK) { return status; }
326 return lmmc_std_store_finite_real(value, out);
327}
328
330 lmmc_real_t* out)
331{
332 lmmc_real_t value;
333 lmmc_status_t status;
334 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
335 if (!lmmc_std_real_is_finite(lambda)) {
337 }
338 status = lmmc_dist_poisson_pmf(k, lambda, &value);
339 if (status != LMMC_STATUS_OK) { return status; }
340 return lmmc_std_store_finite_real(value, out);
341}
342
344 lmmc_real_t* out)
345{
346 lmmc_real_t value;
347 lmmc_status_t status;
348 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
349 if (!lmmc_std_real_is_finite(lambda)) {
351 }
352 status = lmmc_dist_poisson_cdf(k, lambda, &value);
353 if (status != LMMC_STATUS_OK) { return status; }
354 return lmmc_std_store_finite_real(value, out);
355}
double lmmc_real_t
Public and internal LMMC real type.
lmmc_real_t * data
一维稠密向量。
基础统计量与组合数学接口。
lmmc_status_t lmmc_dist_poisson_cdf(size_t k, lmmc_real_t lambda, lmmc_real_t *out)
泊松分布累积分布函数;直接计算正则化上不完全伽马尾。
lmmc_status_t lmmc_vec_quantile(const lmmc_vec_t *x, lmmc_real_t p, lmmc_real_t *out)
计算向量的 p 分位数(线性插值法)。
lmmc_status_t lmmc_dist_chi2_quantile(lmmc_real_t p, lmmc_real_t df, lmmc_real_t *out)
χ² 分布分位数函数。
lmmc_status_t lmmc_dist_normal_quantile(lmmc_real_t p, lmmc_real_t mu, lmmc_real_t sigma, lmmc_real_t *out)
正态分布分位数函数(逆 CDF)。
lmmc_status_t lmmc_vec_variance_sample(const lmmc_vec_t *x, lmmc_real_t *out_variance)
计算样本方差 。
lmmc_status_t lmmc_dist_beta_pdf(lmmc_real_t x, lmmc_real_t alpha, lmmc_real_t beta, lmmc_real_t *out)
贝塔分布概率密度函数。
lmmc_status_t lmmc_vec_mean(const lmmc_vec_t *x, lmmc_real_t *out_mean)
计算向量样本均值 。
lmmc_status_t lmmc_dist_binomial_cdf(size_t k, size_t n, lmmc_real_t p_param, lmmc_real_t *out)
二项分布累积分布函数。
lmmc_status_t lmmc_dist_chi2_cdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
χ² 分布累积分布函数。
lmmc_status_t lmmc_dist_t_cdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
t 分布累积分布函数。
lmmc_status_t lmmc_dist_gamma_quantile(lmmc_real_t p, lmmc_real_t shape, lmmc_real_t scale, lmmc_real_t *out)
伽马分布分位数函数。
lmmc_status_t lmmc_dist_normal_cdf(lmmc_real_t x, lmmc_real_t mu, lmmc_real_t sigma, lmmc_real_t *out)
正态分布累积分布函数。
lmmc_status_t lmmc_dist_f_quantile(lmmc_real_t p, lmmc_real_t df1, lmmc_real_t df2, lmmc_real_t *out)
F 分布分位数函数。
lmmc_status_t lmmc_dist_gamma_pdf(lmmc_real_t x, lmmc_real_t shape, lmmc_real_t scale, lmmc_real_t *out)
伽马分布概率密度函数。
lmmc_status_t lmmc_dist_normal_pdf(lmmc_real_t x, lmmc_real_t mu, lmmc_real_t sigma, lmmc_real_t *out)
连续分布函数的公共参数约束。
lmmc_status_t lmmc_vec_stddev_sample(const lmmc_vec_t *x, lmmc_real_t *out_stddev)
样本标准差,要求 n>=2 。 缩放计算支持方差超出 binary64 范围的情况。 输入非有限或标准差结果溢出时返回 LMMC_STATUS_NUMERICAL_FAILURE。
lmmc_status_t lmmc_dist_t_quantile(lmmc_real_t p, lmmc_real_t df, lmmc_real_t *out)
t 分布分位数函数。
lmmc_status_t lmmc_dist_beta_cdf(lmmc_real_t x, lmmc_real_t alpha, lmmc_real_t beta, lmmc_real_t *out)
贝塔分布累积分布函数。
lmmc_status_t lmmc_dist_gamma_cdf(lmmc_real_t x, lmmc_real_t shape, lmmc_real_t scale, lmmc_real_t *out)
伽马分布累积分布函数。
lmmc_status_t lmmc_dist_f_cdf(lmmc_real_t x, lmmc_real_t df1, lmmc_real_t df2, lmmc_real_t *out)
F 分布累积分布函数。
lmmc_status_t lmmc_dist_chi2_pdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
χ² 分布概率密度函数。
lmmc_status_t lmmc_dist_poisson_pmf(size_t k, lmmc_real_t lambda, lmmc_real_t *out)
泊松分布概率质量函数。
lmmc_status_t lmmc_vec_covariance_sample(const lmmc_vec_t *x, const lmmc_vec_t *y, lmmc_real_t *out_covariance)
样本协方差,要求 n>=2 。
lmmc_status_t lmmc_vec_median(const lmmc_vec_t *x, lmmc_real_t *out)
计算向量中位数。
lmmc_status_t lmmc_dist_beta_quantile(lmmc_real_t p, lmmc_real_t alpha, lmmc_real_t beta, lmmc_real_t *out)
贝塔分布分位数函数。
lmmc_status_t lmmc_dist_t_pdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
t 分布概率密度函数。
lmmc_status_t lmmc_vec_correlation_sample(const lmmc_vec_t *x, const lmmc_vec_t *y, lmmc_real_t *out_correlation)
样本 Pearson 相关系数;使用同一缩放累计路径,要求 n>=2。
lmmc_status_t lmmc_dist_binomial_pmf(size_t k, size_t n, lmmc_real_t p, lmmc_real_t *out)
离散分布函数的公共参数约束。
lmmc_status_t lmmc_dist_f_pdf(lmmc_real_t x, lmmc_real_t df1, lmmc_real_t df2, lmmc_real_t *out)
F 分布概率密度函数。
lmmc_status_t
LMMC 库统一返回的状态码.
@ LMMC_STATUS_OK
@ LMMC_STATUS_NUMERICAL_FAILURE
@ LMMC_STATUS_INVALID_ARGUMENT
@ LMMC_STATUS_OUT_OF_RANGE
@ LMMC_STATUS_EMPTY_INPUT
lmmc_status_t(*) lmmc_std_dist3_fn(lmmc_real_t, lmmc_real_t, lmmc_real_t, lmmc_real_t *)
lmmc_status_t(*) lmmc_std_dist2_fn(lmmc_real_t, lmmc_real_t, lmmc_real_t *)
lmmc_status_t lmmc_std_stats_chi2_cdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_gamma_quantile(lmmc_real_t p, lmmc_real_t shape, lmmc_real_t scale, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_t_pdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_cov(const lmmc_real_t *x, const lmmc_real_t *y, size_t count, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_f_cdf(lmmc_real_t x, lmmc_real_t df1, lmmc_real_t df2, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_quantile(const lmmc_real_t *values, size_t count, lmmc_real_t q, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_gamma_pdf(lmmc_real_t x, lmmc_real_t shape, lmmc_real_t scale, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_normal_quantile(lmmc_real_t p, lmmc_real_t mean, lmmc_real_t stddev, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_f_quantile(lmmc_real_t p, lmmc_real_t df1, lmmc_real_t df2, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_chi2_pdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_beta_pdf(lmmc_real_t x, lmmc_real_t alpha, lmmc_real_t beta, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_binomial_pmf(size_t k, size_t n, lmmc_real_t p, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_normal_pdf(lmmc_real_t x, lmmc_real_t mean, lmmc_real_t stddev, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_f_pdf(lmmc_real_t x, lmmc_real_t df1, lmmc_real_t df2, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_var(const lmmc_real_t *values, size_t count, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_poisson_pmf(size_t k, lmmc_real_t lambda, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_corr(const lmmc_real_t *x, const lmmc_real_t *y, size_t count, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_t_quantile(lmmc_real_t p, lmmc_real_t df, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_mean(const lmmc_real_t *values, size_t count, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_beta_cdf(lmmc_real_t x, lmmc_real_t alpha, lmmc_real_t beta, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_binomial_cdf(size_t k, size_t n, lmmc_real_t p, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_beta_quantile(lmmc_real_t p, lmmc_real_t alpha, lmmc_real_t beta, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_poisson_cdf(size_t k, lmmc_real_t lambda, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_median(const lmmc_real_t *values, size_t count, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_normal_cdf(lmmc_real_t x, lmmc_real_t mean, lmmc_real_t stddev, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_gamma_cdf(lmmc_real_t x, lmmc_real_t shape, lmmc_real_t scale, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_t_cdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_chi2_quantile(lmmc_real_t p, lmmc_real_t df, lmmc_real_t *out)
lmmc_status_t lmmc_std_stats_std(const lmmc_real_t *values, size_t count, lmmc_real_t *out)