How to use stan::math::hypergeometric_2F1 in stan?

I was trying to call my stan model by using rstan in R. It seems like stan does not has built-in hypergeometric_2F1 function. This function is only available in stan math. Is there any way to import stan math function directly into a stan model? Thanks a lot!

1 Like

Sorry nobody answer that. And I’m not 100% sure myself. If it’s in Stan math, you might try declaring its signature up in the functions block and see if that works. I’m not sure about the signature you want for the hypergeometric function, but if there’s a function in the math library with two real arguments and a real return, you’d use this:

functions {
  real foo(real a, real b);

There is an issue for adding this to the language: Expose `hypergeometric_2F1` and `hypergeometric_3F2` · Issue #1285 · stan-dev/stanc3 · GitHub

In the mean time, something close to @Bob_Carpenter’s suggestion could work, but you need a bit of C++. How to wire this together will depend on your interface, for cmdstan it can be found here

File user_header.hpp:

#include <stan/model/model_header.hpp>
#include <ostream>

template <typename T1, typename T2, typename T3, typename T4>
auto hypergeometric_2F1(const T1& a, const T2& b, const T3& c, const T4& z, std::ostream* unused = 0) {
  return stan::math::hypergeometric_2F1(a, b, c, z);

file hyper.stan

functions {
   real hypergeometric_2F1(real a, real b, real c, real z);

parameters {
    real a;
    real b;
    real c;
    real z;

model {
    target += hypergeometric_2F1(a, b, c, z);

Obviously the model is a bit nonsensical here, but this will compile (locally I ran make hyper STANCFLAGS=--allow-undefined).

1 Like