mathmatical expressions evaluation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tonino

    #1

    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

  • beliavsky@aol.com

    #2
    Re: mathmatical expressions evaluation

    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.

    Comment

    • aaronwmail-usenet@yahoo.com

      #3
      Re: mathmatical expressions evaluation

      > have a task of evaluating a complex series (sorta) of mathematical[color=blue]
      > expressions and getting an answer ...[/color]

      If we assume that you are looking for functionality and speed is
      secondary,
      please have a look at the technique in


      Download xsdb -- eXtremely Simple DataBase for free. XSDB XML is to DATA as HTML is to DOCUMENT. Publish and combine data as easily as HTML format and web browsers publish and view documents.


      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"



      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"

      Comment

      • Paul McGuire

        #4
        Re: mathmatical expressions evaluation

        "Tonino" <tonino.greco@g mail.com> wrote in message
        news:1103719856 .106409.190100@ z14g2000cwz.goo glegroups.com.. .[color=blue]
        > 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
        >[/color]
        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 amortizationSch edule( principal, term, rate ):
        pmt = ( principal * rate * ( 1 + rate)**term ) / (( 1 + rate)**term - 1)
        pmt = round(pmt,2) # people rarely pay in fractional pennies
        remainingPrinci pal = principal
        for pd in range(1,term+1) :
        if pd < term:
        pdInterest = rate * remainingPrinci pal
        pdInterest = round(pdInteres t,2)
        pdPmt = pmt
        else:
        pdInterest = 0
        pdPmt = remainingPrinci pal
        pdPrincipal = pdPmt - pdInterest
        remainingPrinci pal -= pdPrincipal
        yield pd, pdPmt, pdInterest, pdPrincipal, remainingPrinci pal

        # 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 amortizationSch edule(P, Y*12,
        R/100.0/12.0):
        print i, pmt, int, princ, remaining

        print

        def aprToEffectiveA pr(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,
        aprToEffectiveA pr(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


        Comment

        • John Machin

          #5
          Re: mathmatical expressions evaluation


          Paul McGuire wrote:[color=blue]
          > Here's
          > a simple loan amortization schedule generator:
          >
          > def amortizationSch edule( principal, term, rate ):
          > pmt = ( principal * rate * ( 1 + rate)**term ) / (( 1 +[/color]
          rate)**term - 1)

          Simpliciter:
          pmt = principal * rate / (1 - (1 + rate)**(-term))
          [color=blue]
          > pmt = round(pmt,2) # people rarely pay in fractional pennies
          > remainingPrinci pal = principal
          > for pd in range(1,term+1) :
          > if pd < term:
          > pdInterest = rate * remainingPrinci pal
          > pdInterest = round(pdInteres t,2)
          > pdPmt = pmt
          > else:[/color]

          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.
          [color=blue]
          > pdInterest = 0
          > pdPmt = remainingPrinci pal
          > pdPrincipal = pdPmt - pdInterest
          > remainingPrinci pal -= pdPrincipal
          > yield pd, pdPmt, pdInterest, pdPrincipal, remainingPrinci pal
          >[/color]

          Comment

          • beliavsky@aol.com

            #6
            Re: mathmatical expressions evaluation

            Paul McGuire wrote:[color=blue]
            >And I wouldn't necessarily agree with beliavsky's assertion that[/color]
            numarray[color=blue]
            >arrays are needed to represent payment dates and amounts, unless you[/color]
            were[color=blue]
            >going to implement a major banking financial system in Python.[/color]

            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.

            Comment

            • Tonino

              #7
              Re: mathmatical expressions evaluation

              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 ;)

              Comment

              • Kent Johnson

                #8
                Re: mathmatical expressions evaluation

                Tonino wrote:[color=blue]
                > 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 ...[/color]

                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
                [color=blue]
                >
                > 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 ;)
                >[/color]

                Comment

                • Tonino

                  #9
                  Re: mathmatical expressions evaluation

                  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

                  Comment

                  Working...