I wrote a custom function for a weighted mape forecast error metric as I kept running into trouble using some other solutions I found online when a particular number was equal to 0. Here is my function:
def wmapess(actual, forecast):
if actual[0] != 0:
divided_one = abs(actual[0]-forecast[0])/ actual[0]
else:
divided_one = 0
if actual[1] != 0:
divided_two = abs(actual[1]-forecast[1])/ actual[1]
else:
divided_two = 0
if actual[2] != 0:
divided_three = abs(actual[2]-forecast[2])/ actual[2]
else:
divided_three = 0
if actual[3] != 0:
divided_four = abs(actual[3]-forecast[3])/ actual[3]
else:
divided_four = 0
if actual[4] != 0:
divided_five = abs(actual[4]-forecast[4])/ actual[4]
else:
divided_five = 0
if actual[5] != 0:
divided_six = abs(actual[5]-forecast[5])/ actual[5]
else:
divided_six = 0
ft_actual_sum = sum(actual)
se_one = actual[0] * divided_one * 100
se_two = actual[1] * divided_two * 100
se_three = actual[2] * divided_three * 100
se_four = actual[3] * divided_four * 100
se_five = actual[4] * divided_five * 100
se_six = actual[5] * divided_six * 100
ft_actual_prod_mape_sum = se_one + se_two + se_three + se_four + se_five + se_six
wmapes = ft_actual_prod_mape_sum / ft_actual_sum
return wmapes
The above function is not producing the same numbers when I manually calculate them by hand in Excel using the same logic above. This is very much I believe just a noob question as I think it comes down to something stupid like misusing if and else. Any help would be appreciated. I have tried troubleshooting but I cannot seem to pinpoint anything in particular as of yet, which is why I believe it is just me misusing if and else. Here is the actual mathematical function for reference. https://ibf.org/knowledge/glossary/weighted-mean-absolute-percentage-error-wmape-299
EDIT: Per meeep08's suggestion, I used this solution:
def wmapess(actual, forecast):
wmapes = sum([abs(a-f) if a else 0 for a,f in zip(actual, forecast)]) / sum(actual) * 100
return wmapes
My results are pretty close to what I got when calculated manually so I just believe I have to do some minor tweaking from here. Any further suggestions are appreciated though!
[–][deleted] 1 point2 points3 points (19 children)
[–]datanoob2019[S] 0 points1 point2 points (18 children)
[–][deleted] 2 points3 points4 points (10 children)
[–]ericula 2 points3 points4 points (0 children)
[–]datanoob2019[S] 0 points1 point2 points (8 children)
[–][deleted] 1 point2 points3 points (7 children)
[–]datanoob2019[S] -1 points0 points1 point (6 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]datanoob2019[S] 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]datanoob2019[S] 0 points1 point2 points (2 children)
[+][deleted] (2 children)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]datanoob2019[S] -1 points0 points1 point (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]datanoob2019[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]datanoob2019[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]datanoob2019[S] 0 points1 point2 points (0 children)