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

Question #2

Closed
yebai opened this issue Dec 13, 2024 · 2 comments
Closed

Question #2

yebai opened this issue Dec 13, 2024 · 2 comments

Comments

@yebai
Copy link

yebai commented Dec 13, 2024

Hi, @nsiccha, README contains the following example

julia_implementation(::Val{:earn_height}; N, earn, height, kwargs...) = begin 
    @stan begin 
        @parameters begin
            beta::vector[2]
            sigma::real(lower=0.)
        end
        @model begin
            earn ~ normal(@broadcasted(beta[1] + beta[2] * height), sigma);
        end
    end
end

Is there a reason for @broadcasted instead of more Julian broadcasting syntax

   earn ~ normal((beta[1] + beta[2] .* height), sigma); 
@nsiccha
Copy link
Owner

nsiccha commented Dec 13, 2024

@Broadcasted replaces Base's @., but doesn't materialize the result, ie doesn't allocate. Instead, the broadcasted expression gets fed into the lpdf function (compare

@inline normal_lpdf(x, mu, sigma) = -bsum(@broadcasted(log(sigma)+.5*square((x-mu)/sigma)))
), where it then gets fed into another (allocation free) @Broadcasted, which then finally gets reduced in a (again allocation free) way that tries to be clever about not doing potentially expensive computation repeatedly (compare the functions in the block starting here: ).

I think initial Benchmarking revealed that to be faster than doing it the "standard" way. I think that Stan either already does such an optimization OR is conceivably able to do it via Eigen (C++) expressions, ie without the "user" having to do additional stuff like adding the @Broadcasted macro.

Doing it this way, including implementing the lpdf functions myself instead of relying on distributions.jl, made that optimizations easier to do.

@yebai
Copy link
Author

yebai commented Dec 13, 2024

Thanks, very helpful.

@yebai yebai closed this as completed Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants