我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用numpy.npv()。
def calc_excel_npv(rate, values): orig_npv = np.npv(rate, values) excel_npv = orig_npv/(1+rate) return excel_npv
def mirr(values, finance_rate, reinvest_rate): """ Modified internal rate of return. Parameters ---------- values : array_like Cash flows (must contain at least one positive and one negative value) or nan is returned. The first value is considered a sunk cost at time zero. finance_rate : scalar Interest rate paid on the cash flows reinvest_rate : scalar Interest rate received on the cash flows upon reinvestment Returns ------- out : float Modified internal rate of return """ values = np.asarray(values, dtype=np.double) n = values.size pos = values > 0 neg = values < 0 if not (pos.any() and neg.any()): return np.nan numer = np.abs(npv(reinvest_rate, values*pos)) denom = np.abs(npv(finance_rate, values*neg)) return (numer/denom)**(1.0/(n - 1))*(1 + reinvest_rate) - 1
def test_npv(self): assert_almost_equal( np.npv(0.05, [-15000, 1500, 2500, 3500, 4500, 6000]), 122.89, 2)
def calc_npv(r, ubi, inputs, n): x = np.zeros((ubi['Years Post Transfer'], n)) y = np.zeros((ubi['Years Post Transfer'], n)) # iterate through years of benefits for j in range(1, ubi['Years Post Transfer'] + 1): # sum benefits during program if(j < r): x[j - 1] += ubi['Expected baseline per capita consumption (nominal USD)']* \ np.power((1.0 + inputs['UBI']['Expected annual consumption increase (without the UBI program)']), float(j))* \ inputs['UBI']['Work participation adjustment'] + \ ubi['Annual quantity of transfer money used for immediate consumtion (pre-discounting)'] # benefits after program else: x[j - 1] += ubi['Expected baseline per capita consumption (nominal USD)']* \ np.power((1.0 + inputs['UBI']['Expected annual consumption increase (without the UBI program)']), float(j)) # investments calculations for k in range(n): if(j < r + inputs['UBI']['Duration of investment benefits (in years) - UBI'][k]): x[j - 1][k] += ubi['Annual return for each year of transfer investments (pre-discounting)'][k]* \ np.min([j, inputs['UBI']['Duration of investment benefits (in years) - UBI'][k], \ r, (inputs['UBI']['Duration of investment benefits (in years) - UBI'][k] + r - j)]) if(j > r): x[j - 1][k] += ubi['Value eventually returned from one years investment (pre-discounting)'][k] # log transform and subtact baseline y[j - 1] = np.log(x[j - 1]) y[j - 1] -= np.log(ubi['Expected baseline per capita consumption (nominal USD)']* \ np.power((1.0 + inputs['UBI']['Expected annual consumption increase (without the UBI program)']), float(j))) # npv on yearly data z = np.zeros(n) for i in range(n): z[i] = np.npv(inputs['Shared']['Discount rate'][i], y[:, i]) return z
def npv(rate, values): """ Returns the NPV (Net Present Value) of a cash flow series. Parameters ---------- rate : scalar The discount rate. values : array_like, shape(M, ) The values of the time series of cash flows. The (fixed) time interval between cash flow "events" must be the same as that for which `rate` is given (i.e., if `rate` is per year, then precisely a year is understood to elapse between each cash flow event). By convention, investments or "deposits" are negative, income or "withdrawals" are positive; `values` must begin with the initial investment, thus `values[0]` will typically be negative. Returns ------- out : float The NPV of the input cash flow series `values` at the discount `rate`. Notes ----- Returns the result of: [G]_ .. math :: \\sum_{t=0}^{M-1}{\\frac{values_t}{(1+rate)^{t}}} References ---------- .. [G] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed., Addison-Wesley, 2003, pg. 346. Examples -------- >>> np.npv(0.281,[-100, 39, 59, 55, 20]) -0.0084785916384548798 (Compare with the Example given for numpy.lib.financial.irr) """ values = np.asarray(values) return (values / (1+rate)**np.arange(0, len(values))).sum(axis=0)