Question

I've decided to build a database tracking the credit card offers I get in the mail, and one confounding factor has been how to represent the offers. With minimum finance charges and variable rates and mimimum variable rates it gets a bit more complex than "10.99 percent". I've decided I need to construct a language to represent these things if I have a shot at comparing or trending these offers.

The question is, how do I represent this? The formulas are far too complex to model by creating fields, so I'll just store the formula in the DB in all it's glory. Yes, this means blurring the line between data and execution, but since this is a personal project I'm not so worried.

  1. Embedded python. Represent the formula as Python and just bind relevant variables and call the formula.
  2. Use a Domain Specific Language. I can't be the first person to think of representing things this way, and in fact I know I'm not. Are there any free, public languages that can represent this?

Can anyone offer advice on which approach is better?

Was it helpful?

Solution

This sounds somewhat similar to the work by Simon Peyton Jones et al. on representing financial contracts using combinator libraries, including an implementation in Haskell, including the ability to run evaluation functions over them to compute the value of the contract given some model. I don't know enough about finance to tell you if this is exactly what you're looking for, but it sounds like it's in the right ballpark.

As to your specific question, I generally prefer embedded domain specific languages (that is, DSL-like constructs embedded in a host language, such as Ruby with its object oriented metaprogramming, Scheme with its macros, or Haskell combinator libraries), since it give you the full power of the host language for free; usually when writing a domain specific language with its own syntax, execution model, runtime, and so on, you run into things that would be easy to do if you had an existing language available, but are a pain to deal with since you haven't implemented something in your DSL.

OTHER TIPS

My vote is for Embedded Python, if you feel you must store the formulas in the database.

A DSL seems like overkill; you already have a perfectly good language for evaluating formulas.

Another option is to use R. It has a nice formula representation; you store it as a string and then evaluate it with the eval() function. There are also extensive financial libraries (see the CRAN finance view), including Rmetrics.

R also interacts very nicely with other languages, including python, java, and C++. You mentioned Python: you can use RPy if you want to write a wrapper script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top