By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,583 Members | 3,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,583 IT Pros & Developers. It's quick & easy.

mathmatical expressions evaluation

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
There is QuantLib at http://quantlib.org/ . The site says "QuantLib is
written in C++ with a clean object model, and is then exported to
different languages such as Python, Ruby, and Scheme." I have not tried
it -- if it is easily usable from Python please write back to c.l.p.

There is a Python Finance email list at
http://www.reportlab.co.uk/mailman/l...python-finance .

In calculations involving bonds one needs arrays to store payment dates
and amounts etc., for which numarray should be used instead of Python
lists, for efficiency.

Jul 18 '05 #2

P: n/a
> have a task of evaluating a complex series (sorta) of mathematical
expressions and getting an answer ...


If we assume that you are looking for functionality and speed is
secondary,
please have a look at the technique in
http://cvs.sourceforge.net/viewcvs.p...py?view=markup

which uses the python parser to generate a parse tree for an arbitrary
expression and then imposes its own semantics on the tree. In fact
take
a look at xsdb use guide under "Computing other derived values"

http://xsdb.sourceforge.net/guide.html

since xsdbXML implements general computations over xml inputs.
Let me know if you have any comments/questions/suggestions.
-- Aaron Watters

===
Later on we'll perspire, as we stare at the fire
And face so afraid, the bills left unpaid
Walking in a winter wonderland. --stolen from "for better or worse"

Jul 18 '05 #3

P: n/a
"Tonino" <to**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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
Jul 18 '05 #4

P: n/a

Paul McGuire wrote:
Here's
a simple loan amortization schedule generator:

def amortizationSchedule( principal, term, rate ):
pmt = ( principal * rate * ( 1 + rate)**term ) / (( 1 + rate)**term - 1)

Simpliciter:
pmt = principal * rate / (1 - (1 + rate)**(-term))
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:
Huh? You don't charge interest in the last period? I'd like to apply
for a loan of $1B for a term of one month with monthly repayments.
pdInterest = 0
pdPmt = remainingPrincipal
pdPrincipal = pdPmt - pdInterest
remainingPrincipal -= pdPrincipal
yield pd, pdPmt, pdInterest, pdPrincipal, remainingPrincipal


Jul 18 '05 #5

P: n/a
Paul McGuire wrote:
And I wouldn't necessarily agree with beliavsky's assertion that numarrayarrays are needed to represent payment dates and amounts, unless you weregoing to implement a major banking financial system in Python.


Maybe arrays are not "needed", but especially for vectors of floating
point numbers, I prefer to use a Numeric array rather than a list
because

(1) array operations like sum and exp are possible, and I want z = x +
y to perform an element-wise sum rather than a concatenation of lists.
Given arrays containing a set of times, coupon amounts, and interest
rates, the value of a bond could be a calculated with a single
expression, without loops.

(2) A TypeError is raised if I do something illogical like

x = zeros(3,Float)
x[0] = "dog"

(3) Higher-dimensional arrays are better represented with Numeric or
Numarray arrays than with lists.

Jul 18 '05 #6

P: n/a
thanks all for the info - and yes - speed is not really an issue and no
- it is not an implementation of a complete financial system - but
rather a small subset of a investment portfolio management system
developed by "another company" ...

What I am trying to achieve is to parse a formula(s) and generate a
result ...

I will look into the pyparsing suggested and maybe even leave out
numarrays for now - as it seems a bit overkill ...

The formula are a bit complex and maybe even difficult to write out
Thanks all - I will post my progress ;)

Jul 18 '05 #7

P: n/a
Tonino wrote:
thanks all for the info - and yes - speed is not really an issue and no
- it is not an implementation of a complete financial system - but
rather a small subset of a investment portfolio management system
developed by "another company" ...

What I am trying to achieve is to parse a formula(s) and generate a
result ...
Why do you need to parse the formulas at runtime? It sounds like they are known in advance and you
could just write functions to implement the calculations.

Kent

I will look into the pyparsing suggested and maybe even leave out
numarrays for now - as it seems a bit overkill ...

The formula are a bit complex and maybe even difficult to write out
Thanks all - I will post my progress ;)

Jul 18 '05 #8

P: n/a
yes - this is what I have been doing - created a set of functions to
handle the formula and they all calculate a section of the formula.
Thanks for all the help ;)
Tonino

Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.