Scenario tree generation for multiperiod financial optimization by optimal discretization