def growth_pe(stockpriceserie, financialdf): growth_pe = {} lasteps = financialdf.eps.iloc[-1] try: n_year = len(financialdf) # Year duration in financialdf growth_pe['growth_min'] = financialdf['epsgrowth'].min() if financialdf.eps.iloc[0] >= 0: growth_pe['growth_mean'] = round( npf.rate(n_year, 0, -1 * financialdf.eps.iloc[0], lasteps), 4) else: # The above equation doesn't work mathematicaly with negative 1st year EPS. # As alternative, mean value will be used. growth_pe['growth_mean'] = financialdf['epsgrowth'].mean() growth_pe['growth_max'] = financialdf['epsgrowth'].max() pe_historical = min_mean_max_pe(stockpriceserie, financialdf) growth_pe['pe_min'] = pe_historical[0] growth_pe['pe_mean'] = pe_historical[1] growth_pe['pe_max'] = pe_historical[2] # print('growth_pe', growth_pe) return growth_pe except: print('EPS does not exist.') return {}
def get_rate_savings(self) -> float: return (rate( self.freq * self.num_of_years, -self.reg_dep, -self.ini_dep, self.fin_bal, self.dep_when, ) * self.freq * 100)
def get_rate_loans(self): return (rate( self.freq * self.num_of_years, -self.reg_pmt, self.loan, 0, self.pmt_when, ) * self.freq * 100)
def calc_parameter(ask_reply, input_string): """ calc_parameter is a calculator to calculate the final goal based on ino given by users. Parameters --------- Param1 : Dictionary The dictionary stores the float provided by user as value, stores corresponding terms as the key. Param2 : String The string is the specified goal for caculation. The string implies what the dictionary would be since each value is dependent to the remaining values. Returns ------ Return1 : Float The final result of requested value after caculation. """ user_fin_dict = ask_reply try: if (ask_reply['input_string']) == 'present value': output = abs( npf.pv(user_fin_dict["required rate per year"], user_fin_dict["required time period"], 0 - user_fin_dict["payment"], user_fin_dict["future value"])) elif (ask_reply['input_string']) == "future value": output = abs( npf.fv(user_fin_dict["required rate per year"], user_fin_dict["required time period"], 0 - user_fin_dict["payment"], 0 - user_fin_dict["present value"])) elif (ask_reply['input_string']) == "payment": output = abs( npf.pmt(user_fin_dict["required rate per year"], user_fin_dict["required time period"], 0 - user_fin_dict["present value"], user_fin_dict["future value"])) elif (ask_reply['input_string']) == "required rate per year": output = abs( npf.rate(user_fin_dict["required time period"], 0 - user_fin_dict["payment"], 0 - user_fin_dict["present value"], user_fin_dict["future value"])) elif (ask_reply['input_string']) == "required time period": output = abs( npf.nper(user_fin_dict["required rate per year"], 0 - user_fin_dict["payment"], 0 - user_fin_dict["present value"], user_fin_dict["future value"])) else: output = random.choice(unknown_input_reply) return output except KeyError: print("I'm dead because of you !") print("You should never have encountered this. What did you do?") return None
def infer_reasonable_share_price(ticker, financialreportingdf, stockpricedf, discountrate, marginrate): years = 2 # period dfprice = pd.DataFrame( columns=['ticker', 'annualgrowthrate', 'lasteps', 'futureeps']) pd.options.display.float_format = '{:20,.2f}'.format # Find EPS Annual Compounded Growth Rate # annualgrowthrate = financialreportingdf.epsgrowth.mean() #growth rate # print('financialreportdf:\n',financialreportingdf) try: # Calcuate the rate per period # parameter: periods , payment, present value, future value firstEPS = financialreportingdf.eps.iloc[0] lastEPS = financialreportingdf.eps.iloc[-1] # Adjust firstEPS at least 1 , prevent npf.rate get NaN if (firstEPS<1): adj = 1 - firstEPS firstEPS = firstEPS + adj lastEPS = lastEPS + adj annualgrowthrate = npf.rate(len(financialreportingdf.eps)-1, 0, -1 * firstEPS,lastEPS) #print("Annual Growth Rate %f" % annualgrowthrate) # Non Conservative #print(financialreportingdf) lasteps = financialreportingdf.eps.tail(1).values[0] # presentvalue #print('1st eps ',financialreportingdf.eps.iloc[0]) #print('last eps ',financialreportingdf.eps.iloc[-1]) #print('annual growth rate ',annualgrowthrate) # conservative # lasteps = financialreportingdf.eps.mean() # np.fv, compute the future value. parameters: interest rate , periods, payment, present value futureeps = abs(npf.fv(annualgrowthrate, years, 0, lasteps)) dfprice.loc[0] = [ticker, annualgrowthrate, lasteps, futureeps] except: print('eps does not exist') dfprice.set_index('ticker', inplace=True) # conservative dfprice['peratio'] = findMinimumPER(stockpricedf, financialreportingdf) # future stock price dfprice['FV'] = dfprice['futureeps'] * dfprice['peratio'] #print('dfprice:\n',dfprice) #print('discountrate: %s' % discountrate) dfprice['PV'] = abs(npf.pv(discountrate, years, 0, fv=dfprice['FV'])) if dfprice['FV'].values[0] > 0: dfprice['marginprice'] = dfprice['PV']*(1-marginrate) else: dfprice['marginprice'] = 0 dfprice['lastprice'] = stockpricedf.Close.tail(1).values[0] dfprice['suggestion'] = np.where( (dfprice['lastprice'] < dfprice['marginprice']), 'BUY', 'SELL') return dfprice
def test_gh48(self): """ Test the correct result is returned with only infeasible solutions converted to nan. """ des = [-0.39920185, -0.02305873, -0.41818459, 0.26513414, numpy.nan] nper = 2 pmt = 0 pv = [-593.06, -4725.38, -662.05, -428.78, -13.65] fv = [214.07, 4509.97, 224.11, 686.29, -329.67] actual = npf.rate(nper, pmt, pv, fv) assert_allclose(actual, des)
def get_goals(goal): print("get_goals", goal) raw_value = npf.rate( goal.time, -goal.montly_contribution, -goal.initial_contribution, goal.goal_value, ) monthly = round_helper(raw_value) yearly = round(pow(12 - 1, monthly), 2) return {"monthly": monthly, "yearly": yearly}
def test_rate_with_infeasible_solution(self, number_type, when): """ Test when no feasible rate can be found. Rate will return NaN, if the Newton Raphson method cannot find a feasible rate within the required tolerance or number of iterations. This can occur if both `pmt` and `pv` have the same sign, as it is impossible to repay a loan by making further withdrawls. """ result = npf.rate(number_type(12.0), number_type(400.0), number_type(10000.0), number_type(5000.0), when=when) is_nan = Decimal.is_nan if number_type == Decimal else numpy.isnan assert is_nan(result)
def test_when(self): # begin assert_equal(npf.rate(10, 20, -3500, 10000, 1), npf.rate(10, 20, -3500, 10000, 'begin')) # end assert_equal(npf.rate(10, 20, -3500, 10000), npf.rate(10, 20, -3500, 10000, 'end')) assert_equal(npf.rate(10, 20, -3500, 10000, 0), npf.rate(10, 20, -3500, 10000, 'end')) # begin assert_equal(npf.pv(0.07, 20, 12000, 0, 1), npf.pv(0.07, 20, 12000, 0, 'begin')) # end assert_equal(npf.pv(0.07, 20, 12000, 0), npf.pv(0.07, 20, 12000, 0, 'end')) assert_equal(npf.pv(0.07, 20, 12000, 0, 0), npf.pv(0.07, 20, 12000, 0, 'end')) # begin assert_equal(npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 1), npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 'begin')) # end assert_equal(npf.pmt(0.08 / 12, 5 * 12, 15000., 0), npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 'end')) assert_equal(npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 0), npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 'end')) # begin assert_equal(npf.ppmt(0.1 / 12, 1, 60, 55000, 0, 1), npf.ppmt(0.1 / 12, 1, 60, 55000, 0, 'begin')) # end assert_equal(npf.ppmt(0.1 / 12, 1, 60, 55000, 0), npf.ppmt(0.1 / 12, 1, 60, 55000, 0, 'end')) assert_equal(npf.ppmt(0.1 / 12, 1, 60, 55000, 0, 0), npf.ppmt(0.1 / 12, 1, 60, 55000, 0, 'end')) # begin assert_equal(npf.nper(0.075, -2000, 0, 100000., 1), npf.nper(0.075, -2000, 0, 100000., 'begin')) # end assert_equal(npf.nper(0.075, -2000, 0, 100000.), npf.nper(0.075, -2000, 0, 100000., 'end')) assert_equal(npf.nper(0.075, -2000, 0, 100000., 0), npf.nper(0.075, -2000, 0, 100000., 'end'))
def test_decimal_with_when(self): """ Test that decimals are still supported if the when argument is passed """ # begin assert_equal( npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), Decimal('1')), npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), 'begin')) # end assert_equal( npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000')), npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), 'end')) assert_equal( npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), Decimal('0')), npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), 'end')) # begin assert_equal( npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), Decimal('1')), npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), 'begin')) # end assert_equal( npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0')), npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), 'end')) assert_equal( npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), Decimal('0')), npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), 'end'))
def get_rate(self): return fin.rate(self.n, self.pmt, self.pv, self.fv, self.when)[0]
print( f'年利率{rate * 100}%,贷款总额¥{abs(pv)}元,每月还款¥{abs(pmt)},需要还款{nper / 12:.2f}年,共{nper:.2f}期。还款总金额:¥{-pmt * nper:.2f}元。' ) '''rate:计算利率''' nper = 240 pmt = -8055.93 pv = 1000000 fv = 0 # nper:还款期数 # pmt:每期还款金额(负值) # pv:贷款总额(正值) # fv:期末剩余贷款金额(正值) rate = npf.rate(nper=nper, pmt=pmt, pv=pv, fv=fv, when='end', guess=0.1, tol=1e-06, maxiter=100) print( f'贷款总额¥{abs(pv)}元,每月还款¥{abs(pmt)}元,分{nper}期还清,期末贷款本金剩余¥{fv}元,贷款利率为:{rate * 12 * 100:.2f}%。' ) '''npv:净现值 净现值是指投资方案所产生的【现金净流量】(流入-流出)以资金成本为贴现率折现之后与原始投资额现值的差额 经济意义: NPV>0表示项目实施后,除保证可实现预定的收益率外,尚可获得更高的收益。 NPV<0表示项目实施后,未能达到预定的收益率水平,而不能确定项目已亏损。 NPV=0表示项目实施后的投资收益率正好达到预期,而不是投资项目盈亏平衡。 ''' rate = 0.281 values = [-100, 39, 59, 55, 20]
def test_rate_decimal(self): rate = npf.rate(Decimal('10'), Decimal('0'), Decimal('-3500'), Decimal('10000')) assert_equal(Decimal('0.1106908537142689284704528100'), rate)
def test_rate(self): assert_almost_equal(npf.rate(10, 0, -3500, 10000), 0.1107, 4)
tir = npf.irr(cf1) tir #MIRR - TIR Modificada mirr = npf.mirr(cf1, wacc, 0.10) mirr if tir > wacc: print("Projeto agrega valor") else: print("Projeto Destrói Valor") #Qual a taxa? taxa = npf.rate(nper, pmt, pv, fv) #Qual Parcela parcela = npf.pmt(rate, nper, pv) #Valuation de Padaria #premissas lucro_operacional = 50000 despesas_capital = 5000 delta_ncg = 3000 reivestimento = (despesas_capital + delta_ncg) i_crescimento = 0.02 periodo = np.array([0, 1, 2, 3, 4, 5])
def test_decimal_with_when(self): """ Test that decimals are still supported if the when argument is passed """ # begin assert_equal(npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), Decimal('1')), npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), 'begin')) # end assert_equal(npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000')), npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), 'end')) assert_equal(npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), Decimal('0')), npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'), Decimal('10000'), 'end')) # begin assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), Decimal('1')), npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), 'begin')) # end assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0')), npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), 'end')) assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), Decimal('0')), npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), Decimal('0'), 'end')) # begin assert_equal(npf.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'), Decimal('0'), Decimal('1')), npf.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'), Decimal('0'), 'begin')) # end assert_equal(npf.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'), Decimal('0')), npf.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'), Decimal('0'), 'end')) assert_equal(npf.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'), Decimal('0'), Decimal('0')), npf.pmt(Decimal('0.08') / Decimal('12'), Decimal('5') * Decimal('12'), Decimal('15000.'), Decimal('0'), 'end')) # begin assert_equal(npf.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'), Decimal('0'), Decimal('1')), npf.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'), Decimal('0'), 'begin')) # end assert_equal(npf.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'), Decimal('0')), npf.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'), Decimal('0'), 'end')) assert_equal(npf.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'), Decimal('0'), Decimal('0')), npf.ppmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('60'), Decimal('55000'), Decimal('0'), 'end')) # begin assert_equal(npf.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'), Decimal('0'), Decimal('1')).flat[0], npf.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'), Decimal('0'), 'begin').flat[0]) # end assert_equal(npf.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'), Decimal('0')).flat[0], npf.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'), Decimal('0'), 'end').flat[0]) assert_equal(npf.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'), Decimal('0'), Decimal('0')).flat[0], npf.ipmt(Decimal('0.1') / Decimal('12'), Decimal('1'), Decimal('24'), Decimal('2000'), Decimal('0'), 'end').flat[0])