Coder Social home page Coder Social logo

vectorize _rng functions about math HOT 8 CLOSED

stan-dev avatar stan-dev commented on August 15, 2024
vectorize _rng functions

from math.

Comments (8)

bob-carpenter avatar bob-carpenter commented on August 15, 2024

Would also be good if the vectorized RNGs would accept vector arguments like dnorm in R and broadcast the smaller ones. We already have the VectorView framework in place for this. With vector arguments, we don't need a redundant numerical argument, so there are really two extensions:

  • vector argument(s)
  • number of repetition arguments with scalars

The second is much easier as it's basically just a bunch of loops.

from math.

bob-carpenter avatar bob-carpenter commented on August 15, 2024

For example, for the normal distribution, we would want to support these three signatures in Stan:

vector normal_rng(vector mu, real sigma);
vector normal_rng(real mu, vector sigma);
vector normal_rng(vector mu, vector sigma);

with broadcasting of the scalar to a vector. When two vectors are present, they must be the same size.

The existing implementations can be generalized using the expression template scalar_seq_view in the same way as the log probability density functions. That lets you view either a scalar or sequence as a sequence and write a single implementation.

A second kind of vectorization that would be convenient would be to have:

vector normal_rng(int N, real mu, real sigma);

to generate N draws with a single pair of parameters.

R just uses a single function that combines the two patterns, requiring an N, and a scalar or vector location and a scalar or vector scale parameter.

from math.

bbbales2 avatar bbbales2 commented on August 15, 2024

I opened a pull req. to get started on this: #573 . Hopefully I didn't go too far off the beaten path.

from math.

bob-carpenter avatar bob-carpenter commented on August 15, 2024

I didn't realize you were working on this, as I just suggested to someone else that they work on it. I'll try to fish out your email and respond that way as I don't know the other person's GitHub handle, so I can't call it out here.

from math.

bob-carpenter avatar bob-carpenter commented on August 15, 2024

I will take a look when I get a chance, but I'm getting bombarded by people wanting me to look over code these days.

from math.

bbbales2 avatar bbbales2 commented on August 15, 2024

Haha, I wasn't working on it till yesterday -- it's no problem if the pull I made gets replaced. I was just getting annoyed at having to write for loops for things that in the model were nicely vectorized so I figured I'd give it a go.

from math.

rok-cesnovar avatar rok-cesnovar commented on August 15, 2024

@bbbales2 We can close this one right?

from math.

bbbales2 avatar bbbales2 commented on August 15, 2024

I think so.

from math.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.