stan-dev / stan-dev/math

Add lower and upper bounds to ordered vector

Open
#2,959 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

The proposal is to have the following

ordered<lower = a>[N] x1;
ordered<upper = b>[N] x2;
ordered<lower = a, upper = b>[N] x3;

For scalar a, b this is straightforward. When a, b are vectors they have to be ordered vectors themselves. In the case of having both a lb and ub then each element of a must be less than each element of b.

Here is Stan code for the scalar case

  vector ordered_lb_lp (vector y, real lb) {
    int N = rows(y);
    vector[N] x;
    
    x[1] = exp(y[1]) + lb;
    target += y[1];
    
    for (i in 2:N) {
      x[i] = exp(y[i]) + x[i - 1];
      target += y[i];
    }
    
    return x;
  }

  vector ordered_ub_lp (vector y, real ub) {
    int N = rows(y);
    vector[N] x;
    
    x[1] = ub - exp(y[1]);
    target += y[1];
    
    for (i in 2:N) {
      x[i] = x[i - 1] - exp(y[i]);
      target += y[i];
    }
    
    return x;
  }
  
  vector ordered_lb_ub_lp (vector y, real lb, real ub) {
    int N = rows(y);
    vector[N] x;
    
    x[1] = lb + (ub - lb) * inv_logit(y[1]);
    target += log(ub - lb) + log_inv_logit(y[1]) + log1m_inv_logit(y[1]);
    
    for (i in 2:N) {
      x[i] = x[i - 1] + (ub - x[i - 1]) * inv_logit(y[i]);
      target += log(ub - x[i - 1]) + log_inv_logit(y[i]) + log1m_inv_logit(y[i]);
    }
    
    return x;
    
  }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the scalar transformations shown in the issue and the requirements for scalar and vector bounds. Done means supporting lower-bounded, upper-bounded, and doubly bounded ordered vectors, including the stated ordering constraints for vector bounds.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.