473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

mathmatical expressions evaluation

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
8 2090
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
> 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
"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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

72
by: Raymond Hettinger | last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289. That led to a number of contributors helping to re-work the pep details into a form that has been well received on the...
8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
24
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I...
7
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And...
2
by: webposter | last post by:
Hi, I am looking for information on a data structure (and associated algorithm) to do short-circuit evaluation of boolean expressions and haven't found a single one even after googing for two...
13
by: Daniel W | last post by:
Hi! I tried to post this to comp.lang.c.moderated but it didn't seem to go through. I've got a question about volatiles in assignment expressions. I found the following code snippet in an...
8
by: birchb | last post by:
While working on type expressions I am rather stuck for a way to express recursive types. A simple example of this is a singly-linked list of integers. In some languages you have compiler syntax...
15
by: skibud2 | last post by:
Consider this example: u8 my_array; for (i = 0; i < (sizeof(my_array)/sizeof(u8)); i++) { ... } In the standard C specification, is the evaluation of '(sizeof(my_array)/sizeof(u8))' to 10...
11
by: Pietro Cerutti | last post by:
Hi group, here I come with a question which is quite simple per se, but for which I can't find an answer. Does the C standard guarantee that inside an expression such as (x && y) "y" is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.