Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Problem HERE'S THE CODE PROVIDED FOR INVESTMENT MACHINE Please have the Python code written down Goal: Use Python to solve interesting real-world problem, with

Python Problem

image text in transcribedimage text in transcribedimage text in transcribed

HERE'S THE CODE PROVIDED FOR INVESTMENT MACHINEimage text in transcribedPlease have the Python code written down

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 usi ng imported modules and functions IN V ES T 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: 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. 1. 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 standard deviation measures how "spread out" a set of numbers is. (see sample code below to 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: (average gain) (standard deviation 1) where average gains 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) 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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions