mathmatical expressions evaluation

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Dec 22 15:15:49 EST 2004


"Tonino" <tonino.greco at gmail.com> wrote in message
news:1103719856.106409.190100 at z14g2000cwz.googlegroups.com...
> Hi,
>
> I have a task of evaluating a complex series (sorta) of mathematical
> expressions and getting an answer ...
>
> I have looked at the numarray (not really suited??) and pythonica (too
> simple??) and even tried using eval() ... but wondered if there were
> other packages/modules that would enable me to to this a bit easier...
>
> The formula/equations are for investment calculations (Swap Yields).
> Is there a module/method that anyone can suggest ??
>
> Many thanks
> Tonino
>
Is it a financial analysis library you are looking for, or an expression
parser?

You will find a basic expression parser included in the examples with the
pyparsing.  This parser can be easily extended to add built-in functions,
additional operators, etc.  (You can download pyparsing at
http://pyparsing.sourceforge.net.)

What does one of these complex mathematical functions look like, anyway?

And I wouldn't necessarily agree with beliavsky's assertion that numarray
arrays are needed to represent payment dates and amounts, unless you were
going to implement a major banking financial system in Python.  You can
easily implement payment schedules using lists, or even generators.  Here's
a simple loan amortization schedule generator:

def amortizationSchedule( principal, term, rate ):
    pmt = ( principal * rate * ( 1 + rate)**term ) / (( 1 + rate)**term - 1)
    pmt = round(pmt,2)  # people rarely pay in fractional pennies
    remainingPrincipal = principal
    for pd in range(1,term+1):
        if pd < term:
            pdInterest = rate * remainingPrincipal
            pdInterest = round(pdInterest,2)
            pdPmt = pmt
        else:
            pdInterest = 0
            pdPmt = remainingPrincipal
        pdPrincipal = pdPmt - pdInterest
        remainingPrincipal -= pdPrincipal
        yield pd, pdPmt, pdInterest, pdPrincipal, remainingPrincipal

# print amortization schedule for $10,000 loan for 3 years at 6% annual
P = 10000
Y = 3
R = 6.00
for (i, pmt, int, princ, remaining) in amortizationSchedule(P, Y*12,
R/100.0/12.0):
    print i, pmt, int, princ, remaining

print

def aprToEffectiveApr(apr):
    apr /= 1200.0
    return round(((1+apr)**12-1) * 100, 2)

APR = R
print "Nominal APR of %.2f%% is an effective APR of %.2f%%" % (APR,
aprToEffectiveApr(APR) )

prints out (rather quickly, I might add):
1 304.22 50.0 254.22 9745.78
2 304.22 48.73 255.49 9490.29
3 304.22 47.45 256.77 9233.52
4 304.22 46.17 258.05 8975.47
5 304.22 44.88 259.34 8716.13
6 304.22 43.58 260.64 8455.49
7 304.22 42.28 261.94 8193.55
8 304.22 40.97 263.25 7930.3
9 304.22 39.65 264.57 7665.73
10 304.22 38.33 265.89 7399.84
11 304.22 37.0 267.22 7132.62
12 304.22 35.66 268.56 6864.06
13 304.22 34.32 269.9 6594.16
14 304.22 32.97 271.25 6322.91
15 304.22 31.61 272.61 6050.3
16 304.22 30.25 273.97 5776.33
17 304.22 28.88 275.34 5500.99
18 304.22 27.5 276.72 5224.27
19 304.22 26.12 278.1 4946.17
20 304.22 24.73 279.49 4666.68
21 304.22 23.33 280.89 4385.79
22 304.22 21.93 282.29 4103.5
23 304.22 20.52 283.7 3819.8
24 304.22 19.1 285.12 3534.68
25 304.22 17.67 286.55 3248.13
26 304.22 16.24 287.98 2960.15
27 304.22 14.8 289.42 2670.73
28 304.22 13.35 290.87 2379.86
29 304.22 11.9 292.32 2087.54
30 304.22 10.44 293.78 1793.76
31 304.22 8.97 295.25 1498.51
32 304.22 7.49 296.73 1201.78
33 304.22 6.01 298.21 903.57
34 304.22 4.52 299.7 603.87
35 304.22 3.02 301.2 302.67
36 302.67 0 302.67 0.0

Nominal APR of 6.00% is an effective APR of 6.17%


-- Paul





More information about the Python-list mailing list