{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "0270_prestamos.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyNdbXS/1ar92ZwFeYoboFk0",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"source": [
"# Préstamos\n",
"## Función Pago (pmt)\n",
"* npf.pmt(rate, nper, pv, fv, when = ‘end’)\n",
"* fv es optativo, por defecto es 0\n",
"* when es optativo, por defecto es 'end', pagos a final de periodo, pospagable\n",
"* Calcular el pago mensual periódico necesario para amortizar un préstamo de 350.000 €, a 20 años, al 6% TIN (Tipo de Interés Nominal)\n",
"* con Excel: =PAGO(6%/12;20*12;-350000)"
],
"metadata": {
"id": "LD16NjKFRYL9"
}
},
{
"cell_type": "code",
"source": [
"!pip install numpy-financial # instalamos con pip la librería financiera"
],
"metadata": {
"id": "mvvo9Yj4Jtcj",
"outputId": "058ed5ef-2756-4733-c94b-8d4945433d20",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Collecting numpy-financial\n",
" Downloading numpy_financial-1.0.0-py3-none-any.whl (14 kB)\n",
"Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.10/dist-packages (from numpy-financial) (1.23.5)\n",
"Installing collected packages: numpy-financial\n",
"Successfully installed numpy-financial-1.0.0\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import numpy_financial as npf\n",
"\n",
"pago = npf.pmt(0.06/12, 20 * 12, 350_000)\n",
"\n",
"# Here fv = 0 ; Also Default value of fv = 0\n",
"print(f\"Pago mensual: {-pago:,.2f} €\" )"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rnlAao1HP2IN",
"outputId": "4eb4abb3-b0f9-4eda-9c98-f291acc4c173"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Pago mensual: 2,507.51 €\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Cuadro de amortización de un préstamo\n",
"Préstamo francés de 100.000 euros, a devolver en 10 años, al 5% anual."
],
"metadata": {
"id": "cyJ7l8jz0X3C"
}
},
{
"cell_type": "code",
"source": [
"import numpy_financial as npf\n",
"import numpy as np\n",
"import tabulate as tab\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# en primer lugar vamos a calcular la cuota constante\n",
"\n",
"C0 = 100000\n",
"n = 10\n",
"tasa = 0.05\n",
"\n",
"pago = npf.pmt(rate=tasa, nper=n, pv=-C0, fv=0, when='end')\n",
"print(pago)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vq_37Go00KzZ",
"outputId": "a6f109b6-982c-4ff1-e426-02052d6aec84"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"12950.457496545661\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Vamos a calcular el capital pendiente de cada periodo\n",
"\n",
"pendiente = npf.fv(rate=tasa, nper =range(n+1), pmt = pago, pv= -C0)\n",
"print(pendiente)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "drPVFcwx1gfh",
"outputId": "27f2d904-a922-410c-9306-f79d721b6ec0"
},
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[100000. 92049.54250345 83701.56213208 74936.18274214\n",
" 65732.5343827 56068.70360529 45921.68128901 35267.30785691\n",
" 24080.21575321 12333.76904433 -0. ]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Calculamos la amortización de cada año\n",
"\n",
"amortz = npf.ppmt(rate=tasa, per=range(n+1), nper=n, pv=-C0, fv=0, when='end')\n",
"print(amortz)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "H9Xt-asI1hQB",
"outputId": "a26d89ec-195c-47aa-fa3c-9278664ed8d3"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[ 7571.86428242 7950.45749655 8347.98037137 8765.37938994\n",
" 9203.64835944 9663.83077741 10147.02231628 10654.3734321\n",
" 11187.0921037 11746.44670888 12333.76904433]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Calculamos los intereses de cada periodo\n",
"\n",
"interes = npf.ipmt(rate=tasa, per=range(n+1), nper=n, pv=-C0, fv=0)\n",
"print(interes)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xdxxQESp1j_2",
"outputId": "9c0e3f69-6350-433f-c8ac-05e52a0eaae2"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[5378.59321412 5000. 4602.47712517 4185.0781066 3746.80913711\n",
" 3286.62671914 2803.43518026 2296.08406445 1763.36539285 1204.01078766\n",
" 616.68845222]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Comprobamos\n",
"\n",
"comprob = np.array(amortz + interes)\n",
"print(comprob)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZJCY9WWL1o2y",
"outputId": "a25097d4-82c5-4fe7-e709-9b6c97c906be"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[12950.45749655 12950.45749655 12950.45749655 12950.45749655\n",
" 12950.45749655 12950.45749655 12950.45749655 12950.45749655\n",
" 12950.45749655 12950.45749655 12950.45749655]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Hacemos un cuadro de amortización para ver los datos con la librería tabulate\n",
"datos =[]\n",
"saldo = C0\n",
"saldo2 = 0\n",
"linea1 = [0,0,0,0,C0,0]\n",
"datos.append(linea1)\n",
"\n",
"Anualidad = npf.pmt(rate=tasa, nper=n, pv=-C0, fv=0, when='end')\n",
"\n",
"for i in range(1, n+1):\n",
" pago_capital = npf.ppmt(rate=tasa, per=i, nper=n, pv=-C0, fv=0, when='end')\n",
" pago_int = Anualidad - pago_capital\n",
" saldo -= pago_capital\n",
" saldo2 += pago_capital\n",
"\n",
" linea = [i, format(Anualidad, '.0f'), format(pago_int, '.0f'), format(pago_capital, '.0f'), format(saldo, '.0f'), format(saldo2, '.0f')]\n",
"\n",
" datos.append(linea)\n",
"\n",
"print(tab.tabulate(datos, headers= ['Periodo', 'Anualidad', 'Intereses', 'Amortización','Capital Vivo','Capital Amortizado'], tablefmt = 'psql'))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_QvkiPUm1scP",
"outputId": "2d5ee5bb-1fe8-4c97-8223-7fd3b445a27d"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"+-----------+-------------+-------------+----------------+----------------+----------------------+\n",
"| Periodo | Anualidad | Intereses | Amortización | Capital Vivo | Capital Amortizado |\n",
"|-----------+-------------+-------------+----------------+----------------+----------------------|\n",
"| 0 | 0 | 0 | 0 | 100000 | 0 |\n",
"| 1 | 12950 | 5000 | 7950 | 92050 | 7950 |\n",
"| 2 | 12950 | 4602 | 8348 | 83702 | 16298 |\n",
"| 3 | 12950 | 4185 | 8765 | 74936 | 25064 |\n",
"| 4 | 12950 | 3747 | 9204 | 65733 | 34267 |\n",
"| 5 | 12950 | 3287 | 9664 | 56069 | 43931 |\n",
"| 6 | 12950 | 2803 | 10147 | 45922 | 54078 |\n",
"| 7 | 12950 | 2296 | 10654 | 35267 | 64733 |\n",
"| 8 | 12950 | 1763 | 11187 | 24080 | 75920 |\n",
"| 9 | 12950 | 1204 | 11746 | 12334 | 87666 |\n",
"| 10 | 12950 | 617 | 12334 | 0 | 100000 |\n",
"+-----------+-------------+-------------+----------------+----------------+----------------------+\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Comprobemos como evoluciona nuestro prestamo en la siguiente gráfica\n",
"\n",
"plt.figure(figsize = (12, 8))\n",
"plt.bar(range(0, n + 1), pendiente)\n",
"plt.title(\"Capital Pendiente\", fontsize = 15)\n",
"plt.xlabel(\"Periodos(años)\", fontsize = 12)\n",
"plt.ylabel(\"Balance del préstamo (euros)\", fontsize = 12)\n",
"plt.plot()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 741
},
"id": "88V5kf4b1vMJ",
"outputId": "01e11132-6495-44a4-939e-10467545d964"
},
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[]"
]
},
"metadata": {},
"execution_count": 9
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"