Bayesium Analytics

Practical Bayesian Data Analysis

  • Archive
  • Backlog
  • Services

Archives for August 2022

August 7, 2022 by kevin@ksvanhorn.com

Installing httpstan on Ubuntu Linux 20.04

The full instructions and source code are found here. In this post I’ll give the slimmed-down basics.

  1. If python3, pip, or curl are not already installed, install them:
    sudo apt install python3 python3-pip curl
  2. Now install via pip:
    python3 -m pip install httpstan
  3. Now do a quick check to verify the install:
    python3 -m httpstan &
    curl -H "Content-Type: application/json" --data \

    '{"program_code":"parameters {real y;} model {y ~ normal(0,1);}"}' \
    http://localhost:8080/v1/models > output.txt
  4. The file output.txt should contain the text representation of a JSON object with a “name” field, a “compiler_output” field that will be a long string, and a “stanc_warnings” field that should have the value “”.
  5. Now shut down the server:
    fg
    ^C

Filed Under: Uncategorized

August 7, 2022 by kevin@ksvanhorn.com

Installing CmdStan on Windows 10

Full installation instructions for CmdStan are in the CmdStan User’s Guide; here we focus specifically on the most straightforward way of installing on Windows 10.

Install conda

Using the conda package management system is the recommended way of installing CmdStan on Windows. It allows one to install a pre-built version of CmdStan along with all required dependencies. Assuming that you do not already have conda installed, follow these steps:

  1. Point your browser at the Miniconda Home Page.



  2. Scroll down to “Latest Miniconda Installer Links”.

  3. Click on “Miniconda3 Windows 64-bit” (Name) for Windows (Platform) to download the installer.



  4. Run the downloaded installer, Miniconda3-latest-Windows-x86_64.exe.



  5. When the installer asks, it is recommended to choose “Install for: Just Me” and, if at all possible, “Register Miniconda3 as my default Python 3.9”.

  6. You will run the conda commands from within an Anaconda Prompt (Miniconda3) window, which you will now find in your Windows Start menu.

Here are some additional conda resources that you most likely will not need:

  • User Guide
  • Cheat Sheet

Install CmdStan

Make sure you have an Anaconda Prompt window open (as described at the end of the previous section) and issue all of the following commands within that window.

  1. Download CmdStan and all required dependencies in a new conda environment called “stan”. Answer “y” to the “Proceed” prompt. This will take a while.
    conda create -n stan -c conda-forge cmdstan
  2. Activate the stan environment.
    conda activate stan
  3. See where your new stan environment is located; there should be a star after its name in the listing.
    conda info -e
  4. Take note of the path to the CmdStan installation directory; it will be nested within the stan environment directory.
    echo %CMDSTAN%

Verify CmdStan installation

The following is just a quick check to verify that CmdStan is properly installed:

  1. Change to the CmdStan installation directory.
    cd /d "%CMDSTAN%"
  2. Make an example Stan model. Note the forward slashes; this is important.
    mingw32-make examples/bernoulli/bernoulli.exe
  3. Now run the model on the data in the accompanying .data.json file. This should be very fast.
    examples\bernoulli\bernoulli.exe sample data ^ file=examples/bernoulli/bernoulli.data.json

    The output will look something like this:


  4. See a summary of the parameter estimates. The mean of theta, +/- one standard deviation, should include 0.2.
    bin\stansummary.exe output.csv

    The output will look something like this:

Using CmdStan outside of Anaconda Prompt

The Anaconda Prompt is convenient because, once you activate the stan environment (conda activate stan), the CMDSTAN environment variable is set to the “work” directory for CmdStan (from which you can make models and run them) and your PATH is modified to include the directories containing the Stan compiler and all supporting tools.

But maybe you want to run CmdStan from PowerShell, or the normal Windows command prompt, or from a script. Then here’s what you need to do.

  1. If you haven’t already done so, open Anaconda Prompt and issue the command
    conda activate stan
  2. Get the values of the environment variables CMDSTAN and CONDA_PREFIX and save them away somewhere.
    echo %CMDSTAN%
    echo %CONDA_PREFIX%

  3. From within PowerShell, the Windows Command Prompt, or a script, you can then set up your environment as follows:
    set CMDSTAN=cmdstan_value
    set TMP=conda_prefix_value
    set PATH=%TMP%;%TMP%\Library\mingw-w64\bin;%TMP%\Library\usr\bin;%TMP%\Library\bin;%TMP%\Scripts;%TMP%\bin;%PATH%;.

Optional: Uninstalling Miniconda3

This is a bit of a delicate proposition because the %CONDA_PREFIX% directory containing CmdStan and all its supporting tools is located within your Miniconda3 installation. So this is what you need to do first:

  1. Choose a new location for your CmdStan installation, somewhere outside of the Miniconda3 installation; let’s call this stanenv.
  2. Open the Windows Command Prompt and issue the following command:
    move conda_prefix_value stanenv

Now you can do the actual uninstall:

  1. Let condainst be the path to the root of the Miniconda3 installation.
  2. Issue the following command:
    condainst\Uninstall-Miniconda3
  3. An uninstaller will pop up; click through the steps.

Note that when setting up your environment to use CmdStan as described in step 3 of Using CmdStan outside of an Anaconda Prompt, you will now need to replace conda_prefix_value with stanenv and modify cmdstan_value accordingly.

Filed Under: Uncategorized

Recent Posts

  • Installing httpstan on Ubuntu Linux 20.04
  • Installing CmdStan on Windows 10
  • Probability Theory Does Not Extend Logic?
  • It’s All About Jensen’s Inequality
  • Analysis of a Nootropics Survey

Archives

  • August 2022
  • July 2017
  • March 2016
  • August 2015
  • July 2015
  • June 2015

Categories

Tags

multilevel modeling nominal variables

Copyright © 2025 · Generate Pro Theme on Genesis Framework · WordPress · Log in