def pv(rate, nper, pmt, fv):
    """Calculate the present value of an asset.

    Parameters
    ----------
    rate : float
        Interest rate per period.
    nper : int
        Number of payment periods.
    pmt : float
        Constant payment made each period.
    fv : float
        Future value, i.e., balance after the last
        payment is made.

    Returns
    -------
    float
    """
    if rate == 0:
        return -(fv + pmt*nper)
    else:
        tmp = (1 + rate)**nper
        return -(fv + pmt*(tmp - 1) / rate) / tmp