You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-commits@axis.apache.org by ro...@apache.org on 2026/09/23 00:06:31 UTC
(axis-axis2-c-core) 06/07: Check the diagonal for finiteness in portfolioVariance
This is an automated email from the ASF dual-hosted git repository.
robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-c-core.git
commit 1393ea33c74a9f6cc7ee1c338df4ddb47d2c36b8
Author: Robert Lazarski <ro...@gmail.com>
AuthorDate: Tue Sep 22 13:26:53 2026 -1000
Check the diagonal for finiteness in portfolioVariance
The symmetry loop skipped i == j, so a NaN or -Inf on the diagonal
bypassed the PSD guard and returned SUCCESS. The loop now includes the
diagonal, and a non-finite w'Sigma*w (NaN weight, overflow) is refused.
Co-Authored-By: Claude Opus 5.5 (1M context) <no...@anthropic.com>
---
.../src/financial_benchmark_service.c | 28 ++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/samples/user_guide/financial-benchmark-service/src/financial_benchmark_service.c b/samples/user_guide/financial-benchmark-service/src/financial_benchmark_service.c
index 322ffd9b3..c79329c41 100644
--- a/samples/user_guide/financial-benchmark-service/src/financial_benchmark_service.c
+++ b/samples/user_guide/financial-benchmark-service/src/financial_benchmark_service.c
@@ -560,15 +560,22 @@ finbench_calculate_portfolio_variance(
/* Symmetry, same rule and tolerance as monteCarlo and composeCovariance.
* w'Σw over an asymmetric matrix silently uses the mean of each
* off-diagonal pair, so the caller gets a number that agrees with no
- * symmetrized version of what they sent. Refuse with the index. */
+ * symmetrized version of what they sent. Refuse with the index.
+ * j <= i so the diagonal is finiteness-checked too: a NaN or -Inf
+ * there would otherwise slip past the PSD guard below (comparisons
+ * against NaN, and -Inf < -Inf, are all false). */
for (i = 0; i < n; i++) {
- for (j = 0; j < i; j++) {
+ for (j = 0; j <= i; j++) {
double a = request->covariance_matrix[i * n + j];
double b = request->covariance_matrix[j * n + i];
if (!isfinite(a) || !isfinite(b)) {
char err_buf[160];
- snprintf(err_buf, sizeof(err_buf),
- "covariance_matrix[%d][%d] or [%d][%d] is not finite.", i, j, j, i);
+ if (i == j)
+ snprintf(err_buf, sizeof(err_buf),
+ "covariance_matrix[%d][%d] is not finite.", i, i);
+ else
+ snprintf(err_buf, sizeof(err_buf),
+ "covariance_matrix[%d][%d] or [%d][%d] is not finite.", i, j, j, i);
response->status = axutil_strdup(env, FINBENCH_STATUS_FAILED);
response->error_message = axutil_strdup(env, err_buf);
AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "FinBench portfolioVariance: %s", err_buf);
@@ -618,6 +625,19 @@ finbench_calculate_portfolio_variance(
* that left the struct zeroed gets the same default, never the old 252. */
int npy = (request->n_periods_per_year > 0) ? request->n_periods_per_year : 1;
+ /* Entries are finite by now, but a NaN weight or entries large enough
+ * to overflow the sum still give a non-finite result; refuse it rather
+ * than let it past the sign test below. */
+ if (!isfinite(variance)) {
+ response->status = axutil_strdup(env, FINBENCH_STATUS_FAILED);
+ response->error_message = axutil_strdup(env,
+ "w'Sigma*w is not finite: check weights and covariance_matrix "
+ "for non-finite or overflowing values.");
+ AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI,
+ "FinBench portfolioVariance: w'Sigma*w is not finite");
+ return response;
+ }
+
/*
* A negative w'Σw means the matrix is not positive semi-definite for
* these weights — it is not a covariance matrix. Only a rounding-scale