25 for (i = 1; i < count; ++i) {
30 if (isfinite(delta)) {
31 mean = fma(delta, 1.0 / sample_count, mean);
33 mean = mean * ((sample_count - 1.0) / sample_count) +
59 for (i = 0; i < count; ++i) {
62 scale = fmax(scale, fabs(value));
75 for (i = 1; i < count; ++i) {
80 column->
mean = fma(delta, 1.0 / sample_count, column->
mean);
82 m2 = fma(delta, value - column->
mean, m2);
106 for (i = 0; i < count; ++i) {
108 (scalbn(x[i * x_stride], -column_x->
exponent) - column_x->
origin) -
111 (scalbn(y[i * y_stride], -column_y->
exponent) - column_y->
origin) -
113 sum = fma(delta_x, delta_y, sum);
116 covariance = scalbn(sum / denominator,
117 column_x->exponent + column_y->exponent);
119 *out_covariance = covariance;
129 status = lmmc_validate_vec(x);
131 status = lmmc_scaled_column(x->
data, x->
size, 1, &column, &m2);
137 return lmmc_finalize_nonnegative(variance, out_variance);
150 status = lmmc_validate_vec(x);
152 status = lmmc_scaled_column(x->
data, x->
size, 1, &column, &m2);
160 *out_stddev = stddev;
177 status = lmmc_scaled_column(x->
data, x->
size, 1, &column_x, NULL);
179 status = lmmc_scaled_column(y->
data, y->
size, 1, &column_y, NULL);
183 return lmmc_covariance_scaled(
193 64.0 * DBL_EPSILON + 8.0 * (
lmmc_real_t)count * DBL_EPSILON * DBL_EPSILON;
194 if (!(m2x > 0.0) || !(m2y > 0.0) ||
197 correlation = (c / sqrt(m2x)) / sqrt(m2y);
199 if (correlation > 1.0 && correlation <= 1.0 + rounding_bound) {
202 if (correlation < -1.0 && correlation >= -1.0 - rounding_bound) {
207 *out_correlation = correlation;
221 lmmc_real_t sx = 0.0, sy = 0.0, qx = 0.0, qy = 0.0, c = 0.0;
223 lmmc_real_t qx_error = 0.0, qy_error = 0.0, c_error = 0.0;
224 int exponent_x, exponent_y;
226 lmmc_status_t status = lmmc_scaled_column(x, count, x_stride, &column_x, NULL);
228 status = lmmc_scaled_column(y, count, y_stride, &column_y, NULL);
231 for (i = 0; i < count; ++i) {
236 spread_x = fmax(spread_x, fabs(dx));
237 spread_y = fmax(spread_y, fabs(dy));
240 frexp(spread_x, &exponent_x);
241 frexp(spread_y, &exponent_y);
247 for (i = 0; i < count; ++i) {
254 const lmmc_real_t xx = dx * dx, yy = dy * dy, xy = dx * dy;
255 lmmc_compensated_add(dx, &sx, &sx_error);
256 lmmc_compensated_add(dy, &sy, &sy_error);
257 lmmc_compensated_add(xx, &qx, &qx_error);
258 lmmc_compensated_add(fma(dx, dx, -xx), &qx, &qx_error);
259 lmmc_compensated_add(yy, &qy, &qy_error);
260 lmmc_compensated_add(fma(dy, dy, -yy), &qy, &qy_error);
261 lmmc_compensated_add(xy, &c, &c_error);
262 lmmc_compensated_add(fma(dx, dy, -xy), &c, &c_error);
267 qx = fma(-sx, sx / (
lmmc_real_t)count, qx + qx_error);
268 qy = fma(-sy, sy / (
lmmc_real_t)count, qy + qy_error);
269 c = fma(-sx, sy / (
lmmc_real_t)count, c + c_error);
270 return lmmc_finalize_correlation(c, qx, qy, count, out_correlation);
283 status_x = lmmc_validate_vec(x);
284 status_y = lmmc_validate_vec(y);
289 return lmmc_correlation_scaled(
299 for (column = 0; column < x->
cols; ++column) {
300 status = lmmc_mean_strided(
311 for (col_i = 0; col_i < x->
cols; ++col_i) {
312 status = lmmc_scaled_column(
339 if (!lmmc_safe_mul_size(x->
cols,
sizeof(*columns), &bytes)) { return LMMC_STATUS_INVALID_ARGUMENT; }
343 status = lmmc_scale_matrix_columns(x, columns);
348 for (col_i = 0; col_i < x->
cols; ++col_i) {
349 for (col_j = col_i; col_j < x->
cols; ++col_j) {
351 status = lmmc_covariance_scaled(
354 x->
rows, denominator, &covariance);
358 out_matrix->
data[col_i * out_matrix->
stride + col_j] = covariance;
359 out_matrix->
data[col_j * out_matrix->
stride + col_i] = covariance;
382 for (col_i = 0; col_i < x->
cols; ++col_i) {
383 for (col_j = col_i; col_j < x->
cols; ++col_j) {
385 status = lmmc_correlation_scaled(
388 x->
rows, &correlation);
390 if (col_i == col_j) {
393 out_matrix->
data[col_i * out_matrix->
stride + col_j] =
395 out_matrix->
data[col_j * out_matrix->
stride + col_i] =
405 status = lmmc_validate_vec(x);
407 return lmmc_mean_strided(x->
data, x->
size, 1, out_mean);
411 return lmmc_vec_variance_common(x, 0, out_variance);
415 return lmmc_vec_variance_common(x, 1, out_variance);
419 return lmmc_vec_stddev_common(x, 0, out_stddev);
423 return lmmc_vec_stddev_common(x, 1, out_stddev);
427 return lmmc_vec_covariance_common(x, y, 0, out_covariance);
431 return lmmc_vec_covariance_common(x, y, 1, out_covariance);
435 return lmmc_vec_correlation_common(x, y, 0, out_correlation);
439 return lmmc_vec_correlation_common(x, y, 1, out_correlation);
452 return lmmc_mat_column_means_to_buffer(x, out_means->
data);
456 return lmmc_mat_covariance_common(x, 0, out_covariance);
460 return lmmc_mat_covariance_common(x, 1, out_covariance);
464 return lmmc_mat_correlation_common(x, 0, out_correlation);
468 return lmmc_mat_correlation_common(x, 1, out_correlation);
LMMC binary real type and arithmetic spelling.
double lmmc_real_t
Public and internal LMMC real type.
void * lmmc_memory_alloc(size_t size)
void lmmc_memory_free(void *pointer)
Private recoverable allocator for persistent LMMC ownership.
lmmc_status_t lmmc_vec_variance_sample(const lmmc_vec_t *x, lmmc_real_t *out_variance)
计算样本方差 。
lmmc_status_t lmmc_vec_mean(const lmmc_vec_t *x, lmmc_real_t *out_mean)
计算向量样本均值 。
lmmc_status_t lmmc_vec_correlation_population(const lmmc_vec_t *x, const lmmc_vec_t *y, lmmc_real_t *out_correlation)
总体 Pearson 相关系数。
lmmc_status_t lmmc_mat_column_mean(const lmmc_mat_t *x, lmmc_vec_t *out_means)
计算矩阵各列均值,输出长度为 x->cols 的向量。
lmmc_status_t lmmc_vec_stddev_population(const lmmc_vec_t *x, lmmc_real_t *out_stddev)
总体标准差。 缩放计算支持方差超出 binary64 范围的情况。 输入非有限或标准差结果溢出时返回 LMMC_STATUS_NUMERICAL_FAILURE。
lmmc_status_t lmmc_mat_covariance_population(const lmmc_mat_t *x, lmmc_mat_t *out_covariance)
计算总体协方差矩阵(cols × cols)。
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_vec_covariance_population(const lmmc_vec_t *x, const lmmc_vec_t *y, lmmc_real_t *out_covariance)
总体协方差 。
lmmc_status_t lmmc_mat_correlation_population(const lmmc_mat_t *x, lmmc_mat_t *out_correlation)
计算总体 Pearson 相关矩阵。
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_correlation_sample(const lmmc_vec_t *x, const lmmc_vec_t *y, lmmc_real_t *out_correlation)
样本 Pearson 相关系数;使用同一缩放累计路径,要求 n>=2。
lmmc_status_t lmmc_mat_correlation_sample(const lmmc_mat_t *x, lmmc_mat_t *out_correlation)
使用同一缩放路径计算样本 Pearson 相关矩阵,要求行数 >= 2。
lmmc_status_t lmmc_mat_covariance_sample(const lmmc_mat_t *x, lmmc_mat_t *out_covariance)
计算样本协方差矩阵(cols × cols),要求行数 >= 2 。
lmmc_status_t lmmc_vec_variance_population(const lmmc_vec_t *x, lmmc_real_t *out_variance)
总体方差 。
lmmc_status_t
LMMC 库统一返回的状态码.
@ LMMC_STATUS_NUMERICAL_FAILURE
@ LMMC_STATUS_DIMENSION_MISMATCH
@ LMMC_STATUS_ALLOCATION_FAILED
@ LMMC_STATUS_INVALID_ARGUMENT