其他分享
首页 > 其他分享> > 贷款计算测试

贷款计算测试

作者:互联网

{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7bc249d7",
   "metadata": {},
   "source": [
    "# 贷款计算\n",
    "\n",
    "x = 月收入(元)\n",
    "\n",
    "y = 月公积金(元)\n",
    "\n",
    "z = 贷款金额(元)\n",
    "\n",
    "r = 商贷年利率(0.05)\n",
    "\n",
    "s = 商贷月利率(r/12)\n",
    "\n",
    "t = 贷款期数(360期)\n",
    "\n",
    "u = 通货膨胀(1.X)\n",
    "\n",
    "v = 月通货膨胀u**(1/12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17c0a05a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def 等额本金(贷款金额,贷款期数,商贷年利率,第几个月):\n",
    "    每月应还本金=贷款金额/贷款期数\n",
    "    每月利息 = (贷款金额-每月应还本金*(第几个月-1))*(商贷年利率/12)\n",
    "    return (每月应还本金,每月利息)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c22ed801",
   "metadata": {},
   "outputs": [],
   "source": [
    "def 等额本息(贷款金额,贷款期数,商贷年利率,第几个月):\n",
    "    商贷月利率=商贷年利率/12\n",
    "    月均还款本金和利息 = 贷款金额*商贷月利率*pow((1+商贷月利率),贷款期数)/(pow((1+商贷月利率),贷款期数)-1)\n",
    "    还款利息总和=贷款期数*贷款金额*商贷月利率*pow((1+商贷月利率),贷款期数)/(pow((1+商贷月利率),贷款期数)-1)-贷款金额\n",
    "    第一个月还款利息=贷款金额*商贷月利率\n",
    "    剩余利息=还款利息总和-第一个月还款利息\n",
    "    剩余本金=贷款金额-(月均还款本金和利息-第一个月还款利息)\n",
    "    if 第几个月==1:\n",
    "        return (月均还款本金和利息-第一个月还款利息,第一个月还款利息)\n",
    "    第几个月利息 = (贷款金额*商贷月利率-月均还款本金和利息)*pow((1+商贷月利率),(第几个月-1))+月均还款本金和利息\n",
    "    第几个月本金 = 月均还款本金和利息-第几个月利息\n",
    "    return (第几个月本金,第几个月利息)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83ef3204",
   "metadata": {},
   "outputs": [],
   "source": [
    "def 等额本金结余(贷款时月收入,贷款时公积金,通货膨胀,贷款金额,贷款期数,商贷年利率,第几个月):\n",
    "    当月收入=贷款时月收入*(通货膨胀**(1/12))**第几个月\n",
    "    当月公积金=贷款时公积金*(通货膨胀**(1/12))**第几个月\n",
    "    (本金,利息)=等额本金(贷款金额,贷款期数,商贷年利率,第几个月)\n",
    "    当月应还=本金+利息\n",
    "    当月现金结余=当月收入\n",
    "    当月公积金结余=当月公积金\n",
    "    if 当月应还>当月公积金:\n",
    "        当月公积金结余=0\n",
    "        当月现金结余=当月收入-(当月应还-当月公积金)\n",
    "        return (当月现金结余,当月公积金结余)\n",
    "    return (当月现金结余,当月公积金-当月应还)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03983f02",
   "metadata": {},
   "outputs": [],
   "source": [
    "def 等额本息结余(贷款时月收入,贷款时公积金,通货膨胀,贷款金额,贷款期数,商贷年利率,第几个月):\n",
    "    当月收入=贷款时月收入*(通货膨胀**(1/12))**第几个月\n",
    "    当月公积金=贷款时公积金*(通货膨胀**(1/12))**第几个月\n",
    "    (本金,利息)=等额本息(贷款金额,贷款期数,商贷年利率,第几个月)\n",
    "    当月应还=本金+利息\n",
    "    当月现金结余=当月收入\n",
    "    当月公积金结余=当月公积金\n",
    "    if 当月应还>当月公积金:\n",
    "        当月公积金结余=0\n",
    "        当月现金结余=当月收入-(当月应还-当月公积金)\n",
    "        return (当月现金结余,当月公积金结余)\n",
    "    return (当月现金结余,当月公积金-当月应还)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59f14e31",
   "metadata": {},
   "outputs": [],
   "source": [
    "现金结余=0\n",
    "公积金结余=0\n",
    "for m in range(1,360):\n",
    "    (a,b)=等额本金结余(贷款时月收入,贷款时公积金,通货膨胀,贷款金额,贷款期数,商贷年利率,m)\n",
    "    现金结余+=a\n",
    "    公积金结余+=b\n",
    "print(现金结余)\n",
    "print(公积金结余)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9d6b7a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "现金结余=0\n",
    "公积金结余=0\n",
    "for m in range(1,360):\n",
    "    (a,b)=等额本息结余(贷款时月收入,贷款时公积金,通货膨胀,贷款金额,贷款期数,商贷年利率,m)\n",
    "    现金结余+=a\n",
    "    公积金结余+=b\n",
    "print(现金结余)\n",
    "print(公积金结余)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24cd47b7",
   "metadata": {},
   "source": [
    "# 车贷转等额本金房贷利率计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4ca0567",
   "metadata": {},
   "source": [
    "# 车贷利息=车贷*年利率*年数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7785d5c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def 车贷转等额本金房贷利率换算(车贷金额,年利率,贷款年期限):\n",
    "    每月应还本金=车贷金额/(贷款年期限*12)\n",
    "    商贷年利率 = symbols('商贷年利率')\n",
    "    车贷利息 = 车贷金额*年利率*贷款年期限\n",
    "    总利息=0\n",
    "    for 第几个月 in range(1,贷款年期限*12):\n",
    "        每月利息 = (车贷金额-每月应还本金*(第几个月-1))*(商贷年利率/12)\n",
    "        总利息+=每月利息\n",
    "    y=solve(总利息-车贷利息,商贷年利率)\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "690343d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# def 车贷转等额本息房贷利率换算(车贷金额,年利率,贷款年期限):\n",
    "#     商贷年利率 = symbols('商贷年利率')\n",
    "#     商贷月利率=商贷年利率/12\n",
    "#     贷款期数=贷款年期限*12\n",
    "#     还款利息总和=贷款期数*车贷金额*商贷月利率*pow((1+商贷月利率),贷款期数)/(pow((1+商贷月利率),贷款期数)-1)-车贷金额\n",
    "#     车贷利息 = 车贷金额*年利率*贷款年期限\n",
    "#     y=solve(还款利息总和-车贷利息,商贷年利率)\n",
    "#     return y\n",
    "def 车贷转等额本息房贷利率换算(车贷金额,年利率,贷款年期限):\n",
    "    商贷年利率 = symbols('商贷年利率')\n",
    "    商贷月利率=商贷年利率/12\n",
    "    贷款期数=贷款年期限*12\n",
    "    还款利息总和=车贷金额*商贷月利率*(贷款期数+1)/2\n",
    "    车贷利息 = 车贷金额*年利率*贷款年期限\n",
    "    y=solve(还款利息总和-车贷利息,商贷年利率)\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06732079",
   "metadata": {},
   "outputs": [],
   "source": [
    "车贷转等额本金房贷利率换算(150000,0.03,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "858a9b72",
   "metadata": {},
   "outputs": [],
   "source": [
    "车贷转等额本息房贷利率换算(150000,0.03,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d84be77",
   "metadata": {},
   "source": [
    "# Sympy 用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d9e7c6f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ce6e43a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=symbols('x')\n",
    "a=symbols('a')\n",
    "b=symbols('b')\n",
    "c=symbols('c')\n",
    "y=a*x**2+b*x+c\n",
    "z=solve(y,x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "5e917926",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\left[ \\frac{- b - \\sqrt{- 4 a c + b^{2}}}{2 a}, \\  \\frac{- b + \\sqrt{- 4 a c + b^{2}}}{2 a}\\right]\n"
     ]
    }
   ],
   "source": [
    "print(latex(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d113224f",
   "metadata": {},
   "source": [
    "$\\left[ \\frac{- b - \\sqrt{- 4 a c + b^{2}}}{2 a}, \\  \\frac{- b + \\sqrt{- 4 a c + b^{2}}}{2 a}\\right]$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2781c36a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\left[ \\frac{- b - \\sqrt{- 4 a c + b^{2}}}{2 a}, \\  \\frac{- b + \\sqrt{- 4 a c + b^{2}}}{2 a}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import Latex\n",
    "Latex('$'+latex(z)+'$')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "c873294c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle -1.0$"
      ],
      "text/plain": [
       "-1.00000000000000"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z[0].evalf(subs={a:1,b:2,c:1})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

 

标签:结余,测试,商贷,车贷,当月,计算,贷款,年利率
来源: https://www.cnblogs.com/yzpopulation/p/16076565.html