Skip to content

Instantly share code, notes, and snippets.

@trinhvanminh
Last active August 14, 2024 15:17
Show Gist options
  • Save trinhvanminh/948b84ccdaa80f04ff7bbad591efcf31 to your computer and use it in GitHub Desktop.
Save trinhvanminh/948b84ccdaa80f04ff7bbad591efcf31 to your computer and use it in GitHub Desktop.
Tính lãi suất kép
import matplotlib.pyplot as plt
lai_suat_theo_ky_han = [
{
"ky_han": 1,
"lai_suat": 0.037
},
{
"ky_han": 2,
"lai_suat": 0.038
},
{
"ky_han": 3,
"lai_suat": 0.039
},
{
"ky_han": 5,
"lai_suat": 0.043
},
{
"ky_han": 6,
"lai_suat": 0.049
},
{
"ky_han": 12,
"lai_suat": 0.052
},
{
"ky_han": 18,
"lai_suat": 0.054
},
{
"ky_han": 21,
"lai_suat": 0.055
},
{
"ky_han": 24,
"lai_suat": 0.056
},
{
"ky_han": 36,
"lai_suat": 0.058
},
]
def tinh_lai_kep(tien_goc: float | int, lai_suat: float, ky_han: int, thoi_gian_gui: int):
"""
tien_goc: so tien gui
lai_suat: percentage (eg: 0.037)
ky_han (thang)
thoi_gian_gui (thang)
"""
if thoi_gian_gui < ky_han:
return tien_goc
return tien_goc*(1 + lai_suat*ky_han/12)**(thoi_gian_gui/ky_han)
TIEN_GOC = 60
TOTAL_MONTHS = 36
THOI_GIAN_GUI = range(1, TOTAL_MONTHS + 1)
fig, ax = plt.subplots()
for item in lai_suat_theo_ky_han:
label = f"Kỳ hạn: {item['ky_han']
} thang ({round(item['lai_suat']*100, 2)}%)"
danh_sach_lai_kep = []
for thoi_gian_gui in THOI_GIAN_GUI:
lai_kep = tinh_lai_kep(
TIEN_GOC,
item["lai_suat"],
item["ky_han"],
thoi_gian_gui
)
danh_sach_lai_kep.append(round(lai_kep, 2))
print(label)
print(danh_sach_lai_kep)
ax.plot(THOI_GIAN_GUI, danh_sach_lai_kep, label=label)
ax.legend()
ax.set_xlabel('Thời gian gửi')
ax.set_ylabel('Số tiền lãi')
plt.xticks(range(TOTAL_MONTHS + 1))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment