Skip to content

Adds MAPE function to Loss functions - #13357

Closed
morgen-code wants to merge 3 commits into
TheAlgorithms:masterfrom
morgen-code:master
Closed

morgen-code wants to merge 3 commits into
TheAlgorithms:masterfrom
morgen-code:master

Conversation

@morgen-code

Copy link
Copy Markdown

Describe your change:

Fixes #13311
Adds the algorithm to calculate mean_absolute_percentage_error in loss_functions.py file in machine_learning directory.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Sep 10, 2026

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! The math is right, but CI is red (ruff / pre-commit / build) and there are a few copy-paste issues to fix before this can go in:

  1. Doctests call the wrong function. They invoke symmetric_mean_absolute_percentage_error(...), but this PR defines mean_absolute_percentage_error. So the doctests are actually testing SMAPE, not MAPE. Change the calls to mean_absolute_percentage_error. (The expected value 0.058333… is in fact correct for MAPE on that data — 0.1, 0.05, 0.0333, 0.05 averaged — so just fixing the function name makes it pass.)
  2. Docstring formula/name. It reads SMAPE = …; it should say MAPE = (1/n) * Σ( |y_true - y_pred| / |y_true| ).
  3. Indentation. The docstring body is over-indented with mixed leading whitespace, which is part of the lint failure. Align everything to 4 spaces under the opening """.
  4. Line length. The >>> float(mean_absolute_percentage_error(true_values, predicted_values)) line will likely exceed the 88-char limit — shorten the local variable names (e.g. true/pred) so it fits.

Once those are fixed the doctests and ruff should pass. Also heads-up: #13355 (SMAPE) touches the same block of loss_functions.py, so a rebase may be needed depending on merge order.

@cclauss

cclauss commented Sep 11, 2026

Copy link
Copy Markdown
Member

@morgen-code @priya-sundaram-dev Can either of you manage this rebase?

@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

Before anyone spends time on the rebase — heads up that this one looks superseded. machine_learning/loss_functions.py on master already defines mean_absolute_percentage_error (currently around line 439, added in an earlier PR), with the same (y_true, y_pred, epsilon=1e-15) signature and passing doctests. That's also the source of the conflict here.

So rather than rebasing, I think #13357 can be closed as a duplicate. Two small notes for the record, in case anything from here is worth folding into the existing function:

  • The doctests in this PR call symmetric_mean_absolute_percentage_error(...) rather than mean_absolute_percentage_error(...), and the formula comment says SMAPE = ... — looks like a copy-paste from the SMAPE PR, so they wouldn't pass as written.
  • The existing master version already handles the divide-by-zero guard via epsilon.

Thanks @morgen-code for the contribution and the interest in the loss-function module! Happy to help if you'd like to pick up a different addition. @cclauss let me know if you'd prefer I do anything else here.

@cclauss cclauss closed this Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding SMAPE and MAPE loss functions in Machine Learning

3 participants