Hi, I am trying to use stan in python to fit a 1d gaussian toy problem with a specified prior on the mean. I have been successful using R, but I need to also use python. I have tried using pystan (to no success - I would get JSON errors (‘The JSON document has an improper structure’) and eventually gave up and switched to cmdstanpy. I am now getting issues trying to build (any) model that looks like:
.
.
.
ld: unsupported tapi file type ‘!tapi-tbd’ in YAML file ‘/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd’ for architecture x86_64
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/var/folders/k_/lm3f3ty933bgx8bfm0dsx7cr0000gn/T/tmp3bkjvjzy/tmp4mr8ymjr] Error 1
Command [‘make’, ‘STANCFLAGS+=–filename-in-msg=1dgaussian.stan’, ‘/var/folders/k_/lm3f3ty933bgx8bfm0dsx7cr0000gn/T/tmp3bkjvjzy/tmp4mr8ymjr’]
error during processing No such file or directory
My model code:
// The input data is a vector 'y' of length 'N'.
data {
int<lower=0> N;
vector[N] y;
real<lower=0> sigma;
real mu_0;
real<lower=0> sigma_0;
}
// The parameters accepted by the model. Our model
// accepts two parameters 'mu' and 'sigma'.
parameters {
real mu;
}
// The model to be estimated. We model the output
// 'y' to be normally distributed with mean 'mu'
// and standard deviation 'sigma'.
model {
mu ~ normal(mu_0, sigma_0);
y ~ normal(mu, sigma);
}
and my python code:
import os
from cmdstanpy import CmdStanModel
import numpy as np
np.random.seed(0)
# generate the normal data
n = 1000
mu = 5
sigma = 1
y = np.random.normal(loc = mu, scale = sigma, size = n)
# prior values for mu
mu_0 = 0
sigma_0 = 1
# compute the true posteriors
a = (1/sigma_0**2) + n/sigma**2
b = mu_0 / sigma_0**2 + y.sum()/sigma**2
mu_p = b/a
sigma_p = 1/np.sqrt(a)
stan_data = {
'N': n,
'y': y,
'sigma': sigma,
'mu_0': mu_0,
'sigma_0': sigma_0
}
# fit the stan model
model = CmdStanModel(stan_file='1dgaussian.stan')
#fit = model.sample(data=stan_data)
#print(fit)
This has the simulated data in the python code. I have been fiddling around and making new venvs and conda envs for several days now and perusing all of the relevant stan discussions that already exist.
Unsure on your specific error here, but could you provide some details on how you installed CmdStan? Did you use the instructions here related to macOS: Installation — CmdStanPy 1.2.4 documentation ?
Your actual Python/Stan code look fine (I am able to run them successfully without any modifications on my machine), so the first place I would look is verifying the CmdStan build setup is done properly (this may include making sure that the Xcode command line tools are installed properly).
It’s more step-by-step instructions with checks as you go. Not turning on the command line tools after a background Xcode update is a common path to failure (no idea why Apple does this, because I can’t imagine who would want different settings on a reinstall).
To address all three questions, I did install it through each of these links separately (following all the different methods and then deleting the installation when it didn’t work). The one from the github I could not get to make - it was giving missing file issues. The pip installation says it is installed but does not work in python to run the provided code. The conda installation I re-did and it is still showing the same error. I have not been able to try install_cmdstan, though. After following the manual build of cmdstanpy from scratch, it is unable to do the “make build” step though.
Do you know if you’re able to use that CmdStan install to compile Stan models? This should only require having a not-too-ancient version of C++ installed properly. If you go into a terminal and execute
clang++ --version
what do you see? If the files not being found are part of the C++ toolchain, that’s the culprit. If they’re part of Stan, then that’s a weird issue about how you’re downloading—everything should be in the repo when downloaded.
I’ve seen few cases of compilation problems with macOS Sonoma that were resolved by uninstalling and reinstalling Xcode (or just the Xcode command line tools), although I’m not sure if that will help here since this error message looks a bit different.
So I updated to the next ios over the weekend and that apparently included an Xcode tools update. No change to the error though. I will look at the github issue now and see what they did.
After using the github instructions provided, (clean conda environment, conda install pip, pip install cmdstanpy, then run install_cmdstan() in python) the install_cmdstan() and rebuild_cmdstan() both failed with this error:
ake: *** [stan/lib/stan_math/lib/tbb/tbb.def] Error 2
11:50:15 - cmdstanpy - WARNING - CmdStan installation failed.
Command "make build" failed
Command ['make', 'build', '-j1']
error during processing No such file or directory