Programming a Finance Calculator in Mathematica

The following excerpts are from an assignment set for Year Twelve Mathematics B students in Semester Two. The assessment is part of the unit on the application of geometric progressions to financial contexts. It is an open task, but has at its core the requirement for students to write a program to produce their own finance calculator.

Finance Calculator

Using Mathematica, build your own financial calculator tool.

It can focus on any particular context you wish to consider, be it loans (home, car, credit cards, etc…), investments (share market, superannuation, etc…), comparisons of options (rent vs buy, saving vs loaning, etc…) or other. The scope of your calculator can be broad or specific, performing a number of different functions or focusing on analyzing one context in depth.

Examine other calculators freely available online (a short list to get you started is on the following page) to consider existing products. Choose a type you want to focus on and comment on what factors similar calculators do and don’t account for. You will develop your calculator with the focus on improving on these existing products with regard to their calculations, not necessarily their appearance.

Develop Mathematica code to implement your equations and compare its results to hand calculations using test data to confirm it works.

With this assessment as the background we will now investigate how to composite basic functions in order to teach students how to write programs in Mathematica.

Consider the following simple calculation:

  1. What is the effective interest rate of 5% p.a. compounding monthly?

All operations performed in Mathematica are done using functions. This makes it an incredibly useful platform for helping to show students how composite functions work. As such I have deliberately emphasized the composition of the functions used, using the form f[g[x]].

Mathematica has a function called EffectiveInterest[r,c], which calculates the effective interest given the interest rate (r), as a decimal, and the compounding term (c), as a fraction of a year:

An important thing to note about Mathematica is that it uses exact value. To round off, take the EffectiveInterest function and put that inside the Round[x,n] function, where x is the value to be rounded and it is rounded to the nearest multiple of n; creating the composite function Round[EffectiveInterest[r,c],n]:

This is a more easily understood solution.

Now consider a calculation that would use the former calculation:

  1. Gene has $1000 that she invests at 5% p.a. compounding monthly. How much will she have after 8 years?

Mathematica’s TimeValue[p,r,t] function determines the future value of a principal (p), at a given interest rate (r), over a number of years (t). To again keep the numbers recognizable round the value off to the nearest hundredth. This process creates the composite function: Round[TimeValue[p,EffectiveInterest[r,c],t],n].

So far Mathematica is basically operating as a calculator. To solve a problem with a principal of $300 at 3% p.a. compounding weekly for 5 years the values can be changed and the function re-executed.

But by using parameters instead of specific values the code can be easily converted to solve for any values within a specified range.

The function Manipulate[e,p] allows us to add parameters (p) to a specific expression (e).

1. Composite our existing calculation (e) within a Manipulate[] function

2. Change the constant values to parameters with relevant names (in blue)

3. State what the range of possible values are for each parameter

There are rules around choosing names and different conventions in different programming circles. Avoid spaces and start with a lower case letter in most cases.

Most of the parameters are of the form: {name, lowest value, highest value, step size, other options}

For example:

This can be read as “a parameter called principal that can range from 100 to 100 000, increasing in lots of 100, with the current value visible”.

ctern is different in that it doesn’t have lowest, highest and step values, but instead has a specified list of possible values: {1, 12, 26, 52, 365}. It also doesn’t use a sliding bar to choose the current value but instead presents the set list of options.

This program can accept a range of values for each parameter and can perform the calculation with live updating as changes are made.

The user interface can be improved by giving the parameters more informative headings, rather than just the name, and setting starting values, rather than defaulting to the lowest value.

These changes set the starting interest rate to 3 and give it the title “rate (%)” and set the starting compounding term to 12 and give it the title “compounding term”.

Further adjustments can be made to make the calculator more useful and visually appropriate. The following code adds in a graph of the compound growth and adds some formatting code, in the form of Column[] and Row[], to better present the output. It also includes code comments, marked with (* and *) within the program to assist with understanding the code.

These same basic principles can be applied to any mathematical process; enabling students to develop dynamic representations across a range of different concepts.

Home Loan Calculator

Trigonometric Graphs

Fraction Equivalence

About the blogger:

Miles Ford

Miles Ford integrates Mathematica and the Wolfram Language into every aspect of his teaching of the Queensland and Australian mathematics curriculums. This includes using them to teach concepts through notebooks and interactive models, as well as student use of their computational power and programming potential across the learning and assessment spectrum. While primarily focused on the mathematics classroom, he also works with other teachers to take advantage of Mathematica’s power across the science, technology, and humanities fields.

Miles is the Head of Mathematics at St John’s Anglican College and has been using Mathematica since 2011, when he introduced it to his senior mathematics programs. Over the last few years, he has expanded its use throughout the mathematics department across all secondary school levels and into other curriculum areas. He particularly enjoys using the Wolfram Language to solve novel problems and helping students to develop their own solutions using Mathematica. Miles presented at the Australian Wolfram Technology Conference 2015 on the process of embedding Mathematica into the mathematics program.

--

--

Tech-Based Teaching Editor
Tech-Based Teaching: Computational Thinking in the Classroom

Tech-Based Teaching is all about computational thinking, edtech, and the ways that tech enriches learning. Want to contribute? Reach out to edutech@wolfram.com.