Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python programing problem AND HERE'S THE PYTHON CODE FOR THE INVESTING MACHINE THAT PROVIDED Please write down the python code in programming 1026B: Assignment 2:
Python programing problem
AND HERE'S THE PYTHON CODE FOR THE INVESTING MACHINE THAT PROVIDEDPlease write down the python code in programming
1026B: Assignment 2: How to Invest Your Money? Due: February 27 (9pm) Weight: 8% Goal Use Python to solve interesting real-world problem, with an emphasis on problem-solving and algorithm design skills By completing this assignment, you will gain Python skills relating to using loops defining & using functions using lists using imported modules and functions - There are two "investment machines". If you put $X in one machine, there is 60% of chance it will return $2"X (and 40% returning $0); if you put $X in the other machine, there is 40% of chance it will return $2"X (and 60% returning $0). You just don't know which machine is which! You have a total of $100 to start with, and you can only invest a maximum of 30 times. The minimum amount to invest is $0 with $1 minimum incremental. Part 1 of this assignment: Use Python to implement the following "vanilla" investment strategy: 1. Put $1 in each investment machine for 10 times to see which one returns more money (thus, you have invested a total of 20 times). Break tie whatever way you prefer 2. For the remaining money, always invest half (of the current remaining total money) in the more profitable machine discovered each time, for the last 10 times of investment. Simulate this strategy for 1000 times. Print out the average amount of return, the standard deviation, and the Sharpe Ratio you end up with using this strategy The stan learn how to calculate "std" with the python numpy module). The Sharpe Ratio is a way to evaluate the performance of an investment by adjusting for its risk. The ultimate goal of the investment is to maximize the Sharpe Ratio, defined as follows: n measures how "spread out a set of numbers is. (see sample code below to (average gain) / (standard deviation 1) where average gain-average return-risk free amount (in this case: the risk free amount = $100). That is, we seek for a higher return, with relatively low volatility. To avoid the rare situation where the standard deviation is 0, we add 1 in the demometer. Part 2 of this assignment: Design and implement a better investment strategy (with a larger Sharpe Ratio) than the "vanilla" one above. [Bonus] Students with the top 3 Sharpe Ratios will receive 1% (added to the final mark) as bonus; the next 3 students will receive 0.5% as bonus. Useful background info: In artificial intelligence (AI), the first step in the "vanilla" approach above is often called "exploration" which uses trial-and-error to see which option is best. The second step is often called "exploitation" which exploits the newly learned best option to make a maximum "profit" Exploration and exploitation can be interleaved and integrated in sophisticated and intelligent ways to achieve an optimal outcome In real-world, things are often much more complicated. For example, there are usually more investments to choose from, each with an unknown distribution of returns which may vary over time, and so on. This type of problems in this assignment do have widespread important applications. The "investments" can be the restaurants you are exploring and enjoying in a new city, different subjects you try to choose in universities, or different jobs/career you want to work for in your life. The assignment may also be helpful to improve your rational decision-making under uncertainty The Python code for the two investment machines will be provided to you as an independent python file called investment-machine.py in which a python class InvestmentMachine, was defined with a function called 'invest. Do not modify investment_machine.py, just put it with your own python file in the same directory The following sample Python program invests $3 in each machine for 15 times, and print out the final total. It may help you start with your assignment. Save the following codes in any_name.py, in the same directory as investment machine.py. Load and run any_name.py in PyCharm. You would get a total return value of around $100. (Here Sharpe Ratio is not calculated, but it would be around 0). #import Investment Machine class from investment machine import InvestmentMachine #initialize investment machine instance my_ investment machines InvestmentMachine() total reward machine 1 0 total reward machine 2 0 #invest to machine 1 with $3 for 15 times for times_machine 1 in range(15): # v1 +-v2 is shorthand for v1-v1 + v2 total reward machine 1my investment machines.invest 1, 3) # The first argument is for machine 1 or machine 2, randomly assigned to have 40% and 60% return # The second argument is for the amount of money put in. #invest to machine 2 with $3 for 15 times for times_machine_2 in range(15): total reward machine 2+2 my investment machinesinvest(2,3) print( The total return of my investment strategy is $, total_reward machine_1+total_reward machine 2) u need to save the total amount of money in a list of size 1000, then For the 1000 simulations, you computer the average amount of money, the standard deviation, and the Sharpe Ratio. To simplify your coding, you can use the existing function "std" to calculate the standard deviation. See belovw #################sample standard deviation calculation############## #import numpy module import numpy as np #generate a list of 10 random numbers value list np.random.rand(10) print(values in the list are ", value list) sd = np.std(value_list) print( Standard deviation is ", sd) You must define and use the following functions in your final submission: a. Define and use the main function b. Define and use a function for repeated investment of a fixed amount in one machine. The function should take three arguments: the machine number, the money you invest each time, and total times you invest with the fixed amount. You will see the benefit of defining such a function, which is an abstraction for repeated calculations. c. Define and use a function for repeated investment of a fixed percentage of the current total money you have in one machine. The function should take three arguments: the machine number, the fixed percentage you invest each time, and total times you invest with the percentage d. You can define any other functions if needed. Note: The assignment is to be done individually and must be your own work. Software may be used to detect cheating What You Will Submit and Be Marked On: 1. A 1-3 page written part (with Word, text, PDF file) about: Part 1: A brief problem-solving process you use in designing and implementing the "vanilla" strategy and its output. Part 2: A detailed problem-solving process you use in designing and implementing your own better investment strategy (with a larger Sharpe Ratio than the "vanilla" one) and its output . . 2. Your Python source file(s). Make sure that your code meets the requirements (such as using functions and list described above). The name of the Python program you submit should be your UWO userid_Assign2.py. Make sure you attach your Python file to your assignment; DO NOT put the code inline in the textbox or the written part above. Make sure that you develop your code with Python 3.7 as the interpreter. TAs will not endeavor to fix code that uses earlier versions of Python. 3. Non-functional specifications: as described belovw Non-functional Specifications 1. Include brief comments in your code identifying yourself, describing the program, and describing key portions of the code from random import random class InvestmentMachine def _init__(self): In this case, when you initialize this InvestmentMachine instance, we assume there are fixed two investment machines with fixed winning probabilities [0.6, 0.4] that will be assigned to these two machines randomly The reward rate will be 200%, eg, if you invest $1, play first machine (No. 1s 1) and you win, you will get $2 self.total_machines2 self.reward rate 2 self.reward_probability_distribution-[e, 0] if(random() 0.5): self.reward_probability_distribution - [0.4, 0.6] else self.reward_probability_distribution-[0.6, 0.4] def invest(self, machine_number, investment_amount): @param: armNo: arm number you pull, in this case, should be 1 / 2 return: reward coins count if you win, otherwise return 0 if machine_number not in [1, 2]: inputCoin: coins you put for this round of play raise Exception( "INVESTMENT MACHINE ] [ERROR] the machine NO. " + str(machine_number) "is out of range, total machines count is " +str(self.total_machines)+" please set 1/2 as argument") win_probabilityself.reward_probability_distribution[machine_number 1] if(random()Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started