# Partial sum error

**URL:** <https://discourse.mc-stan.org/t/partial-sum-error/36384>\
**Category:** Modeling\
**Tags:** rstan\
**Created:** [August 27, 2024, 3:35pm UTC](https://discourse.mc-stan.org/t/partial-sum-error/36384 "2024-08-27T15:35:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paparize](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Paparize](https://discourse.mc-stan.org/u/Paparize)\
**Post date:** [August 27, 2024, 3:35pm UTC](https://discourse.mc-stan.org/t/partial-sum-error/36384/1 "2024-08-27T15:35:41Z")

</div>

Hi, I get some error about the partial sum in my stan program

* * *

The stan code is this

```stan
functions {
  real partial_sum(
    vector slice_y,
    int start, int end,
    matrix x, real sigma,
    vector[] beta, int[] g
  )
  {
    real lp = 0;
    
    for (i in start:end)
      lp += normal_lpdf(slice_y[i-start+1] | dot_product(x[i], beta[g[i]]), sigma);
    
    return lp;
  }
}

data {
  int<lower=1> N; // Number of observations
  int<lower=1> D; // Number of covariates
  vector[N] y; // Continuous outcomes (for OLS regression)
  matrix[N, D] x; // Covariate matrix
  int<lower=1> g[N]; // Group index for each observation
  int<lower=1> P; // Number of different years
}

parameters {
  vector[D] beta21; // Coefficients for the first year
  real<lower=0> sigma; // Standard deviation of the error term
  vector<lower=0>[D] omega; // Non-negative adjustments to coefficients
  vector[D] eta[P-1]; // Random effects for each year (except the first year)
}

transformed parameters {
  vector[D] beta2[P]; // Coefficients for each year
  
  beta2[1] = beta21; // Coefficients for the first year
  for (i in 2:P) {
    beta2[i] = beta2[i-1] + omega .* eta[i-1]; // Adjusted coefficients for subsequent years
  }
}

model {
  // Priors
  beta21 ~ normal(0, 10);
  sigma ~ normal(0, 1);
  omega ~ normal(0, 1);
  for (i in 1:(P-1)) {
    eta[i] ~ normal(0, 1);
  }
  
  // Likelihood
  target += reduce_sum(partial_sum, y, 1, x, sigma, beta2, g);
}

```

* * *

But when I check the program I found that the error can’t be fixed:

```no-highlight
Error in stanc(filename, allow_undefined = TRUE) : 0

Semantic error in 'string', line 49, column 12 to column 61:

Ill-typed arguments supplied to function 'reduce_sum'. Available arguments:
(T[], int, int, ...) => real, T[], int, ...
(T[,], int, int, ...) => real, T[,], int, ...
(T[,,], int, int, ...) => real, T[,,], int, ...
(T[,,,], int, int, ...) => real, T[,,,], int, ...
(T[,,,,], int, int, ...) => real, T[,,,,], int, ...
(T[,,,,,], int, int, ...) => real, T[,,,,,], int, ...
(T[,,,,,,], int, int, ...) => real, T[,,,,,,], int, ...
Where T is any one of int, real, vector, row_vector or matrix.
Instead supplied arguments of incompatible type: (vector, int, int, matrix, real, vector[], int[]) => real, vector, int, matrix, real, vector[], int[]

```

I know it may be the problem with my slice\_y character, but I revised the type to real, and the error still exist. Some suggestions? Very thanks

---

<div class="post-metadata">

**Author:** ![Corey.Plate](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/corey.plate/32/20600_2.png) [@Corey.Plate](https://discourse.mc-stan.org/u/Corey.Plate)\
**Post date:** [August 27, 2024, 4:54pm UTC](https://discourse.mc-stan.org/t/partial-sum-error/36384/2 "2024-08-27T16:54:51Z")

</div>

`y` here should be an array of definite size, and `slice_y` should be an array of arbitrary size. It is from this array that the `partial_sum` function returns the correct type of container to `reduce_sum()`

"The user-defined partial sum functions have the signature:  
`real f(array[] T x_slice, int start, int end, T1 s1, T2 s2, ...)`  
and take the arguments:

x\_slice - The subset of x (from reduce\_sum / reduce\_sum\_static) for which this partial sum is responsible (x\_slice = x[start:end])  
start - An integer specifying the first term in the partial sum  
end - An integer specifying the last term in the partial sum (inclusive)  
s1, s2, … - Arguments shared in every term (passed on without modification from the reduce\_sum / reduce\_sum\_static call)"

[https://mc-stan.org/docs/stan-users-guide/parallelization.html#reduce-sum](https://mc-stan.org/docs/stan-users-guide/parallelization.html#reduce-sum)

---

<div class="post-metadata">

**Author:** ![Paparize](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Paparize](https://discourse.mc-stan.org/u/Paparize)\
**Post date:** [August 28, 2024, 12:41am UTC](https://discourse.mc-stan.org/t/partial-sum-error/36384/3 "2024-08-28T00:41:46Z")

</div>

thanks for your reply, I have revised the array y in my reduce\_sum block, it is ok now
