我也找AI帮忙写了个存401k/Roth401k比例的小蟒蛇

本帖于 2024-01-12 08:34:09 时间, 由普通用户 CatcherInTheRye 编辑

然后稍修改了下。自己要手动算这个挺麻烦。

假定你公司的401k计划是最多match到6% of your salary based on your contribution.

目标是 (1)最大化拿到公司的match (2)之后最大化Roth contribution

跑程序时输入 (1)公司match的比例,一般是50%或100%,(2)你的年薪

def calculate_401k_contributions(annual_salary, company_match_percentage=0.5, max_company_match_percentage=0.06, total_401k_limit=23000):
    max_pre_tax_contribution = annual_salary * max_company_match_percentage
    max_pre_tax_contribution = min(total_401k_limit, max_pre_tax_contribution)
    company_match = max_pre_tax_contribution * company_match_percentage
    remaining_pre_tax_limit = total_401k_limit - company_match
    remaining_pre_tax_limit = max(0, remaining_pre_tax_limit)
    roth_401k_contribution = min(remaining_pre_tax_limit, total_401k_limit - max_pre_tax_contribution)
    pre_tax_contribution_to_maximize_match = min(max_pre_tax_contribution, total_401k_limit - roth_401k_contribution)
    remaining_pre_tax_contribution = max_pre_tax_contribution - pre_tax_contribution_to_maximize_match
    additional_roth_401k_contribution = min(remaining_pre_tax_contribution, roth_401k_contribution)
    pre_tax_contribution = pre_tax_contribution_to_maximize_match + additional_roth_401k_contribution
    return pre_tax_contribution, roth_401k_contribution

# usage
company_match_percentage = input("Enter your company 401k match % (e.g. 50%): ")
if company_match_percentage.isdigit():
    company_match_percentage = float(company_match_percentage)
    if company_match_percentage > 1:
        company_match_percentage = company_match_percentage / 100
else:
    company_match_percentage = float(company_match_percentage.strip('%')) / 100.0
annual_salary = float(input("Enter your annual salary: "))
pre_tax, roth = calculate_401k_contributions(annual_salary, company_match_percentage)

print(f"Annual Salary: ${annual_salary}")
print(f"Pre-tax 401k Contribution: ${pre_tax:.2f}")
print(f"Roth 401k Contribution: ${roth:.2f}")

所有跟帖: 

可以粘贴到这里run -CatcherInTheRye- 给 CatcherInTheRye 发送悄悄话 (56 bytes) () 01/12/2024 postreply 08:26:36

3 -dancinghorse- 给 dancinghorse 发送悄悄话 (0 bytes) () 01/12/2024 postreply 08:43:06

人家这是用做私人用途 分享给大家而已 不存在损失不损失损失。 很多人也不知道怎么用的。 -nilabonita- 给 nilabonita 发送悄悄话 (117 bytes) () 01/12/2024 postreply 09:01:24

是的,就这小程序我自己还是花了30分钟测试修改 -CatcherInTheRye- 给 CatcherInTheRye 发送悄悄话 (146 bytes) () 01/12/2024 postreply 09:04:17

你这个是一维计算,做二维计算也是可行的,比如图像处理,做三维计算漏洞会很大,就算芯片再快 -dancinghorse- 给 dancinghorse 发送悄悄话 (0 bytes) () 01/12/2024 postreply 10:12:04

说明AI自动驾驶还任重道远还不能实用? -CatcherInTheRye- 给 CatcherInTheRye 发送悄悄话 (0 bytes) () 01/12/2024 postreply 10:52:26

AI 写程序必须提供反馈,你水平高他也会写的更好,不然他会糊弄你。 -va168- 给 va168 发送悄悄话 va168 的博客首页 (0 bytes) () 01/12/2024 postreply 09:49:22

嗯,garbage in garbage out -CatcherInTheRye- 给 CatcherInTheRye 发送悄悄话 (0 bytes) () 01/12/2024 postreply 10:50:40

为什么不是全部23000到Roth 401? -NYU1065- 给 NYU1065 发送悄悄话 (38 bytes) () 01/13/2024 postreply 07:36:22

请您先登陆,再发跟帖!