Gives two reasons for this: The sacrifice of the interest you’ve paid and the front-loading of the interest you’re going to pay.

Explains when refinancing might be a good idea and when it’s a bad one.

Author's update, May 14, 2019: After some misunderstanding surfaced in the comments section, I'd like to clarify that my thesis and analysis here center around the fact that many people refinance their mortgages without shortening the term of the loan, so that a ten-year mortgage, refinanced after two years, becomes, in effect, a twelve-year mortgage.

Let’s say you got a ten-year mortgage at 4% on a balance of $250,000 after down payments and costs. Now it’s two years later and you want to refinance at 3.5%. Your balance is $207,652 and you’ve paid $18,399 in interest. If you don’t refinance, you’ll have to pay an additional $35,336 in interest over the next eight years. If you do refinance at 3.5% on that $207,652, however, you’ll have to pay $38,754 in interest over the next ten years, an increase of $3,418. And then there are all the refinancing costs. By getting a significantly lower rate, you’ll be significantly out of pocket.

Now let’s say you have a thirty-year mortgage at 3.5% and after ten years you’re offered a refinance at 2.625%, also for thirty years. That’s a reduction of 0.875%, or 25% of your initial rate. How can you refuse?

Well, let’s say the initial mortgage was for $300,000. Your balance is now $231,611 and you’ve paid $94,612 in interest. If you keep your mortgage, you’ll pay an additional $90,360 in interest. If you refinance your current balance at that significantly lower rate, however, you’ll pay $103,289 in interest over the next thirty years, an increase of $12,929, plus all the refinancing costs. (And those refinancing costs are typically added to your principal, meaning you have to pay interest on them too.) Your monthly payments will go down some, but you’ll be paying them for an additional ten years. The math is decidedly not in your favor.

How is it possible to pay more interest on a lower rate?

Well, there are two reasons for the increase.

The first is that when you refinance, all your interest payments up to now are ignored. They’re gone - zip! - as if you’d never paid a cent. You’ve lost every penny that you’ve paid between the initial mortgage payment and now, except for the amount you’ve paid on the principal.

The second lies in how amortization works, and it’s significantly more complicated than the first.

You start by figuring out your monthly payment, which is determined by the amount of the loan divided by the discount factor, and the discount factor is determined by the interest rate and the number of periodic payments. If i is the interest rate per month (the annual interest rate divided by 12) and n is the number of payments (for a thirty-year loan, it’s 360), the formula for the discount rate is:

What does this formula do? Well, for a thirty-year mortgage, it effectively takes the annual interest rate and multiplies it by 0.69. In other words, if you’re getting an interest rate of 5%, you’re actually paying only an annual rate of 3.11% if you apply it to the entirety of the loan. (This is a rough approximation reached by dividing 1 by the discount rate, subtracting 1/360 for principal, and multiplying that by 12.) This is because the stated loan rate is applied only to the remaining principal, and as that principal gets reduced, the actual interest paid goes down.

Because you’re paying a fixed amount each month, the ratio of interest to principal in each payment will steadily diminish throughout the life of the loan. So at the beginning of the mortgage, you’ll be paying a lot more interest than halfway through the mortgage, and near the end of the mortgage almost all of your monthly payments will consist of the principal.

The precise formula isn’t terribly complicated. For the first month, apply your monthly interest rate to the entire amount of the loan, and that’s the amount of your monthly payment devoted to interest. The rest of the payment is devoted to principal. For the second month, apply your monthly interest rate to the lowered principal amount (lowered by the amount you paid in the first month), and then subtract that from your monthly payment to figure out the amount of principal you’re paying. And so on.

When you’re ten years into the loan process (assuming you have a thirty-year loan), the amount of your monthly payment devoted to interest is going to be a lot lower than when you started. Here’s a chart showing how the amount of interest per payment decreases over the life of the loan (assuming you start by paying about $500 per month in interest).

But when you refinance, the whole process begins over again. Each time you refinance, you’ll be front-loading your interest payments anew.

The idea behind refinancing with a lower rate is to reduce your interest payments. That’s what a lower rate implies, and that’s how the mortgage companies package their offers. But that’s not what actually happens.

Now sometimes it is a good idea to refinance. Sometimes the total interest you’re going to pay plus the refinancing costs are less than the amount of interest you have left to pay on your original loan. This can happen if the interest rate you’re being offered is significantly lower and/or if the period of the new loan is significantly shorter; it helps if you haven’t paid much interest yet because your existing loan is quite recent. But the mortgage companies may not tell you that, and it might be up to you to do the math. And the farther along you get into your mortgage, the harder it’ll be to find a refinancing rate that will make it cheaper to make the switch.

There’s a very easy online way to find out if refinancing your mortgage is going to save you money, and that’s to visit the Mortgage Refinance Calculator. It’s a terrific free service provided by NerdWallet, and it takes into account just about everything you want to take into account. You can adjust it according to your estimated closing costs and you can add cash out. But just remember: a lower rate doesn’t necessarily mean a better deal.

I must add, however, that if your monthly payments go down and you put every penny you save on those monthly payments into a wise stock-market investment strategy, or if you get a cash-out refinance that you can invest profitably, paying the extra interest may well be a very good idea. But look into the alternatives first. You may well be better off with a second mortgage or a HELOC than a cash-out refi. And, as I’ve explained in another article, using personal debt for investing is a bad idea compared to investing the money you have in companies that use debt wisely.

Disclosure:I/we have no positions in any stocks mentioned, and no plans to initiate any positions within the next 72 hours.I wrote this article myself, and it expresses my own opinions. I am not receiving compensation for it. I have no business relationship with any company whose stock is mentioned in this article.