Bitcoin 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...

1 answer below »

Bitcoin


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 indatabelow.


Write Python code to take such a record of any length (the belowdatais 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.


Also, visualize the record as two vertically arranged plots.



  • The top plot should show a line plot of start balance vs. month

  • 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.

  • The two plots' horizontal axes should align. Demonstrate that your code works by applying it todata.


Notes:



  • The gain for each period is the end balance minus the start balance.

  • The growth factor for each period is the end balance divided by the start balance.

  • The return for each period is the growth factor minus 1.



data = ({"Jan 2018":1000},{"Feb 2018":1100},{"Mar 2018":1400},{"Apr 2018":700},{"May 2018":800},{"Jun 2018":500})data
Answered Same DayDec 12, 2021

Answer To: Bitcoin Consider a record of a one-time investment in bitcoin with value of that investment tracked...

Suraj answered on Dec 13 2021
148 Votes
{
"cells": [
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"
Investment
Jan 2018855
Feb 20181100
March 20181078
April 20181178
May 2018871
June 2018990
July 20181170
Aug 20181066
Sept 20181001
Oct 20181020
Nov 20181138
Dec 2018983
\n",
"
"
],
"text/plain": [
" Investment\n",
"Jan 2018 855\n",
"Feb 2018 1100\n",
"March 2018 1078\n",
"April 2018 1178\n",
"May 2018 871\n",
"June 2018 990\n",
"July 2018 1170\n",
"Aug 2018 1066\n",
"Sept 2018 1001\n",
"Oct 2018 1020\n",
"Nov 2018 1138\n",
"Dec 2018 983"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import random\n",
"import pandas as pd\n",
"value=np.random.randint(850,1200,13)\n",
"data={\"Jan 2018\":value[0:1],\"Feb 2018\":value[1:2],\"March 2018\":value[2:3],\"April 2018\":value[3:4],\"May 2018\":value[4:5],\"June 2018\":value[5:6],\"July 2018\":value[6:7],\"Aug 2018\":value[7:8],\"Sept 2018\":value[8:9],\"Oct 2018\":value[9:10],\"Nov 2018\":value[10:11],\"Dec 2018\":value[12:13]}\n",
"data\n",
"df=pd.DataFrame.from_dict(data,orient='Index',columns=[\"Investment\"])\n",
"df.head(12)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"
MonthInvestment
0Jan 2018855
1Feb 20181100
2March 20181078
3April 20181178
4May 2018871
5June 2018990
6July 20181170
7Aug 20181066
8Sept 20181001
9Oct 20181020
10Nov 20181138
11Dec 2018983
\n",
"
"
],
"text/plain": [
" Month Investment\n",
"0 Jan 2018 855\n",
"1 Feb 2018 1100\n",
"2 March 2018 1078\n",
"3 April 2018 1178\n",
"4 May 2018 871\n",
"5 June 2018 990\n",
"6 July 2018 1170\n",
"7 Aug 2018 1066\n",
"8 Sept 2018 1001\n",
"9 Oct 2018 1020\n",
"10 Nov 2018 1138\n",
"11 Dec 2018 983"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.reset_index(inplace=True) \n",
"df.rename(columns={\"index\":\"Month\"},inplace=True)\n",
"#df.drop(\"level_0\",axis=1,inplace=True)\n",
"df.head(12)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3 period of 4 months"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [],
"source": [
"growth=[]\n",
"for i in range(4):\n",
" val=df['Investment'][i]/df['Investment'][3]\n",
" growth.append(val)\n",
"for i in range(4,9):\n",
" val=df['Investment'][i]/df['Investment'][7]\n",
" growth.append(val)\n",
"for i in range(9,12):\n",
" val=df['Investment'][i]/df['Investment'][11]\n",
" growth.append(val)"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [],
"source": [
"ret=[]\n",
"for i in range(12):\n",
" val1=growth[i]-1\n",
" ret.append(val1)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"
MonthInvestmentReturn
0Jan 2018855-0.274194
1Feb 20181100-0.066214
2March 20181078-0.084890
3April 201811780.000000
4May 2018871-0.182927
\n",
"
"
],
"text/plain": [
" Month Investment Return\n",
"0 Jan 2018 855 -0.274194\n",
"1 Feb 2018 1100 -0.066214\n",
"2 March 2018 1078 -0.084890\n",
"3 April 2018 1178 0.000000\n",
"4 May 2018 871 -0.182927"
]
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['Return']=ret\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"data": {
"image/png":...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here