bitcoin/bitcoin_return.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Bitcoin\n",
"\n",
"Consider a record of a one-time investment in bitcoin with value of that investment tracked monthly, provided as an (ordered) tuple of dictionaries, where each dictionary comprises one key for the month and corresponding one value for the value of the investment, and the first entry (Jan 2018) is the initial investment made on 01 Jan 2018, shown in `data` below.\n",
"\n",
"Write Python code to take such a record of any length (the below data is only a sample), and output a table/dataframe comprising a row for each month with columns for date, start balance, and return. **Print out this table/dataframe.**\n",
"\n",
"Also, visualize the record as two vertically arranged plots. \n",
"- The top plot should show a line plot of start balance vs. month\n",
"- The bottom plot should show a bar plot of return vs. month, with a black horizontal line at return=0, and bars color-coded such that positive returns are green and negative returns are red. \n",
"- The two plots' horizontal axes should align. Demonstrate that your code works by applying it to `data`.\n",
"\n",
"Notes:
\n",
"- The gain for each period is the end balance minus the start balance. \n",
"- The growth factor for each period is the end balance divided by the start balance. \n",
"- The return for each period is the growth factor minus 1."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"({'Jan 2018': 1000},\n",
" {'Feb 2018': 1100},\n",
" {'Mar 2018': 1400},\n",
" {'Apr 2018': 700},\n",
" {'May 2018': 800},\n",
" {'Jun 2018': 500})"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Do not alter data\n",
"data = ({\"Jan 2018\":1000},{\"Feb 2018\":1100},{\"Mar 2018\":1400},{\"Apr 2018\":700},{\"May 2018\":800},{\"Jun 2018\":500})\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"d = dict()\n",
"months = []\n",
"values = []\n",
"for t in data:\n",
" m, v = list(t.items())[0][0], list(t.items())[0][1]\n",
" months.append(m)\n",
" values.append(v)\n",
"\n",
"d[\"month\"] = months\n",
"d[\"value\"] = values\n",
"df = pd.DataFrame(d)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"month | \n",
"value | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"Jan 2018 | \n",
"1000 | \n",
"
\n",
"\n",
"1 | \n",
"Feb 2018 | \n",
"1100 | \n",
"
\n",
"\n",
"2 | \n",
"Mar 2018 | \n",
"1400 | \n",
"
\n",
"\n",
"3 | \n",
"Apr 2018 | \n",
"700 | \n",
"
\n",
"\n",
"4 | \n",
"May 2018 | \n",
"800 | \n",
"
\n",
"\n",
"5 | \n",
"Jun 2018 | \n",
"500 | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" month value\n",
"0 Jan 2018 1000\n",
"1 Feb 2018 1100\n",
"2 Mar 2018 1400\n",
"3 Apr 2018 700\n",
"4 May 2018 800\n",
"5 Jun 2018 500"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"df.insert(2, 'start', 0)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"month | \n",
"value | \n",
"start | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"Jan 2018 | \n",
"1000 | \n",
"0 | \n",
"
\n",
"\n",
"1 | \n",
"Feb 2018 | \n",
"1100 | \n",
"0 | \n",
"
\n",
"\n",
"2 | \n",
"Mar 2018 | \n",
"1400 | \n",
"0 | \n",
"
\n",
"\n",
"3 | \n",
"Apr 2018 | \n",
"700 | \n",
"0 | \n",
"
\n",
"\n",
"4 | \n",
"May 2018 | \n",
"800 | \n",
"0 | \n",
"
\n",
"\n",
"5 | \n",
"Jun 2018 | \n",
"500 | \n",
"0 | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" month value start\n",
"0 Jan 2018 1000 0\n",
"1 Feb 2018 1100 0\n",
"2 Mar 2018 1400 0\n",
"3 Apr 2018 700 0\n",
"4 May 2018 800 0\n",
"5 Jun 2018 500 0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"rows = len(df.index)\n",
"df.loc[0, 'start'] = df.loc[0, 'value']\n",
"for i in range(1, rows):\n",
" df.loc[i, 'start'] = df.loc[i-1, 'value']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"month | \n",
"value | \n",
"start | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"Jan 2018 | \n",
"1000 | \n",
"1000 | \n",
"
\n",
"\n",
"1 | \n",
"Feb 2018 | \n",
"1100 | \n",
"1000 | \n",
"
\n",
"\n",
"2 | \n",
"Mar 2018 | \n",
"1400 | \n",
"1100 | \n",
"
\n",
"\n",
"3 | \n",
"Apr 2018 | \n",
" ...SOLUTION.PDFAnswer To This Question Is Available To Download
Submit New Assignment
Please select references for your assignment
Please select no of pages for your assignment
Please select level for your assignment
x
I am Online - Talk to me!
Please fill out the form below to start chatting with the next available agent.
Mehmet Mert
3