Skip to content

Instantly share code, notes, and snippets.

@jawad360
Last active September 1, 2025 17:59
Show Gist options
  • Save jawad360/40787959b02a19779c105f19439e05b7 to your computer and use it in GitHub Desktop.
Save jawad360/40787959b02a19779c105f19439e05b7 to your computer and use it in GitHub Desktop.
# Function to simulate the scenario with investment coming from the original principal and annual interest on the investment
def simulate_investment(principal, withdraw_amount, invest_amount, no_of_years, annual_interest_rate=0.12, annual_withdrawal_increase=0.10):
# Initialize variables
total_investment = 0 # Amount in investments (compounded annually)
current_principal = principal # Starting amount
current_withdraw_amount = withdraw_amount # Monthly withdrawal amount
months_in_year = 12 # Number of months in a year
months = no_of_years * 12 # Total number of months
# Iterate through each month
for month in range(1, months + 1):
# Withdraw from principal first, if there is any left
if current_principal > 0:
if current_principal >= current_withdraw_amount:
current_principal -= current_withdraw_amount # Withdraw from principal
else:
# Withdraw remaining principal and continue from investment
current_withdraw_amount -= current_principal
current_principal = 0
# Once the principal is exhausted, withdraw from investments
if current_principal <= 0:
if total_investment >= current_withdraw_amount:
total_investment -= current_withdraw_amount # Withdraw from investment
else:
# If investment is insufficient, withdraw whatever is available
current_withdraw_amount -= total_investment
total_investment = 0
# Invest the invest_amount from principal
if current_principal > 0:
current_principal -= invest_amount # Deduct the investment amount from principal
total_investment += invest_amount # Add the new investment
# Print the status of principal and investment every month
print(f"Month {month}: Principal = ${current_principal:,.2f}, Total Investment = ${total_investment:,.2f}")
# At the end of each year (every 12 months), apply annual increase and increase withdrawal amount by 5%
if month % months_in_year == 0:
# Apply annual 12% increase to the investment at the end of each year
total_investment *= (1 + annual_interest_rate) # Investment grows by 12% annually
# Increase withdrawal amount after one year
current_withdraw_amount *= (1 + annual_withdrawal_increase)
# Return the final total amount (remaining principal + total investment)
return current_principal + total_investment
# Example usage
if __name__ == "__main__":
# Inputs
principal = 2000000 # Example: Starting principal amount
withdraw_amount = 10000 # Example: Initial monthly withdrawal amount
invest_amount = 40000 # Example: Monthly investment amount (taken from the principal)
no_of_years = 50 # Example: Duration in years
# Simulate the scenario
final_amount = simulate_investment(
principal, withdraw_amount, invest_amount, no_of_years
)
# Output the final amount (remaining principal + total investment)
print(f"\nFinal amount (remaining principal + total investment): ${final_amount:,.2f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment