Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

L1 and L2 norms (Euclidean and taxicab lengths) #2562

Open
1 of 4 tasks
bob-carpenter opened this issue Aug 23, 2021 · 8 comments
Open
1 of 4 tasks

L1 and L2 norms (Euclidean and taxicab lengths) #2562

bob-carpenter opened this issue Aug 23, 2021 · 8 comments

Comments

@bob-carpenter
Copy link
Member

bob-carpenter commented Aug 23, 2021

Description

We should have L1 and L2 norm functions in the Stan language. They have to be implemented in the math library first, which is what this PR is for. We want the functions, but ideally we want custom derivatives.

real norm1(vector x);
real norm1(row_vector x);

real norm2(vector x);
real norm2(row_vector x);

The definitions are:

norm1(x)  =def=  sum(abs(x))
norm2(x)  =def=  sqrt(sum(square(x)))

The derivatives are:

d/dx norm1(x) = signum(x)
d/dx norm2(x) = x / norm2(x)
  • implement norm1 and norm2 in the Stan Math library with autodiff test framework tests
  • replace other uses that calculate vector lengths with norm2
    • unit_vector constrain
    • ???

Current Version:

v4.1.0

@tvrsimhan
Copy link

I would like to work on this issue!

@bob-carpenter
Copy link
Member Author

Hi, @tvrsimhan. That'd be great. We accept pull requests for issues. Please let us know if you have any questions about how to implement it or test it (questions on the issue are fine).

There are two possible ways to do this: either by adding the functions with inefficient autodiff implementations or by adding custom gradients. We'd accept the former, but the advantage in compute speed is from the latter.

@tvrsimhan
Copy link

Thank you, @bob-carpenter! In which directory should this be implemented? I read through the guide but I couldn't locate those directories. Also, please point me to any other links I can refer to! I'll try my best and get back to you with any questions I have!

I'll try to add custom gradients, but I'll get back to you if I have any trouble with it!

@bob-carpenter
Copy link
Member Author

It should follow the other functions. That means a primitive (double) implementation in stan/math/prim/fun and then one with custom gradients in stan/math/rev/fun. The primitive version needs thorough testing, then the reverse mode should be tested in the mix directory for autodiff like the other matrix functions.

You can see how dot_self is implemented as it has the same signature:

@tvrsimhan
Copy link

Thank you for your guidance! I'll do my best!

@yizhang-yiz
Copy link
Contributor

@tvrsimhan any update on this issue? Thanks.

@SteveBronder
Copy link
Collaborator

@tvrsimhan the stan math docs have some examples to get started with as well

https://mc-stan.org/math/getting_started.html

@rok-cesnovar
Copy link
Member

Ah sorry, was too quick to close. #2636 addressed the first point of the issue, the second one is still open. Reopening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants