Author: Mikey Tabak, PhD
Company: Quantitative Science Consulting, LLC
Date: 19 December 2024
Website: www.QSC.earth
Annual Percentage Yield (APY) is a term we often see when comparing savings accounts, but it can be difficult to truly understand what it means for your money. How much will you actually earn in interest over time? How do compounding periods and additional deposits affect your savings?
To answer these questions, I’ve created a simple R function that allows you to calculate how your money will grow under various conditions. This function takes into account your initial deposit, the APY offered by your account, the number of compounding periods per year, and even allows you to model additional monthly deposits.
By using this function, you’ll gain clarity on the real-world impact of APY and be able to make more informed decisions about saving and investing.
Below is the R function that calculates your savings growth. You can copy and paste this function into R.
#' Calculate Account Value or Interest Accrued Based on APY
#'
#' This function calculates the total account value or the interest accrued over a specified
#' number of compounding periods based on the initial deposit, stated APY, number of compounding
#' periods per year, and optional additional monthly deposits.
#'
#' @param initial_deposit Numeric. The initial deposit amount (default is 1000).
#' @param stated_apy Numeric. The annual percentage yield as a decimal (e.g., 0.05 for 5% APY).
#' @param compounding_periods_per_year Integer. The number of compounding periods per year (e.g., 12 for monthly compounding).
#' @param additional_monthly_deposit Numeric. Optional. An additional deposit made each month (default is 0).
#' @param total_periods Integer. The total number of compounding periods to calculate over.
#' @param calculate_interest_only Logical. If `TRUE`, returns the interest accrued instead of the total account value (default is `FALSE`).
#'
#' @return Numeric. The total account value or the interest accrued based on the specified parameters.
#' @export
calculate_apy <- function(initial_deposit = 1000,
stated_apy = 0.04,
compounding_periods_per_year = 12,
additional_monthly_deposit = 0,
total_periods = 12,
calculate_interest_only = FALSE) {
# Calculate periodic interest rate
periodic_rate <- stated_apy / compounding_periods_per_year
# Initialize account value and interest accrued
account_value <- initial_deposit
total_interest_accrued <- 0
# Loop through each compounding period
for (i in 1:total_periods) {
# Calculate interest for the current period
interest_this_period <- account_value * periodic_rate
# Add interest to account value
account_value <- account_value + interest_this_period
# Add monthly deposit (converted to compounding period level if necessary)
if (i %% (compounding_periods_per_year / 12) == 0) {
account_value <- account_value + additional_monthly_deposit
}
# Accumulate interest
total_interest_accrued <- total_interest_accrued + interest_this_period
}
# Return interest or total account value
if (calculate_interest_only) {
return(total_interest_accrued)
} else {
return(account_value)
}
}
To illustrate how this function works, let’s look at a few examples. These examples show how to calculate both the total account value and the interest accrued under various scenarios.
Suppose you start with an initial deposit of $1,000 in a savings account offering a 5% APY. The interest is compounded monthly, and you want to see how much your account will grow in one year.
calculate_apy(
initial_deposit = 1000,
stated_apy = 0.05,
compounding_periods_per_year = 12,
total_periods = 12
)
## [1] 1051.162
After one year, your account value will be about $1,051.16. If you
run the same code with the option
calculate_interest_only = True
, it shows that you would
accrue $51.16 under these conditions.
Now let’s say you make the same initial deposit of $1,000, but you also decide to contribute $100 every month. How much will you have after one year?
calculate_apy(
initial_deposit = 1000,
stated_apy = 0.05,
compounding_periods_per_year = 12,
additional_monthly_deposit = 100,
total_periods = 12
)
## [1] 2279.047
With monthly contributions, your account will grow to approximately $2,279.05 after one year. This includes your initial deposit, additional deposits, and the interest earned.
Let’s say there’s a 5-year CD offering a 6% annual interest rate that you’re considering investing in. Investing $1,000 in that CD would return $338.23 after 5 years. Is it worth investing in the CD? Compare to how much you would earn in your current account with 5% APY:
calculate_apy(
initial_deposit = 1000,
stated_apy = 0.05,
compounding_periods_per_year = 12,
total_periods = 12 * 5, # 12 * 5 = 5 years worth of months
calculate_interest_only = TRUE
)
## [1] 283.3587
The standard banking account would give $283.36 in interest over that same time period. Maybe the extra interest isn’t worth investing in a CD under these conditions, but you can play with this given more realistic scenarios given the conditions of your account.
Understanding how APY affects your savings can help you make smarter financial decisions. By using this R function, you can experiment with different scenarios—such as increasing your monthly contributions or choosing accounts with higher APYs—and see the long-term impact on your savings.
Try out the function with your own inputs to see how to optimize your savings strategy.