Random walk in ulam

Open
#423 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

The error comes from get_dist_template while ulam parses the alist model, so the problem is the loop-based random walk written inside the model block, not the data. Start by running the minimal example to reproduce the error, then check how ulam handles for loops and indexed assignments in its model parser. Done means the random-walk model compiles and samples, or the maintainer confirms the supported pattern.

Written by the indexing model from the issue text.

Description

I have been trying to implement a random walk in ulam. I'd be interested in how to do this in general after reading discussions such as this and this on how to build random walks in Stan and its other R interfaces such as brms.

Here is a MRE for a specific case trying to model the decay parameter k of the exponential decay function $f(x)=ae^{-kx}$ as a function of x using random walk:

# generate data
set.seed(10)
x <- 1:250
r <- rnorm(x, mean = 0, sd = sqrt(x^-1.5))
y <- 2 * exp(-0.04 * x) + r

require(tidyverse)
df <- tibble(x = x, y = y)
ggplot(df, aes(x, y)) +
  geom_point() +
  theme_minimal()

# prepare data for ulam
l <- as.list(df)
l$N <- length(l$x)

# run model
require(rethinking)
rw.mod <- ulam(
  alist(
    y ~ dnorm(mean = mu, sd = sigma),
    mu <- a * exp(-k * x),
    k <- 0.04,
    for (i in 2:N) {
      k[i] <- k[i - 1] + r
    },
    r ~ dnorm(mean = 0, sd = tau),
    a ~ dlnorm(meanlog = log(2), sdlog = 0.5),
    c(sigma, tau) ~ dexp(rate = 1)
  ), data = l, chains = 8, cores = parallel::detectCores(), iter = 1e4
)

This yields the following error: Error in get_dist_template(the_dist[1]) : No template for distribution ':'

I also tried specifying the vector 2:N in the for loop as seq(from = 2, to = N, by = 1), in which case I get the same error: Error in get_dist_template(the_dist[1]) : No template for distribution 'seq'

Any solutions or pointers would be greatly appreciated!

Dominant language
R
Stars
2.4k
Forks
632
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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.

More from rmcelreath/rethinking

All issues in rmcelreath/rethinking

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.