Friday, October 31, 2014

Compound Interest and Paying Down a Mortgage

Suppose, you take out a \(T\) period, fixed rate (rate = \(i\)) mortgage for a house worth \(B\). We want to find out how much your fixed monthly payment \(D\)?

To find the answer to this problem consider two independent "thought" buckets.

In one bucket, you have your loan $B grow without payment.

In the other bucket, you regularly contribute $D every year (I am using a yearly interest rate - modifications for monthly payments are straightforward).

Left unattended, at the end of \(T\) years, the first bucket balloons to \(S^- = B(1+i)^T\), and the second bucket to \(S^+ = D [(1+i)^T - 1]/i\).

If we set things up just right (choose the correct D), the amount we owe (bucket 1), might be just about equal to the amount we have in bucket 2.

Mathematically, we can impose \(S^+ = S^-\), to obtain this "just right" monthly payment. Solving for \(D\), we get,\[D^* = B \frac{i}{1-(1+i)^{-T}}.\] Suppose \(B\) = 100,000, \(i\) = 0.05/12 (monthy interest rate), and \(T = 360\) (30 years), we get \(D^* = $536.82\).

Bucket 1
Bucket 2
Net of Bucket 1 and 2
If you used a higher D, then you would end up with a net excess, and vice versa if you used a smaller D.

Tuesday, October 28, 2014

Compound Interest, Loans, and Saving for Retirement

Suppose you borrow $B at time t=0 at an annual interest rate i. If you don't make any payments, the loan compounds, and at the end of T years ballons to \(B (1 + i)^T\).

As a concrete example, if you borrow B=$10,000 at 5% (= 0.05) for 10 years, your debt expands to nearly $16,289 at the end of the loan term, if you don't make any payments.

Nobody likes to ponder sad eventualities, so lets consider the more optimistic problem of saving for retirement. In a subsequent post, we will connect these two problems: irresponsible borrowing, and prudent saving, for how to responsibly pay down a home mortgage, for example.

Consider you make constant regular payments of $D to your retirement account every year from t=1 to t=T.

If we assume that our money grows at the same constant rate of i=0.05, we want to figure out how much we end up with after T years.

The first installment has (T-1) periods to grow over. So it grows to $\(D (1+i)^{T-1}\).

Similarly the second installment has (T-2) periods to grow over; so it grows to $\(D (1+i)^{T-2}\).

So on and so forth.

Thus, the total amount of money at the end of T years is given by,\[S^+ = $D(1+i)^{T-1} + $D (1+i)^{T-2} + ... + $D.\] This is a simple geometrical series, which conveniently sums up to,\[S^+ = $D \frac{(1+i)^T-1}{i}.\] To finish off this post, and set up the stage for how to responsibly pay down loans, let me plot the increase in the loan amount as a function of time (I am going to call the number \(S^-\)), and the growth of the retirement account.

Again, using B = 10,000, i = 0.05, and T = 10:

Using D = 100, i = 0.05, and T = 10:
In this case, we end up with $1,257.5, out of which only $1,000 were contributions, and the rest was compound interest.

Saturday, October 25, 2014

Setting up SpellCheck in TeXmaker

As the official documentation suggests, you go to "Configure Texmaker" -> "Editor" -> "Spelling dictionary" and set up the location.

The default location on my Linux distribution was: /usr/share/myspell/en_GB.dic

As the wikipedia entry says:
MySpell was the former spell checker included with OOo Writer of the free office suite.
The current spell checker of choice for OpenOffice is Hunspell. We can configure TeXmaker to use this dictionary by pointing to the location: /usr/share/hunspell/en_US.dic

Note, you can try the command locate hunspell to figure out where the "dic" file rests on your installation.

Sunday, October 19, 2014


1. Alcohol consumption in America (
The numbers in the survey surprised me on the downside. 30% of Americans don't drink. The median American has one drink every two weeks. The top decile consumes 10 drinks/day.
2. Is common Common-Core really bad?
You may have seen Facebook posts of frustrated parents (for e.g). However, I think the standards themselves are very decent. Some of the ideas might seem foreign, but I have to admit I am impressed by them. Here is coverage from the American RadioWorks and Intelligence Squared US Debates.
3. 15 Questions to see how logical you are. For example,
No introverts are charismatic
All philosophers are introverts
Therefore, no philosophers are charismatic

Friday, October 17, 2014

Education, Second Chances, and Transformations

For some random reason today, I was reminded of a story that happened when I just started my current academic position.

I was a very eager assistant professor teaching thermodynamics to undergraduates. I had not yet been bathed in the richness of personal struggles that many students dragged with them into the classroom.

So after my first class, this kid - let's call him MJ - walked into my office. He delivered what seemed like a prepared two-minute talk on how he was going to focus, work hard, and turn things around that semester.

After he left, I looked up his past grades, and found that he was struggling with a GPA of less than 2.5, and an academic history that was littered with Cs and Ds. 

I did not think much about the incident, until the semester had picked up some steam. MJ showed up during office hours regularly. Although he was rusty in multiple areas, his dogged effort was palpable.

Over time, I got to know a little more about the social structure in which he had been surrounded - an ugly milieu that was riddled with gangs, drugs, and far worse - apathy.

What MJ lacked in mathematical ability, he tried to make up with logic, unconventional thinking, and tenacity.

He had that "born-again" zeal you can sometimes see in people who are given second chances, who have determined that this is it. It is now or never!

Thanks to the compounding effect of knowledge, he started making rapid progress. Within a month, I realized that he really had a good shot at turning this thing around, and actually found myself rooting for him. I really wanted it to end well for him.

He finished the course with a well-deserved A. I found out later that he had gotten As in most of his other classes that semester.

I watched him land a well-paying job a year later.

I saw him once more after that, when he came to recruit for "his" company, and caught up with him. He was doing well, and he had dramatically changed his family's trajectory.

The lesson: saving or helping one individual might not make a big difference to the world, but it does make a world of difference to that one individual.

Tuesday, October 14, 2014

An underflow problem in slice sampling

In a MCMC seminar that Peter and I run, we were recently discussing slice sampling. Here is an overview from Radford Neal himself (open access). Let me skip the details of the algorithm, and pose the particular problem we encountered.

Problem: Compute the energy \(U^*\) of a system. We don't have any idea of the magnitude of this energy, or its sign. Suppose the probability of the system to be in that state is \(\exp(-U^*)\).

This number is going to be extremely small if \(U^*\) is large (which is probable, if you don't work hard at estimating a good initial guess).

The slice sampling algorithm asks us to draw a number \[y \sim U(0, \exp(-U^*)),\] and compute the difference \[\Delta U_y = -\log(y) - U^*.\]
Straightforward Implementation:

The following Octave program computes 10,000 samples of \(\Delta U_y\) to produce the histogram

% Small Ustar = 10, no problems
Us  = 10;
num = 10000;
y   = rand(num,1) * exp(-Us);
Uy  = -log(y);
dUy = Uy - Us;
[hx, x] = hist(dUy,25);
hx = hx/num;
bar(x, hx)
Histrogram of dUy with U*=10 is an exponential distribution
Unfortunately if I set Us = 1000 in the program above, it fails. The failure is simple to parse. If Us is large, exp(-Us) is below "realmin" in double precision, and is approximated to zero, which spawns a cascade of disastrous events, ending in the logarithm of zero.

There are multiple ways of fixing this problem.

Solution 1:

Since we are really interested in \(\Delta U_y\), we can draw it directly from the correct distribution. In this case, a little analysis suggests that the distribution of \(\Delta U_y\) is an exponential distribution with parameter = 1. Hence, we can skip the auxillary variables \(y\) and \(U_y\) and go directly to the meat of the problem:

Us  = 1000;
num = 10000;
dUy = exprnd(1,num,1);

Solution 2:

Since we observe that \(U^*\) doesn't participate in the distribution, we can think of another method that does not directly involve sampling from an exponential random number generator. We define \(y' \sim U(0,1),\) and \(y = y' * \exp(-U^*)\).

We can write \(-\log(y) = -\log(y') + U^*\).

This implies  that  \(\Delta U_y = -\log(y) - U^* = -\log y'\).


yp  = rand(num,1);
dUy = -log(yp);

also works well. The advantage is that one doesn't need an special exponential random number generator.

On my computer, solution 2 was more efficient than solution 1 for num < 50000, after which solution 1 became more efficient.

Thursday, October 9, 2014


1. How Bayesian thinking might explain how children build their worldview (Massimo Fuggetta).

2. Fuck Yeah Fluid Dynamics Tumblr Page

3. So you aren't scared of the Yellowstone supervolcano, huh?

4. What college has the most billionaires (Barry Ritholtz)?

5. Academic Humilty (Agabus)

Monday, October 6, 2014

Kahan or Compensated Summation

Wikipedia describes this as follows:
In numerical analysis, the Kahan summation algorithm (also known as compensated summation [1]) significantly reduces the numerical error in the total obtained by adding a sequence of finite precision floating point numbers, compared to the obvious approach. This is done by keeping a separate running compensation (a variable to accumulate small errors).
Here are a couple of links (postscript document) which argue that this relatively simple method ought to be better known.

An Octave program to sum \(s = x_1 + x_2 + ... + x_n\) is (embedded):