# Smart Investment

In the smart investment challenge, taken from the DMCommunity, we should pick out a set of stocks to buy to maximize their predicted gain within one year. The full problem is as follows:

Smart Investment

A client of an investment firm has $10000 available for investment. He has instructed that his money be invested in particular stocks, so that no more than $5000 is invested in any one stock but at least $1000 be invested in each stock. He has further instructed the firm to use its current data and invest in the manner that maximizes his overall gain during a one-year period. The stocks, the current price per share and the firm’s predicted stock price a year from now are summarized below:

Stock Current Price Projected Price 1 year ABC 25 35 XYZ 50 60 TTT 100 125 LMN 25 40

As always, we start by creating our glossary.
In this case, we will firstly want a way to talk about the four kinds of stocks: for this, we introduce a *type*.
Recall that a type represents a domain of values, which we can use to express more complex variables.

Type | ||
---|---|---|

Name | Type | Values |

Stock | String | ABC, XYZ, TTT, LMN |

Looking closer at the problem text, we can see that we need to represent five pieces of information for each stock:

Their current price

Their projected price

Their predicted gain

How many we buy

How much they are worth in total

All five of these can be excellently represented using *functions*.
A function represents a mapping from one or more types on another type, which in this case is useful to map each Stock element on a different type as follows.

Function | |
---|---|

Name | Type |

current price of Stock | Int |

projected price of Stock | Int |

Stock gain | Int |

nb Stock bought | Int |

dollars of Stock bought | Int |

Nearly done! All that’s left to express are a variable to represent how much money we start with, and a variable to represent the total worth after a year.
For this, we will rely on constants: these are functions, but without an input type.
As such, they represent exactly *one* value.

Constant | |
---|---|

Name | Type |

total invested | Int |

projected total | Int |

That’s it! Now, we can start modelling the problem logic. Luckily, these tables are fairly simple. We start with two decision tables, to define the total value of the stock we bought and to define the predicted stock gain after one year. As a reminder, the column “Stock = -” should be read as “For every stock”.

Define total value | ||
---|---|---|

U | Stock | dollars of Stock bought |

1 | - | nb Stock bought * current price of Stock |

Define stock gain | ||
---|---|---|

U | Stock | Stock gain |

1 | - | projected price of Stock - current price of Stock |

Next, we want to express that the total value of each stock should be between 1000 and 5000 dollars.
This is easy to do using a constraint table (denoted by hit policy *E**):

Min and max stock value | ||
---|---|---|

E* | Stock | dollars of Stock bought |

1 | - | [1000, 5000] |

Here, the constraint table allows us to specify that the value should be in a specific range, without having to specify a concrete value.
In contrast, decision tables *always* need to define a single value.

For the last part of logic, we still need to express how to define how to calculate the total invested and the projected total.
For this, we use decision tables using the sum hit policy *C+*.

Count invested | ||
---|---|---|

C+ | Stock | total invested |

1 | - | dollars of Stock bought |

Projected gain | ||
---|---|---|

C+ | Stock | projected total |

1 | - | nb Stock bought * Stock gain |

Basically, these first table sums the value of each stock, while the second table sums the number of each stock multiplied by its predicted gain.

Now we’re nearly done: all that’s left is representing the input data (our budget and the stock info), and what we want the cDMN solver to calculate.
For the former, we use cDMN data tables (denoted by *D* hit policy) as follows.

Stock Data | |||
---|---|---|---|

D | Stock | current price of Stock | projected price of Stock |

1 | ABC | 25 | 35 |

2 | XYZ | 50 | 60 |

3 | TTT | 100 | 125 |

4 | LMN | 25 | 40 |

Budget | |
---|---|

D | total invested |

1 | 1000 |

For the latter, we add a goal table to indicate to the cDMN solver that we want to find the maximum value for the projected total.

Goal |
---|

Maximize projected total |

Done! We can now run our model using the cDMN solver, which will tell us that 120 BAC, 20 XYZ, 10 TTT and 200 LMN will lead to an optimal projected total of 4650. :-) Not bad for one year!

```
Model 1
==========
current_price_of_Stock := {ABC -> 25, XYZ -> 50, TTT -> 100, LMN -> 25}.
projected_price_of_Stock := {ABC -> 35, XYZ -> 60, TTT -> 125, LMN -> 40}.
nb_Stock_bought := {ABC -> 120, XYZ -> 20, TTT -> 10, LMN -> 200}.
total_invested := 10000.
projected_total := 4650.
dollars_of_Stock_bought := {ABC -> 3000, XYZ -> 1000, TTT -> 1000, LMN -> 5000}.
Stock_gain := {ABC -> 10, XYZ -> 10, TTT -> 25, LMN -> 15}.
Elapsed Time:
0.574s
```