core.test_math
Test Square Root Precision
Verifies that the square root calculation maintains high precision (50 decimal places) required for financial calculations.
Mathematical Verification
We verify the property: $$(\sqrt{x})^2 \approx x$$
Inputs:
- Value ($x$):
3050(Decimal)
Expected Behavior: The squared result should differ from the original input by less than $10^{-40}$.
$$|(\sqrt{3050})^2 - 3050| < 10^{-40}$$
Calculation:
- $y = \text{safe_sqrt}(3050)$
- $\text{diff} = |y^2 - 3050|$
This ensures that we are not suffering from floating-point errors (where error $\approx 10^{-15}$).
EXCEL REPRODUCTION
1. Inputs (Cell B1)
| Cell | Value |
|---|---|
| B1 | 3050 |
2. Calculation (Cells B2:B3)
| Cell | Name | Formula | Expectation |
|---|---|---|---|
| B2 | Sqrt | =SQRT(B1) |
~55.2268 |
| B3 | Squared | =B2^2 |
3050 |
3. Difference (Cell B4)
| Cell | Name | Formula | Note |
|---|---|---|---|
| B4 | Diff | =ABS(B3-B1)| Should be ~0 |
Test Standard Normal CDF
Verifies the Cumulative Distribution Function $N(x)$ for the standard normal distribution.
Formula
$$N(x) = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^{x} e^{-t^2/2} dt$$
We test against known values and properties.
Test Case 1: Symmetry
Property: $N(x) + N(-x) = 1$
Input: $x = 1.5$
Verification: $|N(1.5) + N(-1.5) - 1| < 10^{-20}$
Test Case 2: Zero Point
Property: $N(0) = 0.5$
Verification: $|N(0) - 0.5| < 10^{-7}$
Test Case 3: 1.96 (95% Confidence Interval)
Standard statistical value for $Z=1.96$.
Input: $1.96$
Expected Output: $\approx 0.9750021048$
Verification: Error $< 10^{-6}$
Test Case 4: -1.96
Lower tail probability.
Input: $-1.96$
Expected Output: $\approx 0.0249978951$
Verification: Error $< 10^{-6}$
EXCEL REPRODUCTION
1. Test Table (Columns A, B)
| Row | Z Value (A) | CDF Formula (B) | Expected (C) |
|---|---|---|---|
| 1 | 1.5 | =NORM.S.DIST(A1,TRUE) |
~0.9332 |
| 2 | -1.5 | =NORM.S.DIST(A2,TRUE) |
~0.0668 |
| 3 | 0 | =NORM.S.DIST(A3,TRUE) |
0.5 |
| 4 | 1.96 | =NORM.S.DIST(A4,TRUE) |
~0.9750 |
| 5 | -1.96 | =NORM.S.DIST(A5,TRUE) |
~0.0250 |
2. Symmetry Check (Row 6)
| Cell | Name | Formula | Expectation |
|---|---|---|---|
| B6 | Sum(1.5) | =B1+B2| 1.0 |
Test that safe_sqrt enforces a minimum precision of 50, even if the global context is set lower. This is critical for maintaining accuracy in financial calculations.
Test safe_sqrt with zero as an input. Expected behavior: The function should return Decimal('0') without any precision issues or errors.
Test that safe_sqrt raises a ValueError when given a negative input, as the square root of a negative number is undefined in real numbers.
Test safe_ln with a standard positive input.
Test that safe_ln raises a ValueError when given zero, as ln(0) is undefined.
Test that safe_ln raises a ValueError when given a negative number.