In: Computer Science
You have corrupt friends in Wall Street that supply
you with the following information:
the stock price of ten fraudulent companies every month for a year.
You have access to the
data for the entire year in advance. You would like to use this
information to get as rich as
possible by the end of the year.
At the start of every month k, you look at the table S[i,k], which
gives you the
average return on
on a $1 investment in company i during the entire month k. You
would like to invest $100 000
per month. You want to invest the entire sum in one company at a
time. By the end of the
month, you either keep all your investments in one company for the
next month, or your sell
your stocks, and invest the $100 000 in a new company. Every month
k, you make 100 000 * S[i,k] of profit for investing in company i.
You have to pay $7500 to Wall Street every time you
sell your stocks. This means that if you keep your investment in
the same company from month
k to month k+1, you do not pay the $7500 fee.
(a)
Write a dynamic programming algorithm to guide your investments.
Which company should
you invest in each month? You are given table S[0 <= i <=9, 0
<=k <=11] as input.
Store your answer in a table C, where C[k] = i indicates investing
in company i at month k.
(b) Would your dynamic programming solution be simpler if you
didn’t have to pay $7500 to
sell stocks? Explain your answer.
Algorithm to be written in python
Let's first see the main objective and the decision we have to actually make in this particular problem.
Aim- To gain maximum returns possible on every $100000 invested every month.
Decision to take-
So, here our base condition is deciding the company to invest in and whether the company should be changed.
Algorithm-
Note- here, the value of the index could have been found out in the same loop, without having to use the second loop, that would have been more feasible solution, in fact instead of checking the maximum value by comparing each term, we could have used the MAX() function present in Python. But I didn't use it just because my aim is to give clear understanding of what needs to be done exactly. Please do make use of MAX() function to enhance the quality of the algo.
Now for the investment made for the subsequent months.
This step is OPT[1] and similarly OPT[2], and so on
The steps here would be the same for all the subsequent months.
I have divided the alogorithm into two parts, these can also be combined if needed. But there would be two major parts, the investment made in first month when k=0 would be the first part, the investment made in the subsequent months k>=1 would be the second part.
Part (b) of the question
Yes, the algorithm would have been easier had there been no payment of $7500 to be made. This is because, in algorithm presently, we have to take two cases, first for the investment made in first month and second for the investment made in months after the first month. Also, for the investment in the second month and so on, we not only have to look for the company giving maximum profit, but we also have to check whether the company is giving overall profit greater than $7500. If there had been no case of $7500, our algorithm would have only one part and almost the same process would have been followed for all the months. The only decision to change the company would have been made on the basis of maximum profit. We only would have to check if the profit by present company for the month k+1 ( the next month) is highest or not, if yes, then continue in the same company. If no, then check company gives the highest profit and invest there. But, in case of time complexity, there is not much change. In the present scenario, we have to move one extra step, in which we are checking whether the overall profit made by the company giving maximum profit (profit on $100000) which is not the same as present company, is greater than $7500.