### Secant Method for XIRR Calculation (Fallback) Source: https://github.com/peliot/xirr-and-xnpv/blob/master/README.rst Provides a fallback implementation of the secant method for calculating XIRR when SciPy is not available. This method approximates the IRR but may not fail gracefully if no answer is found. It requires a list of cash flows and their corresponding dates. ```python from typing import List, Union from datetime import date def secant_method(f, x1, x2, tol=1e-8, max_iter=100): """ Find a root of the function f using the secant method. Arguments: f -- the function to find the root of x1, x2 -- initial guesses tol -- tolerance for convergence max_iter -- maximum number of iterations Returns: the approximate root """ for _ in range(max_iter): if abs(x2 - x1) < tol: return x2 f1 = f(x1) f2 = f(x2) if abs(f2) < tol: return x2 x = x2 - f2 * (x2 - x1) / (f2 - f1) x1, x2 = x2, x raise RuntimeError("Secant method did not converge") def xirr_secant(cashflows: List[tuple[date, Union[int, float]]], guess: float = 0.1) -> float: """ Calculate the Internal Rate of Return for a series of cash flows occurring at irregular intervals using the secant method. Arguments: cashflows -- a list of tuples, where each tuple is (date, amount) guess -- initial guess for the rate Returns: the Internal Rate of Return """ if not cashflows: raise ValueError("cashflows cannot be empty") # Use a second guess slightly different from the first guess2 = guess * 1.01 if guess != 0 else 0.01 return secant_method(lambda r: xnpv(r, cashflows), guess, guess2) ``` -------------------------------- ### Calculate XNPV using Python Source: https://github.com/peliot/xirr-and-xnpv/blob/master/README.rst Calculates the Net Present Value (NPV) for a series of cash flows occurring at irregular intervals. Requires a discount rate, a list of cash flows, and their corresponding dates. This implementation relies on the SciPy library. ```python from typing import List, Union from datetime import date def xnpv(rate: float, cashflows: List[tuple[date, Union[int, float]]]) -> float: ``` -------------------------------- ### Calculate XIRR using Python with SciPy Source: https://github.com/peliot/xirr-and-xnpv/blob/master/README.rst Calculates the Internal Rate of Return (IRR) for a series of cash flows occurring at irregular intervals. This function uses the numerical solver from the SciPy library. It requires a list of cash flows and their corresponding dates. The accuracy depends on the solver's convergence. ```python from typing import List, Union from datetime import date from scipy.optimize import newton def xirr_scipy(cashflows: List[tuple[date, Union[int, float]]], guess: float = 0.1) -> float: ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.