Skip to content

Commit 4c5c0ba

Browse files
committed
fix segment-based RC regression score computation
Signed-off-by: Arthur Koucher <arthurkoucher@precisioninno.com>
1 parent 2f6e9c9 commit 4c5c0ba

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

flow/util/correlateRC.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@
1818

1919
LAYER_HEADER_RE = re.compile("^([^\\(]+)\\(([^\\)]+)\\)$")
2020

21+
# Helper functions
22+
# =============================================================================
23+
24+
25+
# sklearn's default baseline model for scoring the fit i.e., measuring R² is
26+
# "predict the mean" which is not the proper model for our regressions since
27+
# both R and C are through-origin fits - the R² computation doesn't behave
28+
# well for var(y) ≈ 0 - so we compute R² manually with a "predict zero"
29+
# baseline model.
30+
def compute_through_origin_fit_score(model, inputs, observed):
31+
sum_squared_observed = (observed**2).sum()
32+
if sum_squared_observed == 0:
33+
raise ValueError("Cannot score fit: all observed values are zero.")
34+
return 1.0 - ((observed - model.predict(inputs)) ** 2).sum() / sum_squared_observed
35+
36+
2137
# Parse and validate arguments
2238
# =============================================================================
2339

@@ -410,8 +426,8 @@ def generic_rc_fit(type_sieve):
410426
resistances,
411427
capacitances_ff,
412428
) in layer_models.items():
413-
r_sq_res = res_model.score(lengths, resistances)
414-
r_sq_cap = cap_model.score(lengths, capacitances_ff)
429+
r_sq_res = compute_through_origin_fit_score(res_model, lengths, resistances)
430+
r_sq_cap = compute_through_origin_fit_score(cap_model, lengths, capacitances_ff)
415431
print("{:<12s} | {:>8.4f} | {:>8.4f}".format(layer_name, r_sq_res, r_sq_cap))
416432
print("-" * 34)
417433
print("")

0 commit comments

Comments
 (0)