Skip to content

Instantly share code, notes, and snippets.

@koss822
Last active January 12, 2024 18:58
Show Gist options
  • Save koss822/85382a91d107aa2bc6ac4ea7da86be24 to your computer and use it in GitHub Desktop.
Save koss822/85382a91d107aa2bc6ac4ea7da86be24 to your computer and use it in GitHub Desktop.
# calculate and test continuous rent when taking money, also calculate interest rate on financial market
# with help of chatgpt
# Modifying the code to account for a 4% annual increase in the annuity,
# distributed as a monthly increase, and testing the outcome
def find_monthly_annuity_with_monthly_increase(starting_amount, monthly_interest_rate, total_months, annual_increase_rate):
lower_bound = 0
upper_bound = starting_amount # Increased upper bound to include all possibilities
tolerance = 0.01 # Tolerance limit for calculation accuracy
monthly_increase_rate = (1 + annual_increase_rate) ** (1/12) - 1 # Calculating the equivalent monthly increase rate
while lower_bound <= upper_bound:
tested_annuity = (lower_bound + upper_bound) / 2
remaining_balance = starting_amount
current_annuity = tested_annuity
for month in range(total_months):
remaining_balance -= current_annuity
remaining_balance *= (1 + monthly_interest_rate)
current_annuity *= (1 + monthly_increase_rate) # Monthly increase in annuity
if abs(remaining_balance) < tolerance:
return tested_annuity # Found the correct initial monthly annuity
elif remaining_balance > 0:
lower_bound = tested_annuity + tolerance # Increase lower bound
else:
upper_bound = tested_annuity - tolerance # Decrease upper bound
return tested_annuity # Returning the best found annuity
# Recalculating the monthly annuity with a monthly distributed 4% annual increase
monthly_annuity_with_increase = find_monthly_annuity_with_monthly_increase(starting_amount=1000000, monthly_interest_rate=0.08 / 12, total_months=10 * 12, annual_increase_rate=0.04)
# Testing the adjusted annuity payment
remaining_balance = 1000000 # Starting amount
current_annuity = monthly_annuity_with_increase
for month in range(10 * 12):
remaining_balance -= current_annuity
remaining_balance *= (1 + 0.08 / 12)
current_annuity *= (1 + (1 + 0.04) ** (1/12) - 1) # Monthly increase in annuity
monthly_annuity_with_increase, remaining_balance
# Result
# (10117.982961110774, 1.9948362214936544)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment