def testNanCumReduction(self): raw = np.random.randint(5, size=(8, 8, 8)) raw[:2, 2:4, 4:6] = np.nan arr = tensor(raw, chunk_size=3) res1 = self.executor.execute_tensor(nancumsum(arr, axis=1), concat=True) res2 = self.executor.execute_tensor(nancumprod(arr, axis=1), concat=True) expected1 = np.nancumsum(raw, axis=1) expected2 = np.nancumprod(raw, axis=1) np.testing.assert_array_equal(res1[0], expected1) np.testing.assert_array_equal(res2[0], expected2) raw = sps.random(8, 8, density=.1, format='lil') raw[:2, 2:4] = np.nan arr = tensor(raw, chunk_size=3) res1 = self.executor.execute_tensor(nancumsum(arr, axis=1), concat=True)[0] res2 = self.executor.execute_tensor(nancumprod(arr, axis=1), concat=True)[0] expected1 = np.nancumsum(raw.A, axis=1) expected2 = np.nancumprod(raw.A, axis=1) self.assertTrue(np.allclose(res1, expected1)) self.assertTrue(np.allclose(res2, expected2))
def test_nancumprod(array): np_a = numpy.array(array) dpnp_a = dpnp.array(np_a) result = dpnp.nancumprod(dpnp_a) expected = numpy.nancumprod(np_a) numpy.testing.assert_array_equal(expected, result)
def test_nancumprod(array): a = numpy.array(array) ia = inp.array(a) result = inp.nancumprod(ia) expected = numpy.nancumprod(a) numpy.testing.assert_array_equal(expected, result)
def cumprod_nb(a): """Cumulative product.""" b = np.empty_like(a, dtype=a.dtype) for j in range(a.shape[1]): b[:, j] = np.nancumprod(a[:, j]) b[np.isnan(a[:, j]), j] = np.nan return b
def nancumproduct(a, window): prod = np.empty_like(a) prod[:window - 1, :] = np.nan prod[window - 1:, :] = np.squeeze(np.nancumprod(rolling_window( a, (window, a.shape[1])), axis=2), axis=1) return prod
def test_result_values(self): for axis in (-2, -1, 0, 1, None): tgt = np.cumprod(_ndat_ones, axis=axis) res = np.nancumprod(_ndat, axis=axis) assert_almost_equal(res, tgt) tgt = np.cumsum(_ndat_zeros,axis=axis) res = np.nancumsum(_ndat, axis=axis) assert_almost_equal(res, tgt)
def test_result_values(self): for axis in (-2, -1, 0, 1, None): tgt = np.cumprod(_ndat_ones, axis=axis) res = np.nancumprod(_ndat, axis=axis) assert_almost_equal(res, tgt) tgt = np.cumsum(_ndat_zeros, axis=axis) res = np.nancumsum(_ndat, axis=axis) assert_almost_equal(res, tgt)
def maximumDrawdown(rets: np.array): cum_rets = np.nancumprod(rets + 1) mdd = 0 peak = cum_rets[0] for ret in cum_rets: if ret > peak: peak = ret dd = (peak - ret) / peak if dd > mdd: mdd = dd return mdd
def test_nan_cum_reduction(setup): raw = np.random.randint(5, size=(8, 8, 8)).astype(float) raw[:2, 2:4, 4:6] = np.nan arr = tensor(raw, chunk_size=6) res1 = nancumsum(arr, axis=1).execute().fetch() res2 = nancumprod(arr, axis=1).execute().fetch() expected1 = np.nancumsum(raw, axis=1) expected2 = np.nancumprod(raw, axis=1) np.testing.assert_array_equal(res1, expected1) np.testing.assert_array_equal(res2, expected2) raw = sps.random(8, 8, density=.1, format='lil') raw[:2, 2:4] = np.nan arr = tensor(raw, chunk_size=6) res1 = nancumsum(arr, axis=1).execute().fetch() res2 = nancumprod(arr, axis=1).execute().fetch() expected1 = np.nancumsum(raw.A, axis=1) expected2 = np.nancumprod(raw.A, axis=1) assert np.allclose(res1, expected1) is True assert np.allclose(res2, expected2) is True
def stack_stats(arrs, nodata=None): """All statistics for arrs : arrs - either a list, tuple of arrays or an array with ndim=3 : nodata - nodata value, numeric or np.nan (will upscale integers) """ arrs = check_stack(arrs) a_m = mask_stack(arrs, nodata=nodata) nan_sum = np.nansum(a_m, axis=0) nan_cumsum = np.nancumsum(a_m, axis=0) nan_prod = np.nanprod(a_m, axis=0) nan_cumprod = np.nancumprod(a_m, axis=0) nan_min = np.nanmin(a_m, axis=0) nan_mean = np.nanmean(a_m, axis=0) nan_median = np.nanmean(a_m, axis=0) nan_max = np.nanmax(a_m, axis=0) nan_std = np.nanstd(a_m, axis=0) nan_var = np.nanvar(a_m, axis=0) stats = [nan_sum, nan_cumsum, nan_prod, nan_cumprod, nan_min, nan_mean, nan_median, nan_max, nan_std, nan_var] return stats
def test_nancumproduct_1(self): x = np.nancumprod(1) print(x) y = np.nancumprod([1]) print(y) z = np.nancumprod([1, np.nan]) print(z) a = np.array([[1, 2], [3, np.nan]]) b = np.nancumprod(a) print(b) c = np.nancumprod(a, axis=0) print(c) d = np.nancumprod(a, axis=1) print(d) return
def calculate(): df_whole = pd.DataFrame() conn = engine_read.connect() year = process_date.year month = process_date.month month_range = cld.monthrange(year, month)[1] time_to_fill = sf.Time(dt.datetime(year, month, month_range)) # year, month = time_to_fill.year, time_to_fill.month # month_range = time_to_fill.month_range sql_bm = sf.SQL.market_index(time_to_fill.today) # Get benchmark prices sql_pe = sf.SQL.pe_index(time_to_fill.today, freq="m") ###w->m bm = pd.read_sql(sql_bm, engine_read) bm["y1_treasury_rate"] = bm["y1_treasury_rate"].fillna(method="backfill") bm["y1_treasury_rate"] = bm["y1_treasury_rate"].apply(su.annually2monthly) bm["statistic_date"] = bm["statistic_date"].apply(su.date2tstp) pe = pd.read_sql(sql_pe, engine_read) pe["statistic_date"] = pe["statistic_date"].apply(su.date2tstp) conn.close() prices_bm = [ bm["hs300"].tolist(), bm["csi500"].tolist(), bm["sse50"].tolist(), bm["cbi"].tolist(), bm["nfi"] ] price_pe = pe["index_value"].tolist() r_tbond = bm["y1_treasury_rate"].tolist() t_bm = bm["statistic_date"].tolist() t_pe = pe["statistic_date"].tolist() intervals = table.intervals intervals5 = [1, 2, 3, 4, 5, 6, 10, 11] intervals6 = [2, 3, 4, 5, 6, 10, 11] result = [] conn = engine_read.connect() # Get Data date_s = sf.Time(process_date - dt.timedelta(process_date.day)) # Generate statistic_date sql_fids_updated = sf.SQL.ids_updated_sd(date_s.today, "om") ids_updated = tuple( x[0] for x in conn.execute(sql_fids_updated).fetchall()) # 找到当月净值有更新的基金 sql_o_updated = "SELECT DISTINCT fom.org_id FROM fund_org_mapping fom \ JOIN org_info oi ON fom.org_id = oi.org_id \ WHERE org_type_code = 1 AND oi.found_date <= '{0}' AND fund_id IN {1}".format( date_s.today - relativedelta(months=3), ids_updated) # 根据净值更新的基金确定需要计算的投顾 o_updated = tuple(x[0] for x in conn.execute(sql_o_updated).fetchall()) sql_fom = "SELECT fom.org_id, fom.fund_id, oi.found_date, oi.org_name FROM fund_org_mapping fom \ JOIN org_info oi ON fom.org_id = oi.org_id \ JOIN fund_info fi ON fom.fund_id = fi.fund_id \ WHERE fom.org_id IN {0} AND fom.org_type_code = 1 AND oi.found_date <= '{1}' AND fi.foundation_date <= '{2}'".format( o_updated, date_s.today - relativedelta(months=3), date_s.today - relativedelta(months=1)) fom = pd.read_sql(sql_fom, conn) # 根据需要计算的投顾找到其旗下管理的所有基金 fid_used = tuple(fom["fund_id"]) sql_fnd = sf.SQL.nav(fid_used) fnd = pd.read_sql(sql_fnd, conn) fnd = fnd.dropna() fnd.index = range(len(fnd)) data = fom.merge(fnd, how="inner", on="fund_id") data = data.sort_values(by=["org_id", "fund_id", "statistic_date"], ascending=[True, True, False]) t_mins = data.groupby(["org_id"])["statistic_date"].min().tolist() t_mins_tstp = [time.mktime(x.timetuple()) for x in t_mins] data["statistic_date"] = data["statistic_date"].apply( lambda x: time.mktime(x.timetuple())) data.index = range(len(data)) ids_o = data["org_id"].drop_duplicates().tolist() names_o = data.drop_duplicates(subset=["org_id"])["org_name"].tolist() idx4slice_o = su.idx4slice(data, "org_id") dfs = [ data[idx4slice_o[i]:idx4slice_o[i + 1]] if i != len(idx4slice_o) - 1 else data[idx4slice_o[i]:] for i in range(len(idx4slice_o) - 1) ] # Proprocess # 标准序列 t_stds = [ tu.timeseries_std(date_s.today, interval, periods_y=12, use_lastday=True, extend=1) for interval in intervals ] t_std_y5 = t_stds[6] t_stds_len = [len(x) - 1 for x in t_stds] # 基金标准序列_成立以来 t_std_alls = [ tu.timeseries_std(date_s.today, tu.periods_in_interval(date_s.today, t_min, 12), periods_y=12, use_lastday=True, extend=6) for t_min in t_mins ] # 标准序列_成立以来 t_std_alls = [ t_std_all[:len([x for x in t_std_all if x >= t_min]) + 1] for t_std_all, t_min in zip(t_std_alls, t_mins_tstp) ] # 基准指数的标准序列_成立以来 matchs_bm = [ tu.outer_match4indicator_m(t_bm, t_std_all, False) for t_std_all in t_std_alls ] idx_matchs_bm = [x[1] for x in matchs_bm] price_bm0_all = [[ prices_bm[0][ix] if ix is not None else None for ix in idx.values() ] for idx in idx_matchs_bm] price_bm1_all = [[ prices_bm[1][ix] if ix is not None else None for ix in idx.values() ] for idx in idx_matchs_bm] price_bm2_all = [[ prices_bm[2][ix] if ix is not None else None for ix in idx.values() ] for idx in idx_matchs_bm] price_bm3_all = [[ prices_bm[3][ix] if ix is not None else None for ix in idx.values() ] for idx in idx_matchs_bm] price_bm4_all = [[ prices_bm[4][ix] if ix is not None else None for ix in idx.values() ] for idx in idx_matchs_bm] matchs_pe = [ tu.outer_match4indicator_m(t_pe, t_std_all, False) for t_std_all in t_std_alls ] idx_matchs_pe = [x[1] for x in matchs_pe] price_pe_all = [[ price_pe[ix] if ix is not None else None for ix in idx.values() ] for idx in idx_matchs_pe] # 基准指标的收益率_成立以来 r_bm0_all = [fi.gen_return_series(x) for x in price_bm0_all] r_bm1_all = [fi.gen_return_series(x) for x in price_bm1_all] r_bm2_all = [fi.gen_return_series(x) for x in price_bm2_all] r_bm3_all = [fi.gen_return_series(x) for x in price_bm3_all] r_bm4_all = [fi.gen_return_series(x) for x in price_bm4_all] r_pe_all = [fi.gen_return_series(x) for x in price_pe_all] tmp = [len(idx_matchs_bm[i]) for i in range(len(idx_matchs_bm))] tmp_id = tmp.index(max(tmp)) tmp_list = [ r_tbond[ix] if ix is not None else None for ix in idx_matchs_bm[tmp_id].values() ] tmp = pd.DataFrame(tmp_list)[0].fillna(method="backfill").tolist() r_f_all = [[ r_tbond[idx[k]] if idx[k] is not None else tmp[k] for k in idx.keys() ] for idx in idx_matchs_bm] r_f_all = [x[1:] for x in r_f_all] # 基准指标的收益率_不同频率 matchs_bm = tu.outer_match4indicator_m(t_bm, t_std_y5, False) # 基准指数标准序列_成立以来 matchs_pe = tu.outer_match4indicator_m(t_pe, t_std_y5, False) idx_matchs_bm = matchs_bm[1] idx_matchs_pe = matchs_pe[1] price_bm0_y5 = [ prices_bm[0][ix] if ix is not None else None for ix in idx_matchs_bm.values() ] price_bm1_y5 = [ prices_bm[1][ix] if ix is not None else None for ix in idx_matchs_bm.values() ] price_bm2_y5 = [ prices_bm[2][ix] if ix is not None else None for ix in idx_matchs_bm.values() ] price_bm3_y5 = [ prices_bm[3][ix] if ix is not None else None for ix in idx_matchs_bm.values() ] price_bm4_y5 = [ prices_bm[4][ix] if ix is not None else None for ix in idx_matchs_bm.values() ] price_pe_y5 = [ price_pe[ix] if ix is not None else None for ix in idx_matchs_pe.values() ] # 基准指标的收益率_不同频率 r_bm0_y5 = fi.gen_return_series(price_bm0_y5) r_bm1_y5 = fi.gen_return_series(price_bm1_y5) r_bm2_y5 = fi.gen_return_series(price_bm2_y5) r_bm3_y5 = fi.gen_return_series(price_bm3_y5) r_bm4_y5 = fi.gen_return_series(price_bm4_y5) r_pe_y5 = fi.gen_return_series(price_pe_y5) r_f_y5 = [ r_tbond[ix] if ix is not None else None for ix in idx_matchs_bm.values() ] r_f_y5 = r_f_y5[1:] rs_bm0 = [r_bm0_y5[:length - 1] for length in t_stds_len] rs_bm1 = [r_bm1_y5[:length - 1] for length in t_stds_len] rs_bm2 = [r_bm2_y5[:length - 1] for length in t_stds_len] rs_bm3 = [r_bm3_y5[:length - 1] for length in t_stds_len] rs_bm4 = [r_bm4_y5[:length - 1] for length in t_stds_len] rs_pe = [r_pe_y5[:length - 1] for length in t_stds_len] rs_f = [r_f_y5[:length - 1] for length in t_stds_len] benchmark = { 1: rs_bm0, 2: rs_bm1, 3: rs_bm2, 4: rs_pe, 6: rs_bm3, 7: rs_bm4 } benchmark_all = { 1: r_bm0_all, 2: r_bm1_all, 3: r_bm2_all, 4: r_pe_all, 6: r_bm3_all, 7: r_bm4_all } for i in range(len(ids_o)): df = dfs[i] df.index = range(len(df)) idx4slice = su.idx4slice(df, "fund_id") navs = su.slice(df, idx4slice, "nav") t_reals = su.slice(df, idx4slice, "statistic_date") matchs_all = [ tu.outer_match4indicator_m(t_real, t_std_alls[i], drop_none=False) for t_real in t_reals ] idx_matchs_all = [x[1] for x in matchs_all] nav_matchs_all = [[ nav[ix] if ix is not None else np.NaN for ix in idx.values() ] for nav, idx in zip(navs, idx_matchs_all)] nv_matrix = np.array(nav_matchs_all).T r_total = np.nanmean((nv_matrix[:-1] / nv_matrix[1:] - 1), axis=1) price_total = np.nancumprod(1 + r_total[::-1])[::-1].tolist() price_total.append(1) # 定义基期伪价格为1 r_total = fi.gen_return_series(price_total) prices = [] for j in range(7): if t_mins[i] + relativedelta(months=intervals[j]) <= date_s.today: length = min(len(price_total), t_stds_len[j]) prices.append(price_total[:length]) else: prices.append(None) for j in range(7, 11): length = min(len(price_total), t_stds_len[j]) prices.append(price_total[:length]) prices.append(price_total) navs2 = [prices[i] for i in intervals5] navs3 = [prices[i] for i in intervals6] rs2 = [fi.gen_return_series(x) for x in navs2] rs3 = [fi.gen_return_series(x) for x in navs3] rs_f_ = rs_f.copy() rs_f_.append(r_f_all[i]) rs_f2_ = [rs_f_[i] for i in intervals5] rs_f3_ = [rs_f_[i] for i in intervals6] for k in benchmark.keys(): rs_bm_ = benchmark[k].copy() # 指定benchmark rs_bm_.append(benchmark_all[k][i]) rs_bm2 = [rs_bm_[i] for i in intervals5] rs_bm3 = [rs_bm_[i] for i in intervals6] s_time = [ fi.competency_timing(r, r_bm, r_f) for r, r_bm, r_f in zip(rs3, rs_bm3, rs_f3_) ] s_security = [ fi.competency_stock(r, r_bm, r_f) for r, r_bm, r_f in zip(rs3, rs_bm3, rs_f3_) ] persistence = [ fi.persistence_er(r, r_bm) for r, r_bm in zip(rs2, rs_bm2) ] odds = [fi.odds(r, r_bm) for r, r_bm in zip(rs2, rs_bm2)] tmp = [odds, persistence, s_time, s_security] result_i = [ ids_o[i], names_o[i], k, 1, 1, nv_matrix.shape[1], 60001, "全产品", 6000101, "全产品", date_s.today ] for x in tmp: result_i.extend(x) result.append(result_i) df = pd.DataFrame(result) df[list(range(11, 41))] = df[list(range(11, 41))].astype(np.float64) df[list(range(11, 41))] = df[list(range(11, 41))].apply(lambda x: round(x, 6)) df.columns = columns df_whole = df_whole.append(df) return df_whole
def array_nancumprod(arr): return np.nancumprod(arr)
def time_nancumprod(self, array_size, percent_nans): np.nancumprod(self.arr)
"gammavariate": random.gammavariate, "betavariate": random.betavariate, "lognormvariate": random.lognormvariate, "paretovariate": random.paretovariate, "vonmisesvariate": random.vonmisesvariate, "weibullvariate": random.weibullvariate, "triangular": random.triangular, "uniform": random.uniform, "nanmean": lambda *args: np.nanmean(args), "nanmin": lambda *args: np.nanmin(args), "nanmax": lambda *args: np.nanmax(args), "nansum": lambda *args: np.nansum(args), "nanstd": lambda *args: np.nanstd(args), "nanmedian": lambda *args: np.nanmedian(args), "nancumsum": lambda *args: np.nancumsum(args), "nancumprod": lambda *args: np.nancumprod(args), "nanargmax": lambda *args: np.nanargmax(args), "nanargmin": lambda *args: np.nanargmin(args), "nanvar": lambda *args: np.nanvar(args), "mean": lambda *args: np.mean(args), "min": lambda *args: np.min(args), "max": lambda *args: np.max(args), "sum": lambda *args: np.sum(args), "std": lambda *args: np.std(args), "median": lambda *args: np.median(args), "cumsum": lambda *args: np.cumsum(args), "cumprod": lambda *args: np.cumprod(args), "argmax": lambda *args: np.argmax(args), "argmin": lambda *args: np.argmin(args), "var": lambda *args: np.var(args)})
np.prod(matriz) # Devuelve el producto de los elementos de la matriz np.sum(matriz) # Devuelve la suma de los elementos de la matriz np.nanprod( matriz ) # Devuelve el producto de los elementos de la matriz tomando nans como unos np.nansum( matriz ) # Devuelve la suma de los elementos de la matriz tomando nans como ceros np.cumprod( matriz ) # Devuelve un vector con el producto acumulado de los elementos de la matriz np.cumsum( matriz ) # Devuelve un vector con la suma acumulada de los elementos de la matriz np.nancumprod( matriz ) # Devuelve un vector con el producto acumulado de los elementos de la matriz tomando nans como unos np.nancumsum( matriz ) # Devuelve un vector con la suma acumulada de los elementos de la matriz nans como ceros np.diff( matriz, dimensión ) # Devuelve una matriz con las diferencias (deltas) entre valores respecto a la posición de laa dimensión especificada np.ediff1d( matriz ) # Devuelve un vector con las diferencias (deltas) entre valores consecutivos de la matriz contraida a una dimensión np.gradient(matriz) # Devuelve el gradiente de una matriz N-dimensional # Operaciones aritméticas np.add(matriz1, matriz2) # Suma de matrices np.reciprocal(matriz) # reciproco (Inverso multiplicativo)
def cumulative_return(return_arr, axis=0): return np.nancumprod(return_arr + 1, axis=axis)
def backtest(data, wpcol, ppcol, wstake, pstake): ''' Input: data: the dataframe wpcol: colunm name of the win probability ppcol: colunm name of the place probability wstake: colunm name of the win bet ratio pstake: colunm name of the place bet ratio Output: y: a summary series ''' groups = data.groupby(['rdate', 'rid']) ### 1. average of RMSE of win prob over games rmse_win = groups.apply(RMSE, col1=wpcol, col2='ind_win') avgrmse_win = rmse_win.mean() ### 2. average RMSE of place prob over games rmse_pla = groups.apply(RMSE, col1=ppcol, col2='ind_pla') avgrmse_pla = rmse_pla.mean() ### 3. compute and summarize return by REAL odds retpg = groups.apply(RETpg, col1=wstake, col2=pstake) # cumulative wealth cum_wealth = np.nancumprod(1 + retpg) # final wealth finalwealth = cum_wealth[-1] # total profit totalprofit = finalwealth - 1 # bet ratio per game ratiopg = groups[wstake].sum() + groups[pstake].sum() # bet amount per game costpg = ratiopg * (cum_wealth / (1 + retpg)) # mean return per dollar meanretpd = np.round(totalprofit / costpg.sum(), 4) ### 4. compute and summarize return by FAIR odds retpg_fair = groups.apply(RETpg_fair, col1=wstake, col2=pstake) # cumulative wealth cum_wealth_fair = np.nancumprod(1 + retpg_fair) # final wealth finalwealth_fair = cum_wealth_fair[-1] # total profit totalprofit_fair = finalwealth_fair - 1 # mean return per dollar meanretpd_fair = np.round(totalprofit_fair / costpg.sum(), 4) # number of betting games ngames = (ratiopg != 0).sum() # number of betting horses nhorses = ((data['winstake'] + data['plastake']) != 0).sum() # plot changes of cumulative wealth per game if ngames > 0: #plt.figure(figsize=(15,10)) plt.figure(figsize=(9, 6)) plt.plot(cum_wealth, color='blue', label='Real Odds') plt.plot(cum_wealth_fair, color='orange', label='Fair Odds') plt.grid(alpha=0.6) plt.legend(loc='upper right') plt.xlabel('Games') plt.ylabel('Cumulative Wealth') # create the summary series y = pd.Series([ np.round(avgrmse_win, 4), np.round(avgrmse_pla, 4), meanretpd, np.round(totalprofit, 4), np.round(finalwealth, 4), meanretpd_fair, np.round(totalprofit_fair, 4), np.round(finalwealth_fair, 4), nhorses, ngames ], index=[ 'AverageRMSEwin', 'AverageRMSEpalce', 'MeanRetPerDollar(Real odds)', 'TotalProfit(Real odds)', 'FinalWealth(Real odds)', 'MeanRetPerDollar(Fair odds)', 'TotalProfit(Fair odds)', 'FinalWealth(Fair odds)', 'No.Horses', 'No.Games' ]) print(y) return y
def test_nancumprod(self): tgt = np.cumprod(self.mat) for mat in self.integer_arrays(): assert_equal(np.nancumprod(mat), tgt)
import numpy as np print("nancumprod 1:", np.nancumprod(1)) print("nancumprod [1]: ", np.nancumprod([1])) print("nancumprod [1, np.nan]: ", np.nancumprod([1, np.nan])) a = np.array([[1, 2], [3, np.nan]]) print("namcumprod [[1, 2], [3, np.nan]: ", np.nancumprod(a)) print("namcumprod [[1, 2], [3, np.nan], axis=0: ", np.nancumprod(a, axis=0)) print("namcumprod [[1, 2], [3, np.nan], axis=1: ", np.nancumprod(a, axis=1))
def expected_present_value( cash_flows: Union[Iterable, np.ndarray], probabilities: Union[Iterable, np.ndarray], interest_rates: Union[Iterable, np.ndarray], ) -> Union[float, np.ndarray]: """ This function is useful for calculating variable streams of cash flows, interest rates, and probabilities. Args: cash_flows: payouts from time 0 to n, where time n is the last possible payout, e.g., (cf0, cf1, cf2, ..., cfn-1, cfn) probabilities: probability of a cash flow occuring from time 0 to n, e.g., (1p0, 2p1, 3p2, ..., npn-1) interest_rates: collection of interest rates to use for discounting, where each interest rate is for one period, e.g., (i0to1, i1to2, ..., in-1ton) Returns: The expected present value Example: Given the probabilities: 1p0 = 0.98, 2p1=0.94, 3p2=0.91 annuity(x=0, i=0.07, n=3) expected_present_value((1, 1, 1), (0.98, 0.94, 0.91), (0.07, 0.07, 0.07)) The two methods of calculating the present value of an annuity are the same Additionally a two-dimensional arrays of inputs can be provided to perform multiple expected present values at once. The work flows below produce the same results: expected_present_value((1, 1, 1), (0.98, 0.94, 0.91), (0.07, 0.07, 0.07)) expected_present_value((1, 1, 1), (0.88, 0.84, 0.81), (0.06, 0.06, 0.06)) expected_present_value((1, 1, 1), (0.78, 0.74, 0.71), (0.05, 0.05, 0.05)) expected_present_value( np.array([ [1, 1, 1], [1, 1, 1], [1, 1, 1] ]), np.array([ [0.98, 0.96, 0.91], [0.88, 0.86, 0.81], [0.78, 0.76, 0.71] ]), np.array([ [0.07, 0.07, 0.07], [0.06, 0.06, 0.06], [0.05, 0.05, 0.05] ]) ) """ cash_flows = np.array(cash_flows) probabilities = np.array(probabilities) interest_rates = np.array(interest_rates) if not cash_flows.size == probabilities.size == interest_rates.size: raise InvalidEPVInputs( "The shape of the inputs do not match! The cash " f"flow shape is {cash_flows.shape}, the probability " f"shape is {probabilities.shape}, " f"the interest rate shape is {interest_rates.shape}!" ) if cash_flows.ndim > 2 or probabilities.ndim > 2 or interest_rates.ndim > 2: raise InvalidEPVInputs( "The dimensions of the inputs are too high! The cash " f"flow number of dimensions is {cash_flows.ndim}, the probability " f"number of dimensions is {probabilities.ndim}, " f"the interest rate number of dimensions is {interest_rates.ndim}! " "The number of dimensions for each input must be less than 3." ) discount_factors = discount_factor(interest_rates) if interest_rates.ndim == 1: cumulative_product = np.nancumprod(discount_factors) epv = np.nansum( np.multiply(np.multiply(cash_flows, probabilities), cumulative_product) ) else: cumulative_product = np.apply_along_axis(np.nancumprod, 1, discount_factors) epv = np.apply_along_axis( np.nansum, 1, np.multiply(np.multiply(cash_flows, probabilities), cumulative_product), ) return epv
def test_nancumprod(self): self.check(np.nancumprod, masked_result=False) resi = np.nancumprod(self.mb, axis=1) assert not isinstance(resi, Masked) assert_array_equal(resi, np.array([[1, 2, 6], [4, 4, 24]]))
def stack_cumprod(arrs, nodata=None): """see stack_stats""" a = check_stack(arrs) if nodata is not None: a = mask_stack(a, nodata=nodata) return np.nancumprod(a, axis=0)
def cumproduct(self) -> np.array: return np.nancumprod( self.__base) if len(self.__base) > 0 else None
def nancumprod(a, axis=None, dtype=None, out=None): """ Return the cumulative product of tensor elements over a given axis treating Not a Numbers (NaNs) as one. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones. Ones are returned for slices that are all-NaN or empty. Parameters ---------- a : array_like Input tensor. axis : int, optional Axis along which the cumulative product is computed. By default the input is flattened. dtype : dtype, optional Type of the returned tensor, as well as of the accumulator in which the elements are multiplied. If *dtype* is not specified, it defaults to the dtype of `a`, unless `a` has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used instead. out : Tensor, optional Alternative output tensor in which to place the result. It must have the same shape and buffer length as the expected output but the type of the resulting values will be cast if necessary. Returns ------- nancumprod : Tensor A new array holding the result is returned unless `out` is specified, in which case it is returned. See Also -------- mt.cumprod : Cumulative product across array propagating NaNs. isnan : Show which elements are NaN. Examples -------- >>> import mars.tensor as mt >>> mt.nancumprod(1).execute() array([1]) >>> mt.nancumprod([1]).execute() array([1]) >>> mt.nancumprod([1, mt.nan]).execute() array([ 1., 1.]) >>> a = mt.array([[1, 2], [3, mt.nan]]) >>> mt.nancumprod(a).execute() array([ 1., 2., 6., 6.]) >>> mt.nancumprod(a, axis=0).execute() array([[ 1., 2.], [ 3., 2.]]) >>> mt.nancumprod(a, axis=1).execute() array([[ 1., 2.], [ 3., 3.]]) """ a = astensor(a) if dtype is None: dtype = np.nancumprod(np.empty((1, ), dtype=a.dtype)).dtype op = TensorNanCumprod(axis=axis, dtype=dtype) return op(a, out=out)
def func(x): return np.nancumprod(x)[-1]
def stack_cumprod(arrs, nodata=None): """see stack_stats""" arrs = check_stack(arrs) a_m = mask_stack(arrs, nodata=nodata) return np.nancumprod(a_m, axis=0)