{{{id=136| # Copyright 2009, P. Lutus # Released under the GPL (http://www.gnu.org/copyleft/gpl.html) /// }}} {{{id=26| #auto # reset() # commented out for now -- ticket 7255 forget() # special equation rendering def render(x,name = "temp.png",size = "normal"): if(type(x) != type("")): x = latex(x) latex.eval("\\" + size + " $" + x + "$",{},"",name) import re var('a, b, c, d, e, fv, pv, pmt, ir, np, pb') # define base financial function bffv = fv == ((pb*ir+1)*pmt-(ir+1)^np*((pb*ir*pmt)+pmt+ir*pv))/ir # format latex with spaces between multi-character names def latex_spc(x): r = latex(x) # return "$" + re.sub("(\{\w+\s*)(\}\s+[^+-])","\\1 \\2",r) + "$" return "" + re.sub("(\{\w+\s*)(\}\s+[^+-])","\\1 \\2",r) + "" # create functions in global namespace def gen_estr(f,s): soln = solve(f,s)[0].exp_simplify().rhs() return "f%s(" % s + re.sub("%s,?" % s,"","fv,pv,pmt,ir,np,pb") + ") = " + str(soln) def make_functs(list): for v in list: exec preparse(v) in globals() equations = [gen_estr(bffv,v) for v in [fv, pv, pmt, np]] reset('ffv fpv fpmt fnp');make_functs(equations) # special case root finder defines fir() def fir(fv,pv,pmt,ir,np,pb): qf = lambda ir: N(ffv(pv,pmt,ir,np,pb) - fv) # never allow ir to equal zero -- # instead, test ranges on both sides try: r = find_root(qf,1e-9,100) except: try: r = find_root(qf,-1e-9,-100) except: print "*** \"ir\" root finder failed. ***" r = 0 return r /// }}} {{{id=104| var('fv, pv, pmt, ir, np, pb') def test_combo(fv,pv,pmt,ir,np,pb): list = [ffv(pv,pmt,ir,np,pb),fpv(fv,pmt,ir,np,pb),fpmt(fv,pv,ir,np,pb),fir(fv,pv,pmt,ir,np,pb),N(fnp(fv,pv,pmt,ir,pb).real()),pb] print "fv: %12.2f" % list[0] print "pv: %12.2f" % list[1] print "pmt: %12.2f" % list[2] print "ir: %22.12f" % list[3] print "np: %12.2f" % list[4] print "pb: %12.2f" % list[5] print map(lambda x:float("%.2f" % x) ,list) /// }}} {{{id=82| test_combo(23003.0,0.0,-100,.01,120,0) /// fv: 23003.87 pv: 0.26 pmt: -100.00 ir: 0.009999463888 np: 120.00 pb: 0.00 [23003.869999999999, 0.26000000000000001, -100.0, 0.01, 120.0, 0.0] }}} {{{id=112| render("$fv = -pmt \\frac {(1+ir)^{np}-1}{ir}$","equ_fv_simplest.png","large") /// }}} {{{id=113| fvsimple(pmt,ir,np) = -pmt*(((1+ir)^np)-1)/ir;show(fvsimple) ///
\left( \mbox{pmt}, \mbox{ir}, \mbox{np} \right) \ {\mapsto} \ -\frac{{({(\mbox{ir} + 1)}^{\mbox{np}} - 1)} \mbox{pmt}}{\mbox{ir}}
}}} {{{id=114| fvsimple(-100,.01,120) /// 23003.8689457367 }}} {{{id=115| ffv(0,-100,0.01,120,0) /// 23003.8689457367 }}} {{{id=116| bffv = fv == ((pb*ir+1)*pmt-(ir+1)^np*((pb*ir*pmt)+pmt+ir*pv))/ir render(latex_spc(bffv),"base_fv_equation.png","large") /// }}} {{{id=117| ffv(pv,pmt,ir,np,pb) = bffv.rhs() render(latex("ffv(pv,pmt,ir,np,pb) = ") + latex_spc(ffv(pv,pmt,ir,np,pb)),"base_fv_function.png","large") /// }}} {{{id=118| render(latex("ffv(pv,pmt,ir,np,0) = ")+latex_spc(ffv(pv,pmt,ir,np,0)),"base_fv_function_pb0.png","large") /// }}} {{{id=119| render(latex("ffv(pv,pmt,ir,np,1) = ")+latex_spc(ffv(pv,pmt,ir,np,1)),"base_fv_function_pb1.png","large") /// }}} {{{id=121| ffv(0,-100,.01,120,1) /// 23233.9076351941 }}} {{{id=122| ffv(-1234,-100,.01,120,0) /// 27076.5463736406 }}} {{{id=123| fpmt(fv,pv,ir,np,pb) = solve(bffv,pmt)[0].rhs() render(latex("fpmt(fv,pv,ir,np,pb) = ")+latex_spc(fpmt(fv,pv,ir,np,pb)),"fpmt_function.png","large") /// }}} {{{id=124| fpmt(1e6,0,-.01,120,0) /// -14273.0803934121 }}} {{{id=125| fstr = "fq(fv,pv,pmt,ir,np,pb)" n = 0 for f in [fv,pv,pmt,np]: # make a string version of the target fs = str(f) # remove from the argument list # the variable this function defines s = re.sub("%s,?" % str(f),"",fstr) # insert the function's name at 'q' s = re.sub("q",fs,s) # solve for the specified variable soln = solve(bffv,f)[0].rhs().exp_simplify() # format a function with the solution eq = s + " = " + str(soln) # create the function in global namespace exec preparse(eq) in globals() # pretty-print the function ppf = latex(s) + " = " + latex_spc(soln) # show(ppf) fn = "derived_fn%d.png" % n z = latex.eval("\\large $" + ppf + "$",{},"",fn) z = latex("$\input{%s}$" % fn) n += 1 /// }}} {{{id=128| render(latex("np = N(fnp(fv,pv,pmt,ir,pb).real())"),"funct_fnp_wrapped.png","large") /// }}} {{{id=132| fir(0, 10000,-100,0,120,0) /// 0.0031141819459532885 }}} {{{id=133| bffv /// fv == ((ir*pb + 1)*pmt - (ir*pb*pmt + ir*pv + pmt)*(ir + 1)^np)/ir }}} {{{id=134| print "double bffv(double pv, double pmt, double ir, double np, double pb) {\n\treturn " + str(bffv.rhs()) + ";\n}" /// double bffv(double pv, double pmt, double ir, double np, double pb) { return ((ir*pb + 1)*pmt - (ir*pb*pmt + ir*pv + pmt)*(ir + 1)^np)/ir; } }}} {{{id=135| /// }}}