diff --git a/Untitled.ipynb b/Untitled.ipynb deleted file mode 100644 index 7fec5150..00000000 --- a/Untitled.ipynb +++ /dev/null @@ -1,6 +0,0 @@ -{ - "cells": [], - "metadata": {}, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/arviz_scratch.ipynb b/arviz_scratch.ipynb deleted file mode 100644 index a4d92ef7..00000000 --- a/arviz_scratch.ipynb +++ /dev/null @@ -1,172 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Load the centered eight schools model\n", - "import arviz as az\n", - "\n", - "data = az.load_arviz_data(\"centered_eight\")\n", - "data\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import matplotlib\n", - "import pandas as pd\n", - "import os\n", - "\n", - "from cmdstanpy import cmdstan_path, CmdStanModel\n", - "\n", - "# ### Instantiate & compile the model\n", - "\n", - "bernoulli_dir = os.path.join(cmdstan_path(), 'examples', 'bernoulli')\n", - "bernoulli_stan = os.path.join(bernoulli_dir, 'bernoulli.stan')\n", - "\n", - "bernoulli_model = CmdStanModel(stan_file=bernoulli_stan)\n", - "\n", - "bern_data = { \"N\" : 10, \"y\" : [0,1,0,0,0,0,0,0,0,1] }\n", - "\n", - "bern_fit = bernoulli_model.sample(data=bern_data)\n", - "vars = bern_fit.stan_variables()\n", - "for (k,v) in vars.items():\n", - " print(k, v.shape)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "for (k,v) in bern_fit.stan_vars_dims.items():\n", - " if len(v) == 0:\n", - " print('var: {}, scalar'.format(k))\n", - " else:\n", - " print('var: {}, array dims: {}'.format(k,v))\n" - ] - }, - { - "cell_type": "raw", - "metadata": {}, - "source": [ - "cmdstanpy_data = az.from_cmdstanpy(posterior=bern_fit)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "cmdstanpy_data" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ppc_8schools = CmdStanModel(stan_file='ppc_8schools.stan')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import numpy as np\n", - "stan_data = {\n", - " \"J\": 8,\n", - " \"y\": np.array([28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0]),\n", - " \"sigma\": np.array([15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0]),\n", - " }" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ppc_fit = ppc_8schools.sample(data=stan_data)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ppc_fit.stan_vars_cols" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ppc_fit.stan_vars_dims" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ppc_fit.sampler_vars_cols" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "names = ['mu', 'theta']\n", - "cols = []\n", - "for name in names:\n", - " cols.extend(ppc_fit.stan_vars_cols[name])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.5" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/bernoulli_ppc.stan b/bernoulli_ppc.stan deleted file mode 100644 index 5efc1721..00000000 --- a/bernoulli_ppc.stan +++ /dev/null @@ -1,16 +0,0 @@ -data { - int N; - int y[N]; -} -parameters { - real theta; -} -model { - theta ~ beta(1,1); - y ~ bernoulli(theta); -} -generated quantities { - int y_rep[N]; - for (n in 1:N) - y_rep[n] = bernoulli_rng(theta); -} diff --git a/cleanup.sh b/cleanup.sh deleted file mode 100755 index 663a22a0..00000000 --- a/cleanup.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/sh - -if [[ -e cmdstanpy/__pycache__ ]]; then - echo "remove cmdstanpy/__pycache__ dir" - rm -rf cmdstanpy/__pycache__ -fi - -if [[ -e test/__pycache__ ]]; then - echo "remove test/__pycache__ dir" - rm -rf test/__pycache__ -fi - -if [[ -e docs/_build ]]; then - echo "remove docs/_build dir" - rm -rf docs/_build -fi - -echo "remove compiled stan programs" -find test/data -perm 755 -and -type f -find test/data -perm 755 -and -type f -exec rm {} \; -find docs/notebooks -perm 755 -and -type f -find docs/notebooks -perm 755 -and -type f -exec rm {} \; - -echo "remove stan program .hpp files" -find test/data -name "*.hpp" -find test/data -name "*.hpp" -exec rm {} \; -find docs/notebooks -name "*.hpp" -find docs/notebooks -name "*.hpp" -exec rm {} \; - -echo "remove stan program .d files" -find test/data -name "*.d" -find test/data -name "*.d" -exec rm {} \; -find docs/notebooks -name "*.d" -find docs/notebooks -name "*.d" -exec rm {} \; - -echo "remove stan program .o files" -find test/data -name "*.o" -find test/data -name "*.o" -exec rm {} \; -find docs/notebooks -name "*.o" -find docs/notebooks -name "*.o" -exec rm {} \; diff --git a/ppc_8schools b/ppc_8schools deleted file mode 100755 index 216d4e92..00000000 Binary files a/ppc_8schools and /dev/null differ diff --git a/ppc_8schools.stan b/ppc_8schools.stan deleted file mode 100644 index e862cc5d..00000000 --- a/ppc_8schools.stan +++ /dev/null @@ -1,33 +0,0 @@ -data { - int J; - real y[J]; - real sigma[J]; -} - -parameters { - real mu; - real tau; - real eta[J]; -} - -transformed parameters { - real theta[J]; - for (j in 1:J) - theta[j] = mu + tau * eta[j]; -} - -model { - mu ~ normal(0, 5); - tau ~ cauchy(0, 5); - eta ~ normal(0, 1); - y ~ normal(theta, sigma); -} - -generated quantities { - vector[J] log_lik; - vector[J] y_hat; - for (j in 1:J) { - log_lik[j] = normal_lpdf(y[j] | theta[j], sigma[j]); - y_hat[j] = normal_rng(theta[j], sigma[j]); - } -} diff --git a/scratch.ipynb b/scratch.ipynb deleted file mode 100644 index 6e15df39..00000000 --- a/scratch.ipynb +++ /dev/null @@ -1,299 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "\n", - "from cmdstanpy.utils import _TMPDIR, cmdstan_path, set_cmdstan_path\n", - "from cmdstanpy.model import CmdStanModel\n", - "_TMPDIR" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "cmdstan_path()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# set_cmdstan_path(os.path.expanduser(os.path.join('~', 'github', 'stan-dev', 'cmdstan')))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bernoulli_dir = os.path.join(cmdstan_path(), 'examples', 'bernoulli')\n", - "bernoulli_path = os.path.join(bernoulli_dir, 'bernoulli.stan')\n", - "\n", - "bernoulli_model = CmdStanModel(stan_file=bernoulli_path)\n", - "print(bernoulli_model)\n", - "\n", - "bern_json = os.path.join(bernoulli_dir, 'bernoulli.data.json')\n", - "\n", - "bern_data = { \"N\" : 10, \"y\" : [0,1,0,0,0,0,0,0,0,1] }" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The `CmdStanModel` method `sample` runs the NUTS-HMC sampler and returns a `CmdStanMCMC` object. By default, `sample` runs 4 sampler chains." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_fit = bernoulli_model.sample(data=bern_data, save_warmup=True, iter_warmup=200)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_fit.draws().shape" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_fit.draws(inc_warmup=True, concat_chains=True).shape" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bernoulli_ppc_model = CmdStanModel(stan_file='bernoulli_ppc.stan')\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bernoulli_ppc_model.generate_quantities(data=bern_data, fitted_params='bernoulli-fitted-params.csv')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(bern_fit.runset)\n", - "' '.join(bern_fit.runset.cmds[0])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(bern_fit)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Access the sample: the `CmdStanMCMC` object attributes and methods" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Summarize the results\n", - "\n", - "The `CmdStanMCMC` method `summary` returns output of CmdStan bin/stansummary as pandas.DataFrame. The `summary` report provides estimates of both the parameter value and the goodness of fit." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_fit.summary()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Run sampler diagnostics\n", - "\n", - "The `CmdStanMCMC` method `diagnose` prints the output of CmdStan bin/stansummary to the console. This is useful when the summary report shows `R_hat` values outside of the range of .99 to 1.01, or the number of effective samples (`N_eff` value) is below 5% of the total draws in the sample. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_fit.diagnose()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_draws_pd = bern_fit.draws_pd()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_draws_pd" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_draws_pd.shape, bern_draws_pd.columns" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "thetas = bern_fit.stan_variable(name='theta')\n", - "thetas.shape\n", - "thetas.plot.density()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_fit.draws().shape" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_fit.column_names[7], bern_fit.draws()[0:3,0,7]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Get HMC sampler tuning parameters\n", - "\n", - "##### stepsize\n", - "\n", - "The `CmdStanMCMC` property `stepsize` property is a 1-D numpy ndarray which contains the stepsize used by the sampler for each chain. This array is created at the same time as the `sample` and `metric` arrays are created.\n", - "\n", - "At the end of adaptation, the stepsize for the 4 chains in this example is:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_fit.stepsize" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### metric_type, metric\n", - "\n", - "The `metric` property is an numpy ndarray which contains the metric used by the sampler for each chain. This array is created at the same time as the `sample` and `stepsize` arrays are created.\n", - "\n", - "At the end of adaptation, the metric for the 4 chains in this example is:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "bern_fit.metric_type, bern_fit.metric" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.5" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/scratch.py b/scratch.py deleted file mode 100644 index 4281aac4..00000000 --- a/scratch.py +++ /dev/null @@ -1,62 +0,0 @@ -import os -from cmdstanpy.utils import cmdstan_version_at, cmdstan_path -cmdstan_path() -cmdstan_version_at(2,22) -from cmdstanpy.model import CmdStanModel -from cmdstanpy import _TMPDIR -_TMPDIR - - -# + - -bernoulli_stan = os.path.join(cmdstan_path(), 'examples', 'bernoulli', 'bernoulli.stan') -bernoulli_model = CmdStanModel(stan_file=bernoulli_stan) -print(bernoulli_model) -print(bernoulli_model.code()) -# - - -bernoulli_json = os.path.join(cmdstan_path(), 'examples', 'bernoulli', 'bernoulli.data.json') -with open(bernoulli_json, 'r') as f: - print(f.read()) - -bern_fit = bernoulli_model.sample(data=bernoulli_json, output_dir='.') - -print(bern_fit) - -sampler_variables = bern_fit.sampler_vars_cols -stan_variables = bern_fit.stan_vars_cols -print('Sampler variables:\n{}'.format(sampler_variables)) -print('Stan variables:\n{}'.format(stan_variables)) - - -bern_fit.draws().shape - -draws_theta = bern_fit.stan_variable(name='theta') -draws_theta.shape - -bern_fit.summary() - -bern_fit.sampler_diagnostics() - - - - - -test_path = os.path.join('test','data') -logistic_stan = os.path.join(test_path,'logistic.stan') -logistic_data = os.path.join(test_path,'logistic.data.R') - - -logistic = CmdStanModel(stan_file=logistic_stan) -logistic_17 = logistic.sample(data=logistic_data, seed=12345, sig_figs=17) -smry = logistic_17.summary() -print(smry) -smry = logistic_17.summary(sig_figs=2) -print(smry) -smry = logistic_17.summary(sig_figs=17) -print(smry) - -import numpy as np -np.set_printoptions(formatter={"float_kind": lambda x: "%g" % x}) - -print(smry) diff --git a/sig_figs_test.py b/sig_figs_test.py deleted file mode 100644 index c80c432d..00000000 --- a/sig_figs_test.py +++ /dev/null @@ -1,24 +0,0 @@ -import os -from cmdstanpy import _TMPDIR -from cmdstanpy.cmdstan_args import Method, SamplerArgs, CmdStanArgs -from cmdstanpy.utils import EXTENSION -from cmdstanpy.stanfit import RunSet, CmdStanMCMC -from cmdstanpy.model import CmdStanModel - -# HERE = os.path.dirname(os.path.abspath(__file__)) -# DATAFILES_PATH = os.path.join(HERE, 'test', 'data') - -DATAFILES_PATH = os.path.join('test', 'data') - -stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan') -bern_model = CmdStanModel(stan_file=stan) - -jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json') -bern_fit = bern_model.sample( - data=jdata, - chains=2, - parallel_chains=2, - seed=12345, - iter_sampling=100, - sig_figs=1, -) diff --git a/slurm/run_chain.py b/slurm/run_chain.py deleted file mode 100644 index 29c0f066..00000000 --- a/slurm/run_chain.py +++ /dev/null @@ -1,40 +0,0 @@ -# User CmdStanPy to run one chain -# Required args: -# - cmdstanpath -# - model_exe -# - seed -# - chain_id -# - output_dir -# - data_file - -import os -import sys - -from cmdstanpy.model import CmdStanModel, set_cmdstan_path, cmdstan_path - -useage = """\ -run_chain.py ()\ -""" - -def main(): - if (len(sys.argv) < 5): - print(missing arguments) - print(useage) - sys.exit(1) - a_cmdstan_path = sys.argv[1] - a_model_exe = sys.argv[2] - a_seed = int(sys.argv[3]) - a_chain_id = int(sys.argv[4]) - a_output_dir = sys.argv[5] - a_data_file = None - if (len(sys.argv) > 6): - a_data_file = sys.argv[6] - - set_cmdstan_path(a_cmdstan_path) - print(cmdstan_path()) - - mod = CmdStanModel(exe_file=a_model_exe) - mod.sample(chains=1, chain_ids=a_chain_id, seed=a_seed, output_dir=a_output_dir, data_file=a_data_file) - -if __name__ == "__main__": - main() diff --git a/slurm/run_chains.sh b/slurm/run_chains.sh deleted file mode 100644 index 6b822459..00000000 --- a/slurm/run_chains.sh +++ /dev/null @@ -1,16 +0,0 @@ -# distribute N chains, M chains per node on the cluster - -# set up all slurm directives -# common args: -# - cmdstan_path, model_exe, seed, output_dir, data_path -# unique arg: chain_id - jobs array number: %a - -#!/bin/bash -#SBATCH --job-name=cmdstanpy_runs -#SBATCH --output=cmdstanpy_stdout-%j-%a.out -#SBATCH --error=cmdstanpu_stderr-%j-%a.err -#SBATCH --nodes=20 -#SBATCH --cpus-per-task=1 -#SBATCH -a 0-100 -python run_chain cmdstan_path model_exe seed chain_id output_dir data_path - diff --git a/test/data/bernoulli b/test/data/bernoulli deleted file mode 100755 index e4bc25a7..00000000 Binary files a/test/data/bernoulli and /dev/null differ diff --git a/test/data/bernoulli with space in name b/test/data/bernoulli with space in name deleted file mode 100755 index 86ac11f9..00000000 Binary files a/test/data/bernoulli with space in name and /dev/null differ diff --git a/test/data/datagen_poisson_glm b/test/data/datagen_poisson_glm deleted file mode 100755 index 09221ea1..00000000 Binary files a/test/data/datagen_poisson_glm and /dev/null differ diff --git a/test/data/logistic b/test/data/logistic deleted file mode 100755 index 193cc26e..00000000 Binary files a/test/data/logistic and /dev/null differ diff --git a/test/data/path with space/bernoulli_path_with_space b/test/data/path with space/bernoulli_path_with_space deleted file mode 100755 index c663ed39..00000000 Binary files a/test/data/path with space/bernoulli_path_with_space and /dev/null differ diff --git a/windows-install-notes.txt b/windows-install-notes.txt deleted file mode 100644 index 1c7b1861..00000000 --- a/windows-install-notes.txt +++ /dev/null @@ -1,18 +0,0 @@ -cmdstanpy.install_cxx_toolchain works on Win 10 - does it do enough? - - -building CmdStan from command line requires adding RTools toolchain directories: `mingw64/bin` and `usr/bin` to PATH -install-tbb problems? - - -Ari: - -pip install cmdstanpy -python -m cmdstanpy.install_cxx_toolchain -python -m cmdstanpy.install_cmdstan --compiler - - -then: - -from cmdstanpy.utils import cxx_toolchain_path -cxx_toolchain_path()