stan-dev / stan-dev/rstan

Argument `iter` in `optimizing` does not work properly

Open
#680 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

The argument iter in optimizing does not seem to work as expected.

Description:

Summary says it all, see below for reproducible steps.

Reproducible Steps:

Consider simple stan model:

data {
  int n;
  vector[n] x;
}

parameters {
  real mu;
}

model {
  target += normal_lpdf(x | mu, 1);
}

Now if we call optimizing with iter=0 I would expect that the program returns the initial value, but it doesn't.

library(rstan)

# create some synthetic data
set.seed(123)
x <- rnorm(10)+5
model <- stan_model('simple.stan')
data <- list(x=x, n=length(x))

fit <- optimizing(model, data=data, hessian=T, as_vector=F, init=list(mu=0), iter=0)
print(fit$par$mu) # should print 0
print(mean(x)) # just for comparison

This prints

[1] 5.074626
[1] 5.074626

In other words, optimizing optimizes mu to the MAP estimate, so iter has no influence.

A weird thing is that when I change the init, it does not necessarily iterate until MAP is reached.

fit <- optimizing(model, data=data, hessian=T, as_vector=F, init=list(mu=10), iter=0)
print(fit$par$mu) # should print 10
print(mean(x)) # just for comparison

This prints

[1] 9.507463
[1] 5.074626

I stumbled upon this when I tried to evaluate the Hessian of the log posterior with some fixed values of parameters, and thought I could achieve this by fixing iter=0.

RStan Version:

2.19.2

R Version:

"R version 3.6.0 (2019-04-26)"

Operating System:

OS X 10.14.6

Contributor guide

No contributing guide indexed for this repository

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 with the optimizing entry point and run the reproducible R example using simple.stan, init=list(mu=0), and iter=0. Compare the returned parameter with the supplied initial value and verify that zero iterations no longer optimize the parameter while preserving the intended Hessian evaluation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.