0

Lets say we have an initial fortune of 10$ and we want to make money by buying and selling potatoes and in a magical way we know the prices($) per kg per year of each potato spice. What is the best algorithm to find the final maximum fortune or a local maximum?

!! If you have a fortune 15$ and you sell some kg in a year and earn lets say 4$ more you cant buy something that costs you 19$ at the same year, they are independent so you should have the money to buy something before selling something else in the same year !!

e.g

starting fortune : 10$

Prices($) per kg of potato spice per year:

  • Potato Spice 1 price($) per kg:
    [5, 8, 7, 10, 12, 11, 14, 11, 10]

  • Potato Spice 2 price($) per kg:
    [8, 8, 4, 5, 7, 15, 10, 12, 10]

  • Potato Spice 3 price($) per kg:
    [4, 7, 5, 6, 10, 9, 11, 15, 11]

What will be a good solution for that?

A simple local maximum can be made by the following path:

  • 1st Year:
    [Buy 1 kg (5$) from first spice]
    [Buy 1.25 kg (5$) from third spice]
  • 2nd Year:
    [Sell 1kg (8$) from first spice]
  • 3rd Year:
    [Buy 2 kg (8$) from second spice]
  • 4th Year: nothing
  • 5th Year: nothing
  • 6th Year: nothing
  • 7th Year: nothing
  • 8th Year:
    [Sell 1.25 kg (-15x1.25=18.75 $) from third spice]
    [Sell 2 kg (12x2=24 $) from second spice]
  • 9th Year
    nothing

Fortune 42.75$ (this was an example, for sure not the maximum fortune)

P. M. K
  • 13
  • 6

1 Answers1

0

Based on your example, I'm assuming that money and potatoes are continuous quantities. This means that our maximum value of our fortune in any given year can be linearly separated into the value of each potato type, plus the value of cash.

Let P(i, k) be the price per kilogram of potato i in year k. Define V(i, k) to be the value of 1 kg of potato i in year k, and let F(k) be the value of $1 in year k. The maximum fortune starting from d dollars is d * F(0).

As a base case, in the last year k_max we have:

V(i, k_max) = P(i, k_max)
F(i, k_max) = 1

We can compute the remaining values of V and F through a mutual recursion as follows:

  • In a given year k, for each type of potato i, we can either keep our potatoes till the next year, or sell them for cash:

    V(i, k) = max(V(i, k+1), P(i, k) * F(k+1))

  • In a given year k, we can either buy some potato i (for any value of i) or hold onto our cash till the next year:

    F(k) = max(V(i, k+1)/P(i, k), F(k+1))

Note that due to the linear separability, there's no benefit in "mixed" strategies. In a given year, we should either invest all our cash in the most profitable potato type, or not invest at all. Likewise, for each potato type and year we should either sell all our holdings of that type, or keep them till the next year.


Let's look at the results for your example. The following table shows the optimal strategy computed using this approach. For each year, B means we should invest our money in that potato type, and S means we should sell off that potato type. - means we should neither buy nor sell that potato type for that year.

    Year 1 2 3 4 5 6 7 8 9
Potato 1 - S - S S S S S S
       2 S S B B B S - S S
       3 B S S S S B B S S

Starting with $10, this gives us:

  • Year 1: Buy 2.5 kg of potato 3 for $10
  • Year 2: Sell 2.5 kg of potato 3 for $17.50
  • Year 3: Buy 4.375 kg of potato 2 for $17.50
  • Year 4: Do nothing
  • Year 5: Do nothing
  • Year 6: Sell 4.375 kg of potato 2 for $65.625
  • Year 7: Buy ~5.97 kg of potato 3 for $65.625
  • Year 8: Sell ~5.97 kg of potato 3 for ~$89.49
  • Year 9: Do nothing
augurar
  • 12,081
  • 6
  • 50
  • 65