[docs]defts_mape(expected_y,predicted_y,sample_weight=None):""" Computes :math:`\\frac{\\sum_i | \\hat{Y_t} - Y_t |} {\\sum_i | Y_t - Y_{t-1} |}`. It compares the prediction to what a dummy predictor would do by using the previous day as a prediction. :param expected_y: expected values :param predicted_y: predictions :param sample_weight: sample weight :return: metrics """assertlen(expected_y)==len(predicted_y),f"Size mismatch {len(expected_y)} != {len(predicted_y)}."expected_y=numpy.squeeze(expected_y)predicted_y=numpy.squeeze(predicted_y)mask=numpy.isnan(predicted_y)mask2=mask.copy()mask2[1:]|=numpy.isnan(predicted_y[:-1])expected_y=numpy.ma.masked_array(expected_y,mask=mask)predicted_y=numpy.ma.masked_array(predicted_y,mask=mask2)ifsample_weightisNone:dy1=numpy.sum(numpy.abs(expected_y[:-1]-expected_y[1:]))dy2=numpy.sum(numpy.abs(predicted_y[1:]-expected_y[1:]))else:dy1=numpy.sum((numpy.abs(expected_y[:-1]-expected_y[1:])*sample_weight[1:]))dy2=numpy.sum((numpy.abs(predicted_y[1:]-expected_y[1:])*sample_weight[1:]))dy1=dy1.sum()dy2=dy2.sum()ifdy1==0:return0ifdy2==0elsenumpy.inftyreturndy2/dy1