关键词 > Python代写

Assignment #2 Python and Applications to Business Analytics

发布时间:2021-03-03

Assignment #2

Professor Ahmad Namini

Python and Applications to Business Analytics


Exercise 1. Amortization

Amortization is a method of repaying a loan through regular payments of interest and prin-cipal. The size of the loan (the original balance) is reduced by the principal part of the payment. The interest part of the payment pays the interest incurred on the remaining principal balance. As the principal gets paid down over the term of the loan, the interest part of the payment diminishes.

A new loan is typically issued with the following metrics:

● Principal: amount of the loan

● Interest Rate: annual interest rate

● Term: amount of time to pay back the loan with monthly payments

For instance, a given 15-year $250,000 at an 8.0% interest rate, the monthly payment of $2,389.13 is expected over the 180 (15 years * 12 payments per year) payments. Please note that borrowers have the option to make an extra payment which will be applied to the prin-cipal. Extra payments will lower the principal and thus the loan can be paid in full earlier than expected. A typical payment schedule with no extra payment is seen in the following.

Month
Begin P
Payment
Interest
Extra Payment
P Applied
End P
1
250,000.00
2,389.13
1,666.66
0.00
722.46
249,277.53
2
249,277.53
2,389.13
1,661.85
0.00
727.27
248,550.25
3
248,550.25
2,389.13
1,657.00
0.00
732.12
247,818.12






178
7,072.94
2,389.13
47.15
0.00
2,341.97
4,730.97
179
4,730.97
2,389.13
31.53
0.00
2,357.59
2,373.38
180
2,373.38
2,389.13
15.82
0.00
2,373.30
0.00
Total

430,043.44
180,043.43

250,000.00

Write a Python computer program to do the following:

Get user input of principal, minimum expected payment, interest, and extra payment, and error check that all input is valid. If not valid, have the user re-enter. Note that one does not need to enter the term of the loan.

Compute and print the schedule of payments, similar to the table shown above.

Compute and print the total (sum of all) payments, interest paid, and principal applied.

Compute and print the total time (in years) required to payoff the loan.

The computer program should leverage comments, readability, and above all, efficiency.


Exercise 2. Rolling Statistics

Attached is a file containing historical stock prices over a one-year period. With the data, compute a rolling window of statistics. Statistics would include the following:

Range: max - min

Mean: average

Median: 50th percentile

Standard Deviation: standard deviation

Write a Python computer program to do the following:

From the user, input the rolling window size. For instance, inputting 5 would yield a weekly window, while inputting a 20 is a monthly window, etc. Make sure that the input is valid. For instance, if a user inputs 400, this would be impossible because not enough data exists.

Over the rolling window, compute statistics mentioned above.

The following code will read a file a yahoo finance historical time series csv file and populate a time series with date and adjusted close (see the GOOG.csv)

def read_file ( filename, date_index, field_index, has_header=True ) :

      " " "

      Reads a csv file and parses the content field into a time series .

      Input :

      filename : csv filename

      date_index : zero — based index of the time series date field

      field_index : zero — based index of the time series content field

      has_header : True or False on whether the file contents has a header row

      Output :

      time_series : list of tuples with tuple consisting of ( date , content field )

      " " "

      time_series = [ ]

      with open( filename ) as csvfile :

   reader = csv . reader ( csvfile , delimiter=’ , ’ )

   if has_header :

next ( reade r , None )

   for row in reader :

time_series . append ( ( row [ date_index ] , float ( row [ field_index ] ) ) )

   return time_series

def main ( ) :

      # open and read file to populate date and adjusted_close

      filename = ’GOOG. csv ’

      ts = read_file ( filename , 0 , 5 )

      print ( f ’ { filenam e } _ has _ been _ read _ with _ { len ( ts )} _ daily _ prices ’ )

if _ _nam e_ _  == ’ _ _main_ _ ’ :

      main ( )