From 3984ed06a7060c5c005fe062d5570a1b38f1b926 Mon Sep 17 00:00:00 2001 From: bitromortac Date: Fri, 22 Dec 2023 13:33:07 +0100 Subject: [PATCH] routing: add test for small scale This test demonstrates that the current bimodal model leads to numerical inaccuracies in a certain regime of successes and failures. --- routing/probability_bimodal_test.go | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/routing/probability_bimodal_test.go b/routing/probability_bimodal_test.go index 30fac41d7..f9fe0fe4e 100644 --- a/routing/probability_bimodal_test.go +++ b/routing/probability_bimodal_test.go @@ -240,6 +240,39 @@ func TestSuccessProbability(t *testing.T) { }) } +// TestSmallScale tests that the probability formula works with small scale +// values. +func TestSmallScale(t *testing.T) { + var ( + // We use the smallest possible scale value together with a + // large capacity. This is an extreme form of a bimodal + // distribution. + scale lnwire.MilliSatoshi = 1 + capacity lnwire.MilliSatoshi = 7e+09 + + // Success and failure amounts are chosen such that the expected + // balance must be somewhere in the middle of the channel, a + // value not expected when dealing with a bimodal distribution. + // In this case, the bimodal model fails to give good forecasts + // due to the numerics of the exponential functions, which get + // evaluated to exact zero floats. + successAmount lnwire.MilliSatoshi = 1.0e+09 + failAmount lnwire.MilliSatoshi = 4.0e+09 + ) + + estimator := BimodalEstimator{ + BimodalConfig: BimodalConfig{BimodalScaleMsat: scale}, + } + + // An amount that's close to the success amount should have a very high + // probability. + amtCloseSuccess := successAmount + 1 + _, err := estimator.probabilityFormula( + capacity, successAmount, failAmount, amtCloseSuccess, + ) + require.ErrorContains(t, err, "normalization factor is zero") +} + // TestIntegral tests certain limits of the probability distribution integral. func TestIntegral(t *testing.T) { t.Parallel()