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/20 15:58:09 UTC

(axis-axis2-c-core) 01/07: Bound Monte Carlo periods and drop the redundant covariance zero-fill

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 a85f9c872f282d0333a1829ce0faf2668b4470d0
Author: Robert Lazarski <ro...@gmail.com>
AuthorDate: Mon Sep 7 08:49:16 2026 -1000

    Bound Monte Carlo periods and drop the redundant covariance zero-fill
    
    Cap n_periods independently of the work budget, so a small n_simulations with
    a huge n_periods can no longer accumulate the GBM value past DBL_MAX (Inf ->
    NaN response). Make an extreme path terminal rather than capping and then
    multiplying again on the next period. Remove the covariance zero-fill: the
    shape validation already rejects any matrix that would leave a cell unwritten.
    
    Co-Authored-By: Claude <no...@anthropic.com>
---
 .../src/financial_benchmark_service.c              | 28 ++++++++++++++++------
 .../src/financial_benchmark_service.h              |  3 +++
 2 files changed, 24 insertions(+), 7 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 49bb05ea2..c6145ba2e 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
@@ -237,11 +237,10 @@ finbench_portfolio_variance_request_create_from_json(
         json_object_put(json_obj);
         return NULL;
     }
-    /* Zero first: any cell a malformed matrix leaves unwritten must read as 0,
-     * not as stale heap contents (which would leak into the numeric result). */
-    memset(request->covariance_matrix, 0, matrix_size * sizeof(double));
 
-    /* Extract covariance_matrix - supports both flat and 2D array formats */
+    /* Extract covariance_matrix - supports both flat and 2D array formats.
+     * The shape validation below rejects any matrix that would leave a cell
+     * unwritten, so no separate zero-fill is needed. */
     if (json_object_object_get_ex(json_obj, "covariance_matrix", &array_obj) &&
         json_object_is_type(array_obj, json_type_array)) {
         int outer_len = json_object_array_length(array_obj);
@@ -969,6 +968,14 @@ finbench_run_monte_carlo(
             "n_periods must be >= 1 (at least one time step is required).");
         return response;
     }
+    if (request->n_periods > FINBENCH_MAX_PERIODS) {
+        char pbuf[128];
+        snprintf(pbuf, sizeof(pbuf),
+            "n_periods exceeds the maximum (%d).", FINBENCH_MAX_PERIODS);
+        response->status = axutil_strdup(env, FINBENCH_STATUS_FAILED);
+        response->error_message = axutil_strdup(env, pbuf);
+        return response;
+    }
     if ((int64_t)request->n_simulations * request->n_periods > FINBENCH_MAX_WORK) {
         response->status = axutil_strdup(env, FINBENCH_STATUS_FAILED);
         response->error_message = axutil_strdup(env,
@@ -1102,10 +1109,17 @@ finbench_run_monte_carlo(
              * Extreme GBM shocks with high volatility can produce
              * exponents that overflow to Inf, corrupting all downstream
              * statistics. Cap to a safe maximum. */
+            /* Capping value alone is self-defeating: a later period would
+             * multiply the cap and overflow to +Inf, which propagates NaN into
+             * the whole response. Treat an extreme path as terminal instead. */
             if (exponent > 709.0) {
-                value = 1e308; /* Near DBL_MAX — treat as extreme outcome */
-            } else {
-                value *= exp(exponent);
+                value = 1e308;   /* extreme outcome */
+                break;
+            }
+            value *= exp(exponent);
+            if (!isfinite(value)) {
+                value = 1e308;
+                break;
             }
 
             /* Track drawdown */
diff --git a/samples/user_guide/financial-benchmark-service/src/financial_benchmark_service.h b/samples/user_guide/financial-benchmark-service/src/financial_benchmark_service.h
index af6ace226..2a482365f 100644
--- a/samples/user_guide/financial-benchmark-service/src/financial_benchmark_service.h
+++ b/samples/user_guide/financial-benchmark-service/src/financial_benchmark_service.h
@@ -61,6 +61,9 @@ extern "C"
 /* Cap on total Monte Carlo work (n_simulations * n_periods): bounds worst-case
  * CPU so a large n_periods cannot hang a worker thread. */
 #define FINBENCH_MAX_WORK           1000000000L
+/* Independent cap on Monte Carlo time steps: bounds numerical growth so the
+ * simulated value cannot accumulate past DBL_MAX (100000 daily steps ~ 400y). */
+#define FINBENCH_MAX_PERIODS        100000
 
 /** Maximum number of percentile levels accepted in a monteCarlo request */
 #define FINBENCH_MAX_PERCENTILES    8