# Evaluate parametric metric with multiple bindings parameters

Did you know you can now evaluate a parametric metric with multiple values for the same variable?

Let’s suppose you have a metric with 2 variables variable1 and variable2

var m = SimpleMetric.make({
“id”: “MyParametricMetric_BaseAsset”,
“name”: “MyParametricMetric”,
“srcType”: “BaseAsset”,
“expression”: “variable1 * identity(2) + variable2”,
“variables”: [ {
“name” : “variable1”,
“dataType” : “Double”
}, {
“name” : “variable2”,
“dataType” : “Double”
} ]
})

You can call MyParametricMetric with two sets of variables in one call:

expressions: [“MyParametricMetric”],
ids: [“foo”],
start: “2018-01-01”,
end: “2018-01-02”,
interval: “DAY”,
bindings: [{“variable1”: 1, “variable2”: 1}, {“variable1”: 2, “variable2”: 2}]
}, [m]);

You would get 2 time-series in the result: (1,1) and (2,2)

You can also generate a cartesian product of all the binding parameters like this:

expressions: [“MyParametricMetric”],
ids: [“foo”],
start: “2018-01-01”,
end: “2018-01-02”,
interval: “DAY”,
bindingsCartesianProduct: true,
bindings: [{“variable1”: 1}, {“variable1”: 2}, {“variable2”: 1}, {“variable2”: 2}]
}, [m]);
You would get 4 time-series in the result: (1,1), (1,2), (2,1), and (2,2)

Finally you can provide a lambda function to rename the keys of your EvalMetricsResult based on the bindings parameters:

var resultKey = function(expression, bindings) {
var bindingsExpression = .reduce(.map(.keys(bindings), function(key) {return key + "" + bindings[key]}), function(s1, s2) {return s1 + “_” + s2;})
return expression + “” + bindingsExpression;
}

expressions: [“MyParametricMetric”],
ids: [“foo”],
start: “2018-01-01”,
end: “2018-01-02”,
interval: “DAY”,
bindingsCartesianProduct: true,
bindings: [{“variable1”: 1}, {“variable1”: 2}, {“variable2”: 1}, {“variable2”: 2}],
resultKey: Lambda.fromJavaScript(resultKey)
}, [m]);

You would get 4 time-series in the result named:
MyParametricMetric_variable2_1_variable1_1
MyParametricMetric_variable2_1_variable1_2
MyParametricMetric_variable2_2_variable1_1
MyParametricMetric_variable2_2_variable1_2

5 Likes

Note that all the resulting metric names from this approach such as `MyParametricMetric_variable2_1_variable1_1`, must be valid metric names. If for instance, you have a string variable, and one of the values it takes has characters such as `.`, `:` or `-` in it, this `resultKey` function won’t work. One way to get around this is to replace such characters with `_` or simply drop them (e.g. `STRING.replace(".","_")`) as follows:

``````var resultKey = function(expression, bindings) {
var bindingsExpression = _.reduce(_.map(_.keys(bindings), function(key) {return "_" + key + "_" + bindings[key].replace('.','_')}), function(s1, s2) {return s1 + "" + s2;})
return expression + "" + bindingsExpression;
}
``````