matrix.Matrix.if_

Matrix.if_(**criterion_to_value) → matrix.matrix.Matrix

The first method in the if-then chain syntatic sugar for declaring what score should a choice receive given the values for the criterion.

Keyword Arguments

**criterion_to_value (float) – The criterion (dictionary key) and a given value (dictionary value).

Raises

ValueError – * If the criterion is not continuous.

  • If the criterion has not been added yet; its weight is unknown.

Returns

Returns the instance so that it can be chained with the then() method.

Return type

Matrix

Examples

This example shows the declaration of a continuous criterion called cost. The usage is declarative in nature, mimicking natural language: “if the cost is 0 (dollars), then award a score of 10”

>>> import matrix
>>> m = matrix.Matrix()
>>> m.add_continuous_criterion('cost', weight=9)
>>> m.if_(cost=0).then(score=10)
>>> m.if_(cost=10).then(score=5)
>>> m.if_(cost=30).then(score=0)
>>> m.value_score_df
   cost  cost_score
0     0          10
1    10           5
2    30           0

If a choice does indeed has a price of 10$, a score of 5 will be assigned in its cost column

>>> m.add_choices('apple')
>>> m.add_data('apple', cost=10)
>>> m
|        |   cost | Percentage   |
|:-------|-------:|:-------------|
| Weight |      9 |              |
| apple  |      5 | 50.0         |

If a choice has a price between a specified value, such as 5$, then a simple linear interpolating function will be used as shown below.

>>> m.add_choices('orange')
>>> m.add_data('orange', cost=5)
>>> m
|        |   cost | Percentage   |
|:-------|-------:|:-------------|
| Weight |    9   |              |
| apple  |    5   | 50.0         |
| orange |    7.5 | 75.0         |

Notes

The interpolating function is calculated by assuming a linear function with points at \((0, 10)\) and \((10, 5)\). The x-axis is the cost, or the criterion value in general; the y-axis is the score to be assigned.

\[ \begin{align}\begin{aligned}\text{The gradient is: } m = \frac{(5-10)}{(10-0)} = -0.5\\\therefore y = (-0.5)x + c\\10 = (-0.5)(0) + c\\\therefore c = 10\end{aligned}\end{align} \]

Thus the interpolating function is \(y = (-0.5)x + 10\) for the domain \(0 \le x \le 10\)

If the price is 5$, then \(score = (-0.5)(5) + 10 = 7.5\)

See also

then()

The last method in the chain