{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Prezzi e quantità:\n", " Q1 P1 Q2 P2 Q3 P3 Q4 P4\n", "2013-12-31 802 17 373 14 438 10 1097 18\n", "2014-12-31 505 19 541 19 1620 13 1221 12\n", "2015-12-31 1036 10 1167 15 1728 16 669 19\n", "2016-12-31 1871 17 1321 14 524 18 974 19\n", "2017-12-31 1446 14 1426 11 183 17 1778 14\n", "\n", "Risultati:\n", " PIL PIL b/2014 Crescita PIL Crescita PIL b/2014 Deflatore\n", "2013-12-31 42982 41183 [] [] 1.04\n", "2014-12-31 55586 55586 0.29 0.35 1.00\n", "2015-12-31 68224 72349 0.23 0.3 0.94\n", "2016-12-31 78239 79148 0.15 0.094 0.99\n", "2017-12-31 63933 78283 -0.18 -0.011 0.82\n" ] } ], "source": [ "import pandas as pd\n", "pd.set_option('precision',2) #number of decimals\n", "import numpy as np\n", "\"\"\"\n", "Create basic table for PIL exercises\n", "\"\"\"\n", "#parametri:\n", "T=5 #number of years\n", "t_0='2013' #initial period\n", "N=4 #number of goods\n", "\n", "#index (rows)\n", "years=pd.date_range(t_0, periods=T,freq='Y')\n", "#quantità\n", "q=np.random.randint(100,2001, size=(T, N))\n", "#prezzi\n", "p=np.random.randint(10,20, size=(T, N))\n", "#matrice prezzi e quantità\n", "D=np.zeros((T,2*N))\n", "for n in range(N):\n", " D[:,2*n]=q[:,n]\n", "for n in range(N):\n", " D[:,2*n+1]=p[:,n]\n", "D=D.astype(int)\n", "#print(D)\n", "\n", "#colonne: quantità e prezzi\n", "PQ=[None]*2*N\n", "for n in range(N):\n", " PQ[2*n]='Q%s'%(n+1)\n", "for n in range(N):\n", " PQ[2*n+1]='P%s'%(n+1)\n", "\n", "data=pd.DataFrame(D, index=years, columns=PQ)\n", "\n", "#print dati problema\n", "print('Prezzi e quantità:')\n", "print(data)\n", "\n", "#write to latex table\n", "#then \"input\" as child from Lyx - with \\usepackage{booktabs} in preamble\n", "with open('BasicPIL-data-B.tex','w') as f1:\n", " f1.write(data.to_latex())\n", "\n", "#start computing \n", "\n", "#PIL nominale\n", "PIL=[0]*T\n", "for i in range(T):\n", " PIL[i]=np.dot(D[i,0::2],D[i,1::2]) \n", "data['PIL'] = PIL\n", "\n", "#PIL base t_0+s\n", "s=1\n", "PILs=[0]*T\n", "for i in range(T):\n", " PILs[i]=np.dot(D[i,0::2],D[s,1::2]) \n", "data['PIL b/%s' %(np.asarray(t_0, dtype=np.int)+s)] = PILs\n", "\n", "#crescita PIL\n", "PILg=[0]*T\n", "for t in range(1,T):\n", " PILg[t]=PIL[t]/PIL[t-1]-1\n", "PILg[0]=['']\n", "data['Crescita PIL'] = PILg\n", "#the following breaks the precision setting\n", "#data['crescita PIL'] = data['crescita PIL'].astype(str).str.replace('^0.0$', '')\n", "\n", "#crescita PILs \n", "PILsg=[0]*T\n", "for t in range(1,T):\n", " PILsg[t]=PILs[t]/PILs[t-1]-1\n", "PILsg[0]=['']\n", "data['Crescita PIL b/%s' %(np.asarray(t_0, dtype=np.int)+s)] = PILsg\n", "\n", "#deflatore\n", "defl=[0]*T\n", "for t in range(T):\n", " defl[t]=PIL[t]/PILs[t]\n", "data['Deflatore'] = defl\n", "\n", "results=data.loc[:, 'PIL':'Deflatore']\n", "\n", "\n", "# print \n", "print('')\n", "print('Risultati:')\n", "print(results)\n", "\n", "#write to latex table\n", "with open('BasicPIL-results-B.tex','w') as f2:\n", " f2.write(results.to_latex())" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }