stan-dev / stan-dev/rstan

Error reading from connection when controls are added to the model

Open
#953 2 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 issue arise when I try to add control in a model. While I can have rstan up and running, when I try to increase the delta with the command "control=list(adapt_delta=0.9)" it throws the error: "Error in unserialize(socklist[[n]]) : error reading from connection". I was wondering whether there might be an issue when pasting the controls to the model.

Description:

I have a Stan model that returns some divergence - 3 for 2000 iterations in four chains. I already used non-centric parametrisation but the only way to get ride of these divergence is to slightly increase the step size for the NUTS algorithm. While there was no issue to do so in the past, it was recently impossible to do. I install rstan on my Mac and my Window computer and the issues are persistent in both cases.

To be clear: the model runs as long as I don't specify the "control" parameter. If I specify a control parameter with an empty list the model still runs. As long as I try to specify any specifics in the control parameter - adapt_delta, max_treedepth, etc. - the sampling is not done and the error "Error in unserialize(socklist[[n]]) : error reading from connection". This issue did not exist in the past and is persistent across operating systems (Mac and windows).

Reproducible Steps:

Here is an example of the "eight school model"

model_code <- "
  data {
    int<lower=0> J;          // number of schools 
    real y[J];               // estimated treatment effects
    real<lower=0> sigma[J];  // s.e. of effect estimates 
  }
  parameters {
    real mu; 
    real<lower=0> tau;
    vector[J] eta;
  }
  transformed parameters {
    vector[J] theta;
    theta = mu + tau * eta;
  }
  model {
    target += normal_lpdf(eta | 0, 1);
    target += normal_lpdf(y | theta, sigma);
  }
  
"


model.data <- list(J = 8,
                   y = c(28,  8, -3,  7, -1,  1, 18, 12),
                   sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

fit.working <- stan(
  model_code =model_code,  
  data = model.data,
  chains = 4,   
  warmup = 1000,  
  iter = 2000, 
  cores = 4
)

## Here the model runs.
# Message of the outputs: 
# Warning messages:
# 1: There were 2 divergent transitions after warmup. See
# http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
# to find out why this is a problem and how to eliminate them. 
# 2: Examine the pairs() plot to diagnose sampling problems

fit.notworking <- stan(
  model_code =model_code,  
  data = model.data,
  chains = 4,   
  warmup = 1000,  
  iter = 2000, 
  cores = 4,   
  control=list(adapt_delta=0.9) ## Or any otehr control
)

## Here the model does not run:
# Meassage output: 
# SAMPLING FOR MODEL '8e0b427f57af3571c0dcc2e3d408b556' NOW (CHAIN 1).
# Error in unserialize(socklist[[n]]) : error reading from connection
# 
# SAMPLING FOR MODEL '8e0b427f57af3571c0dcc2e3d408b556' NOW (CHAIN 2).
# 
# SAMPLING FOR MODEL '8e0b427f57af3571c0dcc2e3d408b556' NOW (CHAIN 3).


fit.also.working <- stan(
  model_code =model_code,  
  data = model.data,
  chains = 4,   
  warmup = 1000,  
  iter = 2000, 
  cores = 4,
  control=list()
)

## Here the model also runs
# Output message
# Warning messages:
# 1: There were 3 divergent transitions after warmup. See
# http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
# to find out why this is a problem and how to eliminate them. 
# 2: Examine the pairs() plot to diagnose sampling problems


RStan Version:
packageVersion("rstan")
[1] ‘2.21.2’
R Version:

R 4.1.0

Operating System:

OS X 11.4 but also on a window computer.

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 reproducible eight schools model and compare the stan() calls using control=list() and control=list(adapt_delta=0.9). Inspect how rstan handles control values during sampling; done means specific controls no longer cause the unserialize connection error while sampling completes.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.