SHARE
TWEET

Untitled

a guest May 19th, 2017 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "markdown",
  5.    "metadata": {
  6.     "collapsed": false
  7.    },
  8.    "source": [
  9.     "\n",
  10.     "\n",
  11.     "\n",
  12.     "<h1><center>Tina Koziol</center></h1>\n",
  13.     "<center>PhD Candidate, UCT </center>\n",
  14.     "\n",
  15.     "\n",
  16.     "------------------------------------------------------------------------\n",
  17.     "\n",
  18.     "\n",
  19.     "\n",
  20.     "&nbsp;\n",
  21.     "\n",
  22.     "\n",
  23.     "\n",
  24.     "\n",
  25.     "Coding \n",
  26.     "================\n",
  27.     "\n",
  28.     "\n",
  29.     "- Writing a test for my vulnerable banks code. The __str__ method in my agent class did not display what my agent config xml looked like becuase of the parent class. So I modified the ```__str__``` method in my example folder. I also created symlink folders within the tests folder to not have redundant files. \n",
  30.     "\n",
  31.     "&nbsp;"
  32.    ]
  33.   },
  34.   {
  35.    "cell_type": "code",
  36.    "execution_count": 21,
  37.    "metadata": {
  38.     "collapsed": false
  39.    },
  40.    "outputs": [
  41.     {
  42.      "name": "stdout",
  43.      "output_type": "stream",
  44.      "text": [
  45.       "/Users/Tina/git_repos/BlackRhino/examples/firesales_SA/tests\n"
  46.      ]
  47.     }
  48.    ],
  49.    "source": [
  50.     "cd /Users/Tina/git_repos/BlackRhino/examples/firesales_SA/tests/"
  51.    ]
  52.   },
  53.   {
  54.    "cell_type": "code",
  55.    "execution_count": 22,
  56.    "metadata": {
  57.     "collapsed": false
  58.    },
  59.    "outputs": [
  60.     {
  61.      "name": "stdout",
  62.      "output_type": "stream",
  63.      "text": [
  64.       "This tests methods for agent class\n",
  65.       "XxxxxxXXxxxxXXXXxx\n",
  66.       "Initiating agent object..\n",
  67.       "\n",
  68.       "XxxxxxXXxxxxXXXXxx\n",
  69.       "  <agent identifier=''>\n",
  70.       "     <parameter type='state_variables' name='losses_from_system_deleveraging' value='0.0'></parameter>\n",
  71.       "    <parameter type='state_variables' name='shock_for_agent' value='0.0'></parameter>\n",
  72.       "    <parameter type='state_variables' name='total_assets' value='0'></parameter>\n",
  73.       "    <parameter type='state_variables' name='direct_losses' value='0.0'></parameter>\n",
  74.       "    <parameter type='state_variables' name='total_asset_sales' value='0.0'></parameter>\n",
  75.       "</agent>\n",
  76.       "\n",
  77.       "Reading in parameters by calling method..\n",
  78.       "\n",
  79.       "XxxxxxXXxxxxXXXXxx\n",
  80.       "  <agent identifier='CAPITEC'>\n",
  81.       "  <parameter type='parameters' name=debt' value='48913724000.0'></parameter>\n",
  82.       " <parameter type='parameters' name=equity' value='13056412000.0'></parameter>\n",
  83.       "    <parameter type='state_variables' name='losses_from_system_deleveraging' value='0.0'></parameter>\n",
  84.       "    <parameter type='state_variables' name='direct_losses' value='0.0'></parameter>\n",
  85.       "    <parameter type='state_variables' name='m_22' value='0.0'></parameter>\n",
  86.       "    <parameter type='state_variables' name='m_23' value='0.0292484274885'></parameter>\n",
  87.       "    <parameter type='state_variables' name='m_20' value='0.0'></parameter>\n",
  88.       "    <parameter type='state_variables' name='m_21' value='0.0615316071201'></parameter>\n",
  89.       "    <parameter type='state_variables' name='leverage' value='3.74633735516'></parameter>\n",
  90.       "    <parameter type='state_variables' name='m_9' value='0.0'></parameter>\n",
  91.       "    <parameter type='state_variables' name='m_8' value='0.0'></parameter>\n",
  92.       "    <parameter type='state_variables' name='m_3' value='0.0'></parameter>\n",
  93.       "    <parameter type='state_variables' name='m_2' value='0.263017629379'></parameter>\n",
  94.       "    <parameter type='state_variables' name='m_1' value='0.0579756038937'></parameter>\n",
  95.       "    <parameter type='state_variables' name='m_7' value='0.0'></parameter>\n",
  96.       "    <parameter type='state_variables' name='m_6' value='0.0'></parameter>\n",
  97.       "    <parameter type='state_variables' name='m_5' value='0.0'></parameter>\n",
  98.       "    <parameter type='state_variables' name='m_4' value='0.0'></parameter>\n",
  99.       "    <parameter type='state_variables' name='m_19' value='0.0039719616554'></parameter>\n",
  100.       "    <parameter type='state_variables' name='m_18' value='0.0'></parameter>\n",
  101.       "    <parameter type='state_variables' name='m_17' value='0.0'></parameter>\n",
  102.       "    <parameter type='state_variables' name='m_16' value='0.0'></parameter>\n",
  103.       "    <parameter type='state_variables' name='m_15' value='0.0'></parameter>\n",
  104.       "    <parameter type='state_variables' name='m_14' value='0.0'></parameter>\n",
  105.       "    <parameter type='state_variables' name='m_13' value='2.43665755389e-06'></parameter>\n",
  106.       "    <parameter type='state_variables' name='m_12' value='0.575324856078'></parameter>\n",
  107.       "    <parameter type='state_variables' name='m_11' value='0.00892747772778'></parameter>\n",
  108.       "    <parameter type='state_variables' name='m_10' value='0.0'></parameter>\n",
  109.       "    <parameter type='state_variables' name='shock_for_agent' value='0.0'></parameter>\n",
  110.       "    <parameter type='state_variables' name='total_assets' value='0'></parameter>\n",
  111.       "    <parameter type='state_variables' name='total_asset_sales' value='0.0'></parameter>\n",
  112.       "</agent>\n",
  113.       "\n"
  114.      ]
  115.     }
  116.    ],
  117.    "source": [
  118.     "# %load /Users/Tina/git_repos/BlackRhino/examples/firesales_SA/tests/abm_firesales_tests.py\n",
  119.     "# -------------------------------------------------------------------------\n",
  120.     "#\n",
  121.     "#  MAIN\n",
  122.     "#\n",
  123.     "# -------------------------------------------------------------------------\n",
  124.     "if __name__ == '__main__':\n",
  125.     "\n",
  126.     "    import pdb  # python debugger, for debugging purposes only\n",
  127.     "\n",
  128.     "    import sys\n",
  129.     "\n",
  130.     "\n",
  131.     "    from inspect import getsourcefile\n",
  132.     "    from os import path\n",
  133.     "    sys.path.append( path.dirname( path.dirname( path.abspath(getsourcefile(lambda:0)) ) ) )\n",
  134.     "    \n",
  135.     "    from src.environment import Environment\n",
  136.     "    from TestAgent import TestAgent\n",
  137.     "\n",
  138.     "\n",
  139.     "    test_agent_object = TestAgent()  \n",
  140.     "    \n",
  141.     "    test_agent_object.print_info(\"This tests methods for agent class\")\n",
  142.     "\n",
  143.     "    test_agent_object.get_parameters_from_file([\"configs/environment/\", \"test_firesales\"])\n"
  144.    ]
  145.   },
  146.   {
  147.    "cell_type": "code",
  148.    "execution_count": 23,
  149.    "metadata": {
  150.     "collapsed": false
  151.    },
  152.    "outputs": [
  153.     {
  154.      "name": "stdout",
  155.      "output_type": "stream",
  156.      "text": [
  157.       "/Users/Tina/git_repos/jupyter_notebooks/weeklyreports/code/replace_xmls/agent1.xml\n",
  158.       "XXXX OLD:\n",
  159.       "<agent identifier=\"SBSA\">\n",
  160.       "\n",
  161.       "<parameter type=\"parameters\" name=\"equity\" value=\"86200249000.0\"></parameter>\n",
  162.       "\n",
  163.       " <parameter type=\"state_variables\" name=\"leverage\" value=\"13.0780116888061\"></parameter>\n",
  164.       "\n",
  165.       " <parameter type=\"parameters\" name=\"debt\" value=\"1127327864000.0\"></parameter>\n",
  166.       "\n",
  167.       " <parameter type=\"state_variables\" name=\"m_1\" value=\"0.0245212259042243\" label=\"Cash and gold reserves \"></parameter>\n",
  168.       "\n",
  169.       " <parameter type=\"state_variables\" name=\"m_2\" value=\"0.0107197211672673\" label=\"SA Interbank deposits, loans and advances \"></parameter>\n",
  170.       "\n",
  171.       " <parameter type=\"state_variables\" name=\"m_3\" value=\"0.00405856934605684\" label=\"Rand Deposits with and loans to foreign banks\"></parameter>\n",
  172.       "\n",
  173.       " <parameter type=\"state_variables\" name=\"m_4\" value=\"0.0359414516505725\" label=\"Loans granted under repo agreement\"></parameter>\n",
  174.       "\n",
  175.       " <parameter type=\"state_variables\" name=\"m_5\" value=\"0.129182760844701\" label=\"Foreign currency loans and advances d 134),\"></parameter>\n",
  176.       "\n",
  177.       " <parameter type=\"state_variables\" name=\"m_6\" value=\"0.0170625845237423\" label=\"Redeemable preference shares\"></parameter>\n",
  178.       "\n",
  179.       " <parameter type=\"state_variables\" name=\"m_7\" value=\"0.0296141684852751\" label=\"corporate instalment credit \"></parameter>\n",
  180.       "\n",
  181.       " <parameter type=\"state_variables\" name=\"m_8\" value=\"0.0252157207337808\" label=\"household instalment credit \"></parameter>\n",
  182.       "\n",
  183.       " <parameter type=\"state_variables\" name=\"m_9\" value=\"0.0690936667241429\" label=\"corporate mortgage\"></parameter>\n",
  184.       "\n",
  185.       " <parameter type=\"state_variables\" name=\"m_10\" value=\"0.217515842585181\" label=\"household mortgage\"></parameter>\n",
  186.       "\n",
  187.       " <parameter type=\"state_variables\" name=\"m_11\" value=\"0.114607784121438\" label=\"Unsecured lending corporate\"></parameter>\n",
  188.       "\n",
  189.       " <parameter type=\"state_variables\" name=\"m_12\" value=\"0.0247813962262941\" label=\"Unsecured lending households\"></parameter>\n",
  190.       "\n",
  191.       " <parameter type=\"state_variables\" name=\"m_13\" value=\"0.0589651490010434\" label=\"Other credit (credit card + leasing + Overdarft + factoring debt)\"></parameter>\n",
  192.       "\n",
  193.       " <parameter type=\"state_variables\" name=\"m_14\" value=\"0.0412585423144622\" label=\"Central and provincial government bonds\"></parameter>\n",
  194.       "\n",
  195.       " <parameter type=\"state_variables\" name=\"m_15\" value=\"0.00466172471770335\" label=\"Other public-sector bonds\"></parameter>\n",
  196.       "\n",
  197.       " <parameter type=\"state_variables\" name=\"m_16\" value=\"0.0123382885320927\" label=\"Private sector bonds\"></parameter>\n",
  198.       "\n",
  199.       " <parameter type=\"state_variables\" name=\"m_17\" value=\"0.000201706905161743\" label=\"Equity holdings in subsidiaries and joint ventures\"></parameter>\n",
  200.       "\n",
  201.       " <parameter type=\"state_variables\" name=\"m_18\" value=\"0.0129940574355693\" label=\"Listed and unlisted equities\"></parameter>\n",
  202.       "\n",
  203.       " <parameter type=\"state_variables\" name=\"m_19\" value=\"0.0839139051737815\" label=\"Securitisation/ asset-backed securities\"></parameter>\n",
  204.       "\n",
  205.       " <parameter type=\"state_variables\" name=\"m_20\" value=\"0.00454473608062181\" label=\"Derivative instruments\"></parameter>\n",
  206.       "\n",
  207.       " <parameter type=\"state_variables\" name=\"m_21\" value=\"0.0408429870466462\" label=\"Treasury bills, SA Reserve Bank bills,  Land Bank bills \"></parameter>\n",
  208.       "\n",
  209.       " <parameter type=\"state_variables\" name=\"m_22\" value=\"0.00209378503289771\" label=\"Other investments\"></parameter>\n",
  210.       "\n",
  211.       " <parameter type=\"state_variables\" name=\"m_23\" value=\"0.0358702254473441\" label=\"Non financial assets\"></parameter>\n",
  212.       "\n",
  213.       "</agent>\n",
  214.       "\n",
  215.       "XXXX NEW\n",
  216.       "<agent identifier=\"SBSA\">\n",
  217.       "\n",
  218.       "<parameter type=\"state_variables\" name=\"equity\" value=\"86200249000.0\"></parameter>\n",
  219.       "\n",
  220.       " <parameter type=\"parameters\" name=\"leverage\" value=\"13.0780116888061\"></parameter>\n",
  221.       "\n",
  222.       " <parameter type=\"state_variables\" name=\"debt\" value=\"1127327864000.0\"></parameter>\n",
  223.       "\n",
  224.       " <parameter type=\"parameters\" name=\"m_1\" value=\"0.0245212259042243\" label=\"Cash and gold reserves \"></parameter>\n",
  225.       "\n",
  226.       " <parameter type=\"parameters\" name=\"m_2\" value=\"0.0107197211672673\" label=\"SA Interbank deposits, loans and advances \"></parameter>\n",
  227.       "\n",
  228.       " <parameter type=\"parameters\" name=\"m_3\" value=\"0.00405856934605684\" label=\"Rand Deposits with and loans to foreign banks\"></parameter>\n",
  229.       "\n",
  230.       " <parameter type=\"parameters\" name=\"m_4\" value=\"0.0359414516505725\" label=\"Loans granted under repo agreement\"></parameter>\n",
  231.       "\n",
  232.       " <parameter type=\"parameters\" name=\"m_5\" value=\"0.129182760844701\" label=\"Foreign currency loans and advances d 134),\"></parameter>\n",
  233.       "\n",
  234.       " <parameter type=\"parameters\" name=\"m_6\" value=\"0.0170625845237423\" label=\"Redeemable preference shares\"></parameter>\n",
  235.       "\n",
  236.       " <parameter type=\"parameters\" name=\"m_7\" value=\"0.0296141684852751\" label=\"corporate instalment credit \"></parameter>\n",
  237.       "\n",
  238.       " <parameter type=\"parameters\" name=\"m_8\" value=\"0.0252157207337808\" label=\"household instalment credit \"></parameter>\n",
  239.       "\n",
  240.       " <parameter type=\"parameters\" name=\"m_9\" value=\"0.0690936667241429\" label=\"corporate mortgage\"></parameter>\n",
  241.       "\n",
  242.       " <parameter type=\"parameters\" name=\"m_10\" value=\"0.217515842585181\" label=\"household mortgage\"></parameter>\n",
  243.       "\n",
  244.       " <parameter type=\"parameters\" name=\"m_11\" value=\"0.114607784121438\" label=\"Unsecured lending corporate\"></parameter>\n",
  245.       "\n",
  246.       " <parameter type=\"parameters\" name=\"m_12\" value=\"0.0247813962262941\" label=\"Unsecured lending households\"></parameter>\n",
  247.       "\n",
  248.       " <parameter type=\"parameters\" name=\"m_13\" value=\"0.0589651490010434\" label=\"Other credit (credit card + leasing + Overdarft + factoring debt)\"></parameter>\n",
  249.       "\n",
  250.       " <parameter type=\"parameters\" name=\"m_14\" value=\"0.0412585423144622\" label=\"Central and provincial government bonds\"></parameter>\n",
  251.       "\n",
  252.       " <parameter type=\"parameters\" name=\"m_15\" value=\"0.00466172471770335\" label=\"Other public-sector bonds\"></parameter>\n",
  253.       "\n",
  254.       " <parameter type=\"parameters\" name=\"m_16\" value=\"0.0123382885320927\" label=\"Private sector bonds\"></parameter>\n",
  255.       "\n",
  256.       " <parameter type=\"parameters\" name=\"m_17\" value=\"0.000201706905161743\" label=\"Equity holdings in subsidiaries and joint ventures\"></parameter>\n",
  257.       "\n",
  258.       " <parameter type=\"parameters\" name=\"m_18\" value=\"0.0129940574355693\" label=\"Listed and unlisted equities\"></parameter>\n",
  259.       "\n",
  260.       " <parameter type=\"parameters\" name=\"m_19\" value=\"0.0839139051737815\" label=\"Securitisation/ asset-backed securities\"></parameter>\n",
  261.       "\n",
  262.       " <parameter type=\"parameters\" name=\"m_20\" value=\"0.00454473608062181\" label=\"Derivative instruments\"></parameter>\n",
  263.       "\n",
  264.       " <parameter type=\"parameters\" name=\"m_21\" value=\"0.0408429870466462\" label=\"Treasury bills, SA Reserve Bank bills,  Land Bank bills \"></parameter>\n",
  265.       "\n",
  266.       " <parameter type=\"parameters\" name=\"m_22\" value=\"0.00209378503289771\" label=\"Other investments\"></parameter>\n",
  267.       "\n",
  268.       " <parameter type=\"parameters\" name=\"m_23\" value=\"0.0358702254473441\" label=\"Non financial assets\"></parameter>\n",
  269.       "\n",
  270.       "</agent>\n",
  271.       "\n"
  272.      ]
  273.     }
  274.    ],
  275.    "source": [
  276.     "run /Users/Tina/git_repos/jupyter_notebooks/weeklyreports/code/replace_xmls/replace_xml.py"
  277.    ]
  278.   },
  279.   {
  280.    "cell_type": "markdown",
  281.    "metadata": {},
  282.    "source": [
  283.     "- This script uses the ```  TestAgent``` class to test the    ```get_parameters_from_file``` function within my agent class. \n",
  284.     "- Within ``` TestAgent```, I first initiate the environment (because the agent directory is stored in the environment object), use  ```  listings = os.listdir(agent_directory)  ``` to loop over the agent xmls in the directory.\n",
  285.     "- Then I instantiate an agent object and ```  print agent```, so you can see the agent doesn't have any parameters yet.  \n",
  286.     "- Afterwards I call  ```agent.get_parameters_from_file(agent_filename, environment)```  and ```  print agent```  to see if the function worked\n",
  287.     "- State variables ```total_asset_sales``` is still 0 because the update algorythm was not called\n",
  288.     "\n",
  289.     "\n",
  290.     "\n",
  291.     "As you can see the state_variable - parameters type are mixed up so I wrote a small script that loops over agent xmls and replaces the strings. This looks like this. \n"
  292.    ]
  293.   },
  294.   {
  295.    "cell_type": "markdown",
  296.    "metadata": {},
  297.    "source": [
  298.     "I know this doesn't seem much, but I was quite happy I managed to do this in python. Now I can loop over all agent configs and change the xml files, instead of going through them individually. Even though I am happy about the coding this week, the paper itself didn't really move forward. I need to push more the next weeks."
  299.    ]
  300.   },
  301.   {
  302.    "cell_type": "markdown",
  303.    "metadata": {},
  304.    "source": [
  305.     "# Seminar\n",
  306.     "\n",
  307.     "-I prepared some slides for the ACQUFFR seminar and was quite nervous, but it went ok. My public speaking skills are a bit rusty, I suppose I need more practice/exposure.  I gave them a general talk about central banks and monetary policy because they don't have that in their course (I also mentioned the Lucca%Moench paper). Alex said he took away some interesting things, as they \"normally don't consider the bigger picture\", but only asset pricing.  \n",
  308.     "\n"
  309.    ]
  310.   },
  311.   {
  312.    "cell_type": "markdown",
  313.    "metadata": {},
  314.    "source": [
  315.     "# Other stuff\n",
  316.     "\n",
  317.     "- I looked into the Econometrics content. I really need to fresh up on my R (this week installed it and covered some basics, like reading into files, installing packages and working with the notebooks), so I anticipate this will take up quite some time next week. I talked to Nolwazi and we want to do a 50:50 work split between the tutorials.\n",
  318.     "\n",
  319.     "- I talked to Alexia and we agreed to find a feasable solution for the outreach program. The class is rather struggling this year, so I don't want to make it more difficult for them (apparently 4 students already deregistered). We spoke to some of the people who did it last year and apparently Ndima Yam was providing most of the content. The students did not spend more than 2h preparing, so we should be able to find something 'easy' like this again. My meeting with Pheliswa was postponed because she is knocked out by the flue. But she was very positive about a cooperation this year again. "
  320.    ]
  321.   }
  322.  ],
  323.  "metadata": {
  324.   "kernelspec": {
  325.    "display_name": "Python 2",
  326.    "language": "python",
  327.    "name": "python2"
  328.   },
  329.   "language_info": {
  330.    "codemirror_mode": {
  331.     "name": "ipython",
  332.     "version": 2
  333.    },
  334.    "file_extension": ".py",
  335.    "mimetype": "text/x-python",
  336.    "name": "python",
  337.    "nbconvert_exporter": "python",
  338.    "pygments_lexer": "ipython2",
  339.    "version": "2.7.13"
  340.   }
  341.  },
  342.  "nbformat": 4,
  343.  "nbformat_minor": 2
  344. }
RAW Paste Data
Pastebin PRO Autumn Special!
Get 40% OFF on Pastebin PRO accounts!
Top