{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Weekly exercise 2: Tax function\n", "\n", "Australia has a progressive income taxes with several brackets.\n", "The tax function has the form\n", "\n", "$$\n", "\\begin{cases}\n", "0,&\n", "\\text{ if income} \\le \\text{th}_1 \\\\\n", "(\\text{income} - \\text{th}_1)*\\text{rate}_1,&\n", "\\text{ if income} \\in [\\text{th}_1, \\text{th}_2] \\\\\n", "(\\text{th}_2 - \\text{th}_1)*\\text{rate}_1 +\n", "(\\text{income} - \\text{th}_2)*\\text{rate}_2,&\n", "\\text{ if income} \\in [\\text{th}_2, \\text{th}_3] \\\\\n", "(\\text{th}_2 - \\text{th}_1)*\\text{rate}_1 +\n", "(\\text{th}_3-\\text{th}_2)*\\text{rate}_2 +\n", "(\\text{income} - \\text{th}_3)*\\text{rate}_3,&\n", "\\text{ if income} \\in [\\text{th}_3, \\text{th}_4] \\\\\n", "(\\text{th}_2 - \\text{th}_1)*\\text{rate}_1 +\n", "(\\text{th}_3-\\text{th}_2)*\\text{rate}_2 +\n", "(\\text{th}_4-\\text{th}_3)*\\text{rate}_3 +\n", "(\\text{income} - \\text{th}_4)*\\text{rate}_4,&\n", "\\text{ if income} > \\text{th}_4\n", "\\end{cases}\n", "$$\n", "\n", "The values for the thresholds and the rates in the 15 year in the period\n", "between 2000 and 2015 are given in the table below.\n", "\n", "|Year|$ th_1 $|$ th_2 $|$ th_3 $|$ th_4 $|$ rate_1 $|$ rate_2 $|$ rate_3 $|$ rate_4 $|\n", "|:----:|:------------:|:------------:|:------------:|:------------:|:--------------:|:--------------:|:--------------:|:--------------:|\n", "|2000|6,000|20,000|50,000|60,000|0.17|0.30|0.42|0.47|\n", "|2001|6,000|20,000|50,000|60,000|0.17|0.30|0.42|0.47|\n", "|2002|6,000|20,000|50,000|60,000|0.17|0.30|0.42|0.47|\n", "|2003|6,000|21,600|52,000|62,500|0.17|0.30|0.42|0.47|\n", "|2004|6,000|21,600|58,000|70,000|0.17|0.30|0.42|0.47|\n", "|2005|6,000|21,600|63,000|95,000|0.15|0.30|0.42|0.47|\n", "|2006|6,000|25,000|75,000|150,000|0.15|0.30|0.40|0.45|\n", "|2007|6,000|30,000|75,000|150,000|0.15|0.30|0.40|0.45|\n", "|2008|6,000|34,000|80,000|180,000|0.15|0.30|0.40|0.45|\n", "|2009|6,000|35,000|80,000|180,000|0.15|0.30|0.38|0.45|\n", "|2010|6,000|37,000|80,000|180,000|0.15|0.30|0.37|0.45|\n", "|2011|6,000|37,000|80,000|180,000|0.15|0.30|0.37|0.45|\n", "|2012|18,200|37,000|80,000|180,000|0.19|0.325|0.37|0.45|\n", "|2013|18,200|37,000|80,000|180,000|0.19|0.325|0.37|0.45|\n", "|2014|18,200|37,000|80,000|180,000|0.19|0.325|0.37|0.45|\n", "|2015|18,200|37,000|80,000|180,000|0.19|0.325|0.37|0.45|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1:\n", "\n", "- Write a function to calculate income tax from 2 input arguments: year and amount of income \n", "- Use **if/elif/then** statements for branching of the code \n", "- Run your function on the data provided " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "# draft implementation for year 2000 to get you started\n", "\n", "income=54780.0\n", "\n", "if income <= 6000:\n", " tax = 0.0\n", "elif income > 6000 and income <= 20000:\n", " tax = (income-6000)*0.17\n", "elif income > 20000 and income <= 50000:\n", " tax = (income-20000)*0.30\n", "elif income > 50000 and income <= 60000:\n", " tax = (income-50000)*0.42\n", "elif income > 60000:\n", " tax = (income-60000)*0.47\n", "\n", "print('tax(%1.2f) = %1.2f AUD' % (income,tax))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "# run your function on the following data\n", "\n", "year = 2000\n", "income = 42400\n", "\n", "year = 2010\n", "income = 58300\n", "\n", "year = 2015\n", "income = 82710\n", "\n", "year = 2012\n", "income = 120340\n", "\n", "year = 2001\n", "income = 431400\n", "\n", "year = 2003\n", "income = 41870\n", "\n", "year = 2090\n", "income = 1407790" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2:\n", "\n", "- Make the plot of the tax functions using the starter code below \n", "- Replace the @@@ symbols with appropriate code \n", "- The plot should show 15 different lines in the income - tax space " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "#let's make a plot as well\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "plt.rcParams['figure.figsize'] = [12, 8]\n", "K = 250 #number of points in the income grid\n", "max_inc = 250000 #maximum income\n", "inc = [k * @@ / (K-1) for k in range(@@)] # grid over income\n", "tax = [0,]*K\n", "for year in [@@@ for i in range(@@)]:\n", " for j in range(@@):\n", " income = inc[j]\n", " tax[j] = @@@\n", " plt.plot(inc,tax,label=\"year %d\" % year)\n", "plt.xlabel(@@@)\n", "plt.ylabel(@@@)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 3 Alternative implementation [optional]\n", "\n", "- Write another implementation of the same function based on the **while** loop \n", "- Hint: precompute thresholds and rates based on the year, and run a while loop to compute the amount of tax in stages \n", "- Run the new implementation on the same data as above and verify it returns the same answers \n", "- Discuss the advantages and disadvantages of the two implementations " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide-output": false }, "outputs": [], "source": [ "#Your code here" ] } ], "metadata": { "date": 1627475014.4823182, "filename": "exercise02.rst", "kernelspec": { "display_name": "Python", "language": "python3", "name": "python3" }, "title": "Weekly exercise 2: Tax function" }, "nbformat": 4, "nbformat_minor": 4 }