QuestionQuestion

Transcribed TextTranscribed Text

lnstructions Write a Python program to solve the following problem. Your solution should include a readme.md file (which includes details of how to run your assignment) and your Python program in a file named fimc. py , and be submitted as a single . t gz file named a3 · tgz You should ensure your solution works using the Python 3 interpreter on turing. Problem Bob is worried . Your financia! independence calculator showed that he should be able to retire , but he's beginning to question sorne of the assumptions he made when giv ing you his initial specification. He'd hate to retire, only to have to come back to work in ten years' time if he ran out of money . In particular, he's worried that inflation and interest rates and not constant, and would like you to modify your program to take that into account. Further, given the uncertain ty of futu re rates, he would like you to implement a Monte Cario experiment to help give him confidence that the savings he has are good enough. fo this end, you should first create a function named . run_simulation that works very ,imilarly to your previous financia! independence calculator, with the followi ng parameters (in ,rder): .........., . ...................................................................................................................................................................... ......... Parameter name Description Parameter name annual_spend inflation_rate savings_bala nce interest_rate num_years inflation_change interest_change Description The amount the user spent last year to live at their current lifestyle The inflation rate at which the user's cost of living increases each year · The amount the user currently has saved for investment The interest rate at which the user's savings grow each year The number of years for which the simulation should be run The maximum amount the inflation rate can change in a single year The maximum amount the interest rate can change in a single year This funct ion should return a list with one entry for each year of the simulation, consisting of the remaining savings balance after the annual spend amount has been subtracted, and the annual interest added. lf we ignore rate cllanges, then the list returned woule be identícal to the values you printed out for each year in your previous financia! calculator. But rather than printing out the values, this function simply adds them to a list, which gets returned once the entire simulation is complete (note, in particular, that negative values should not stop the simulation like they did in the previous calculator). For example, a cal! such as run_simulation(50000, 0.02, 1000000, 0.05, 20, 0, 0) should return the list [996450.0, 991651.5, 985520.655, 977968.99935, 968903.2071495001, 958224.8404956151, and the call run_simulation(100000, 0.02s, 1s00000, 0.04, 20, 0, 0) should return (1453400.0, 1402211.0, 1346365.215, 1285423.282975, 1219173.760153375, 1147332.s 9! The Monte Cario simulation comes in when we modify the inflation and interest rates after a year's savings balance has been calculated. Befare iterating to the next year, the inflation rate should be updated using a call similar to inflation_rate = modify_rate(inflation_rate, inflation_change) , and the interest rate using a call similar to interest_rate = modify_rate(interest_rate, interest_change) , where modify_rate is defined as follows: Note: You wí/1 need to ·lmport random at the beginning of your program for the modify_rate function to work. > def modify_rate(rate, change): ti ti H Calculate a random value within change of the given rate Arguments: rate -- The initial rate to change from change -- The maximum amount to add or subtract from the rate Returns a random value within change of the given rate Explanation: random.random() calculates a random value between 0 and l. Thus random.random() * 2 gives a value between 0 and 2, so 1 - random.random() * 2 gives a value between-~1 and l. Multiplying by change gives a value between -change and change, which is then added to the rate. return rate + change * (1 - random.random() * 2) Because the rates are changing by a random amount each iteration, anda different random value is generated every time we call modify_rate , we cannot expect calls to run_simulation to always give the same results. Thus, if we call run_simulation multiple times, even with the same parameters, we will actually be running different experiments, where the rates change differently (provided our rate change val u es are not zero ). lf we run a number of these simulations, then w~ c"'atcirrat~e many of them result in the savings balance ending up negative (indicating that Bob would need to go back to work), and how many end up non-negative (indicating that Bob does not need to work any more). lf a large number of simulations are run and a high enough percentage are successful, Bob will feel confident that he can retire. Thus, your program should start by first getting the required inputs frorn the user. This will be very similar to your previous firiancial independence calculator, only you will also need to obtain the inflation change value (which should,. be_entered immediately after the base inflation rate) and interest change value (which should be ente red immediately after the base interest rate) values from the user. Then, after reading in the number of years for which each individual sitnulation will run, you need to ask the user for the number of ~imulati~s to run (thi~ust ~a positive integer). You should then loop for the required number of simulations, and get the result from cal/ing run_simulation with the values you read in from the user. lf the final balance is greater than or equal to zero, then that indicates that the simulation was successful and the user would be able to retire if the rates followed the random sequence used in that simulation. You should keep a count of the number of successful simulations and output both the number of successful simulations and percentage of successful simulations before your program exits . As well as outputting the number and percentage of successful simulations to standard output , Bob would also like to look at the individual simulation results. To that end, he would like your program to create (or overwrite) a file named output. txt where each line consists of the values of a single simulation. Each line should contain the balance for each year of the simulation (to two decimal places) seperated by spaces, followed by the string "successful" if the simulation was successful, or the string "unsuccessful" otherwise (see example interactions below fer more details ). Note that program specifications are not always clear. lf you are uncertain about any aspect, you are typically better off asking than making assumptions. Please use the appropriate discussion forum to ask far c/arification, if required. Example lnte·ractions Sorne example interactions with the program could look like: How much did you spend last year to support your current lifestyle? 50000 Please enter the base inflation rate: 0.02 Please enter the expected maximum change for inflation in a given year: 0.005 How much do you curr~ntly have saved for investment? 1000000 Please enter the base annual interest rate: 0.05 Please enter the expected maximumc hange for i·n t eres t i·n a given year: 0.01 How many years do you want to test? 20 How many simulations should be run? 10 Simulation was successful in 10/10 runs (100.00%) which could result in the following being written to output. txt : or 996450.00 997127.57 991738.31 991 570.19 998556.25 1000484.89 997192.4 5 990387.97 9756 23.83 958100.54 942007.60 929704.62 918233.23 900004.58 887633.66 870515.07 847598.11 818308.90 789771.25 758229.52 successful 996450.00 990742.82 990672.72 985749.77 975074.53 968791.87 957437.07 939607 .47 91316 8.46 879636.30 848867.03 821970.21 788351.47 747336.70 697057.29 645413 .23 584250.92 516481.62 443916.83 366322.98 successful 996450.00 984864.56 966349.06 946290.65 930039.13 906929.30 889398 . 10 869435.43 84683 9.11 825558.81 801240.47 772569.28 744955.97 719052.12 684296.49 648650.32 608710.95 563139.21 514628.47 459006.28 successful 996450.00 991021.06 987994.35 990566.15 999510.05 1001476.72 999795.9 1 989668.45 9784 68.37 974108.38 964150.93 951465.49 930572.62 900426.75 874582.85 850496.38 822418.99 793876.87 761754.89 722025.21 successful 996450.00 988315.40 971097.67 953592.83 928254.94 897432.03 858915.51 819282.51 77448 0.58 733599.88 690442.87 649415.01 608173.95 560050.88 507090.65 44903 3 .86 391749.60 330482.33 267064.66 200467.62 successful 996450.00 995583.14 990118.07 986506.97 976976.67 959630.10 941217.41 920010.23 88948 2.94 863641.31 828430.79 792768.97 758749.57 716961.68 669008.56 61875 1 .16 567777.90 514683.54 460895.87 404014.99 successful 996450.00 1001025.26 1004000.64 1006275.12 1005355.03 1001912.09 9948 16.68 990644.97 978347.15 964815.09 956917.20 938870.90 922686.48 898394.32 877677.78 860909.80 84250 7.64 822149.32 805462.05 789396.32 successful 996450.00 983440.51 974691.77 959623.50 942173.09 915875.19 885931.86 853122.84 81330 0.26 772777.32 731838.06 682693.23 635092.66 580649.62 520025.10 452890.62 378359.38 302885.26 221603.17 135806.35 successful 996450.00 982870.36 962530.78 938946.45 918506.32 895509.32 868759.74 835849 .95 79906 6.21 763733.44 726344.68 686079.46 638342.18 594013.99 546518.85 4978 66 .11 448964 . 84 398676.51 344741.56 288125.35 successful 996450.00 989554.74 986020.89 987178.50 991353.62 999140.09 1003679.7 1 101 5112.14 102 1784.56 1024498.81 1028085.23 1031613.11 1034157.27 1041985.32 105707 2. 05 1068082.57 1076359.21 1093909.70 1115623.15 1130307.06 successful you spend last year to support your current lifestyle? hello How much did Please entera non-negative integer your current lifestyle? -1 How much did you spend last year to support Please enter a non-negative integer your current lifestyle? 1.1 How much did you spend last year to support Pl ease entera non-negative integer How much did you spend last year to support your current lifestyle? 50000 Please enter the base inflation rate: hello Please entera real number Please enter the base inflation rate: 0.025 Please enter the expected maximum change for inflation in a given year: hello Please entera real number Please enter the expected maximum change for inflation in a given year: 0.005 How much do you currently have saved for investment? hello Please enteran integer How much do you currently have saved for investment? 850000 Please enter the base annual interest rate: hello Please enter a real number Please enter the base annual interest rate: 0.05 Please enter the expected maximumc hange for interest in a given year: hello Please enter a real number Please enter the expected maximumc hange for interest in a given year: hello Please enter a real number Please enter the expected maximumc hange for interest in a given year: 0.01 How many years do you want to test? hello Please entera positive integer How many years do you want to test? -1 Please entera positive integer How many years do you want to test? 0 Pl ease entera positive integer How many years do you want to test? 20 How many simulat ions should be run? hello Pl ease entera positive integer How many simulat ions should be run? -1 Please ent era positive integer How many si mulat ions should be run? 0 Please enter a posit ive integer How many simulati ons should be run? 10 Simulation was successful in 7/10 runs (70.00%) which could result in the following being written to output. txt --------------■ 838687.50 823340.63 803023.40 784081.93 759252.34 729454.21 696870.37 666087.53 63119 9 .66 596533.42 555586.17 511647.61 467819.64 419249.00 368043.07 315524.06 259713.30 198661.90 131223.00 58310.49 successful 838687.50 830490.72 824849.55 818236.12 810918.85 804902.19 797123.32 780542.52 76613 8.37 750995.38 736443.37 720819.33 699382.43 680439.19 655040.54 624486.54 593120.10 556777.30 512227.51 465423.07 successful 838687.50 819561.45 794449.33 762414.79 723381.91 685223.08 642298.48 594156.29 53985 5.85 481083.34 418298.13 352622.94 285361.94 218967.22 152557.77 83586.23 13244.46 -5 8839.73 -131849.04 -206989.92 unsuccessful 838687.50 817780.17 787338.37 749271.51 704032.38 660869.92 618303.75 572539.46 52548 2.69 473952.79 421694.53 367522.09 307820.73 243565.43 178497.07 112111 .28 43287.20 - 28863.90 -104751.29 -185543.80 unsuccessful 838687.50 829780.37 823889.16 819989.62 817097.58 814945.16 811921.47 809420.16 80184 3.46 785174.33 760587.62 739149.29 713170.01 681650.88 648985.02 610163.93 566489.13 516591.07 462340.81 406279.60 successful 838687.50 823154.12 802320.21 781021.46 756444.84 734534.00 712696.21 684646.48 64728 5.22 606351.52 566529.99 524385.99 476719.67 426946.85 375245.08 317502.67 251375.64 179038.40 98493.26 9628.97 successful 838687.50 827154.31 806383.51 777305.67 752753.00 732040.84 705091.24 678555.44 64527 0.82 614137.62 576962.78 532760.68 485849.78 434174.23 375667.59 312466.91 243684.89 169209.35 87989.05 -1730.31 unsuccessful 838687.50 825527.48 805244.78 787460.94 770323.99 755217.45 742419.20 721983.89 69539 3.78 669778.46 641112.32 610084.37 572282.52 524240.97 470954.15 414539.83 347700.74 272641.03 188706.57 96574.56 successful 838687.50 831060.57 823116.86 815769.13 810109.94 797132.45 779764.25 759806.98 73875 4.53 715557.28 688552.90 659936.48 623157.20 577395.45 529064.05 473658.55 410808.78 343767.34 270940.04 191221.17 successful 838687.50 824967.68 801854.64 778621.67 758147.66 738970.22 713037.17 686397.45 65939 6.23 631418.71 599053.15 558757.36 515469.91 470167.74 415649.03 356539.98 293129.80 220597.98 141010.34 53635.93 successful Note that, because of the random nature of these results, the exact values in output. txt could differ even for the same input.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import random

def run_simulation(annual_spend, inflation_rate, savings_balance, interest_rate, num_years, inflation_change, interest_change):
    # Store savings in list
    savings = []
    # For each year:
    for year in range(num_years):
       # Update the annual spend by inflation rate
       annual_spend *= (1 + inflation_rate)
       # Reduce the savings balance by the annual spend
       savings_balance -= annual_spend
       # Update the savings balance by interest rate
       savings_balance *= (1 + interest_rate)
       # Add savings balance to savings in list
       savings.append(savings_balance)
       # Update inflation rate
       inflation_rate = modify_rate(inflation_rate, inflation_change)
       # Update interest rate
       interest_rate = modify_rate(interest_rate, interest_change)
    # Return savings in list
    return savings

def modify_rate(rate, change):
    """
       Calcualate a random value within the change of given rate
      
       Arguments:
       rate -- The intial rate to change from
       change -- The maximum amount to change of the given rate
      
       Returns a random value within change of the given rate
      
       Explation:
       random.random() calculates a random value between o and 1.
       Thus random.randm()*2 gives a value between 0 and 2,
       so 1 - random.random()*2 gives a value between -1 and 1.
       Multiplying by change gives a value between -change and change, which is then added to the rate.
       """
      
    return rate + change * (1 - random.random() * 2)

def main():
   
    # Last year expense
    while True:
       try:
            annual_spend = int(input("How much did you spend last year to support your current life style? "))
            if annual_spend >= 0:
                break
            else:
                print("Please enter a non-negative integer")
       except ValueError:
            print( "Please enter a non-negative integer")

    # Inflation rate
    while True:
       try:...
$45.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats