Python
def mortgage_payment(principal, annual_rate, years):
    monthly_rate = annual_rate / 12 / 100
    num_payments = years * 12
    payment = principal * (monthly_rate * (1 + monthly_rate)**num_payments) / ((1 + monthly_rate)**num_payments - 1)
    return payment

def monthly_cost():
    hoa = 88
    property_price = 335000
    # Duhram rate:https://smartasset.com/taxes/north-carolina-property-tax-calculator#jUMf87tNBM
    anual_tax_rate = 1.03 / 100
    tax = property_price * anual_tax_rate / 12
    insurance = 70
    private_mortage_insurance = 0 # PMI, https://www.consumerfinance.gov/ask-cfpb/what-is-private-mortgage-insurance-en-122/
    return hoa + tax + insurance + private_mortage_insurance

def fixed_cost(principal, origination_rate, credit_rate):
    one_time_cost = 3778
    origination = principal * origination_rate / 100
    credit = principal * credit_rate / 100
    return one_time_cost + origination - credit
    
def monthly_inflow_cal(principal, fixed, avg_anual_rate):
    monthly_return  = avg_anual_rate / 12 / 100 * principal
    return monthly_return + fixed

loan_amount = 245000
loan_term_years = 30
monthly_rental = 1867.6
capital_anual_return_rate = 8 # Choose 401k return rate as a standard benchmark
# S&P 500 could be higher,  ytd 19.96%,
# https://www.marketwatch.com/investing/index/spx, the variance is quite high

# 5.990%, you pay  0.535% origination fee+ closing cost (~$3,778).
# 6.124%, you pay  0.084% origination fee+ closing cost (~$3,778)
# 6.250%, you pay  0.064% origination fee+ closing cost (~$3,778)
# 6.375%, you pay  zero origination fee+ closing cost (~$3,778). But the lender
# will credit you 0.262%
# 6.490%, you pay  zero origination fee + closing cost (~$3,778). But the lender
# will credit you 0.597%
# [interest_rate, origination_rate, credit_rate]
opt1 = [5.990, 0.535, 0]
opt2 = [6.124, 0.084, 0]
opt3 = [6.250, 0.064, 0]
opt4 = [6.375, 0, 0.262]
opt5 = [6.490, 0, 0.597]
opt_list = [opt1, opt2, opt3, opt4, opt5]
for opt in opt_list:
    interest_rate, origination_rate, credit_rate = opt
    monthly_payment = mortgage_payment(loan_amount, interest_rate, loan_term_years)
    monthly_extra_cost = monthly_cost()
    fixed = fixed_cost(loan_amount, origination_rate, credit_rate)
    monthly_outflow = monthly_payment + monthly_extra_cost
    monthly_inflow = monthly_inflow_cal(loan_amount, monthly_rental, capital_anual_return_rate)
    print(f"Interest rate: {interest_rate}%, Monthly payment: {monthly_payment}, Monthly cost: {monthly_extra_cost}, Once time cost: {fixed}, Monthly outflow: {monthly_outflow}, Monthly rental: {monthly_rental}, Monthly inflow: {monthly_inflow}, Monthly net: {monthly_inflow - monthly_outflow}")
Interest rate: 5.99%, Monthly payment: 1467.3240053771215, Monthly cost: 445.5416666666667, Once time cost: 5088.75, Monthly outflow: 1912.8656720437882, Monthly rental: 1867.6, Monthly inflow: 3500.9333333333334, Monthly net: 1588.0676612895452
Interest rate: 6.124%, Monthly payment: 1488.487390227665, Monthly cost: 445.5416666666667, Once time cost: 3983.8, Monthly outflow: 1934.0290568943317, Monthly rental: 1867.6, Monthly inflow: 3500.9333333333334, Monthly net: 1566.9042764390017
Interest rate: 6.25%, Monthly payment: 1508.5071410446665, Monthly cost: 445.5416666666667, Once time cost: 3934.8, Monthly outflow: 1954.0488077113332, Monthly rental: 1867.6, Monthly inflow: 3500.9333333333334, Monthly net: 1546.8845256220002
Interest rate: 6.375%, Monthly payment: 1528.4812521313931, Monthly cost: 445.5416666666667, Once time cost: 3136.1, Monthly outflow: 1974.02291879806, Monthly rental: 1867.6, Monthly inflow: 3500.9333333333334, Monthly net: 1526.9104145352735
Interest rate: 6.49%, Monthly payment: 1546.9557648549205, Monthly cost: 445.5416666666667, Once time cost: 2315.35, Monthly outflow: 1992.4974315215873, Monthly rental: 1867.6, Monthly inflow: 3500.9333333333334, Monthly net: 1508.4359018117461
Python