{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# Analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two classes for analysis: Alternative which provides methods for a [Cost Benefit Analysis](https://en.wikipedia.org/wiki/Cost–benefit_analysis) and Decision which provides methods for a [Multiple Criteria Analysis](https://en.wikipedia.org/wiki/Multiple-criteria_decision_analysis). The Timeline class generates a [Gantt Chart](https://en.wikipedia.org/wiki/Gantt_chart)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Alternative" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime, timedelta\n", "from dpd.analysis import Activity, Alternative, Currency\n", "\n", "USD = Currency(\"USD\", 2020, 0.07).discount()\n", "\n", "a1 = Alternative(name=\"Alternative One\")\n", "planning = Activity(\n", " \"Planning\", datetime(2005, 1, 1), datetime(2009, 12, 31), 100 * USD, 0 * USD\n", ")\n", "construction = Activity(\n", " \"Construction\", datetime(2010, 1, 1), datetime(2014, 12, 31), 100 * USD, 0 * USD\n", ")\n", "operations = Activity(\n", " \"Operations\",\n", " construction.End,\n", " construction.End + timedelta(days=365 * 20 + 4),\n", " 0 * USD,\n", " 400 * USD,\n", ")\n", "\n", "for activity in [planning, construction, operations]:\n", " a1.add_activity(activity)\n", "\n", "a1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a1.benefit_cost_ratio" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a1.period_range_pivot().head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "\n", "fig, ax = plt.subplots()\n", "\n", "a1.cash_flow_diagram(ax=ax)\n", "ax.get_legend().remove()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Timeline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from dpd.analysis import Timeline\n", "\n", "t = Timeline()\n", "for activity in [planning, construction, operations]:\n", " t.add_activity(activity)\n", "\n", "fig, ax = plt.subplots()\n", "t.plot_gantt(ax=ax)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "a1.timeline.plot_gantt(ax=ax)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Decision" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "from random import randint\n", "\n", "from astropy import units\n", "\n", "from dpd.analysis import Alternative, Currency, Decision\n", "from dpd.analysis.criteria import AttributeCriterion, CostCriterion, Criterion\n", "\n", "USD = Currency(\"USD\", 2020, 0.07).discount()\n", "decision = Decision()\n", "\n", "no_build_alternative = Alternative(\"No Build Alternative\")\n", "no_build_alternative.some_cost = 0 * USD\n", "alternative_one = Alternative(\"Alternative 1\")\n", "alternative_one.some_cost = 100 * USD\n", "alternative_two = Alternative(\"Alternative 2\")\n", "alternative_two.some_cost = 200 * USD\n", "\n", "\n", "@property\n", "def travel_time(self):\n", " return randint(100, 200) * units.second\n", "\n", "\n", "setattr(Alternative, \"travel_time\", travel_time)\n", "\n", "decision.add_criterion(AttributeCriterion(\"Travel Time\", weight=1 / units.second))\n", "decision.add_criterion(\n", " Criterion(\"Distance\", lambda x: 200 * units.meter, weight=1 / units.meter)\n", ")\n", "decision.add_criterion(AttributeCriterion(\"Some Cost\", weight=1 / USD))\n", "\n", "for alternative in [no_build_alternative, alternative_one, alternative_two]:\n", " decision.add_alternative(alternative)\n", "\n", "decision" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "decision.plot(kind=\"bar\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "decision.multiple_criteria_decision_analysis().plot(kind=\"bar\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from dpd.analysis.values import (\n", " Value_of_Reduced_Fatalities_and_Injuries,\n", " Value_of_Travel_Time_Savings,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Value_of_Reduced_Fatalities_and_Injuries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Value_of_Travel_Time_Savings" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from astropy.units import day, minute, year\n", "from dpd.analysis.units import person, usd2020" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(\n", " 5\n", " * minute\n", " * 40000\n", " * person\n", " / day\n", " * Value_of_Travel_Time_Savings[\n", " \"Walking Cycling, Waiting, Standing, and Transfer Time\"\n", " ]\n", ").decompose().to(usd2020 / year)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }