Up

Easy Ways to Install PyStan

2020.05, 2023.09

 

The present (2023.05.15) version of PyStan is PyStan 3, which I have installed on Ubuntu. At present, to install PyStan 3 on Ubuntu, C++ compiler is needed.

I installed PyStan 3 according to the following steps.

Step 1. I installed g++ on Ubuntu, by running the code

sudo apt install g++

Step 2. I installed PyStan 3 on Python 3.11 (or Python 3.10) virtual environment of Anaconda, running the code

pip install pystan

 

Then, PyStan 3 can be used by the following import statement

import stan

 

When you use PyStan 3 in a Windows system, install Ubuntu on Windows. Ubuntu can be installed on Windows by the following simple steps:

1. Run the Windows PowerShell as Administrator. To run the powershell as Adminisrator, click on the icon by the right button of the mouse.

2. To install Ubuntu, run the code

wsl –install

3. Restart the Windows.

4. Run the Ubuntu, and so on. For details, check the site

http://y-okamoto-psy1949.la.coocan.jp/Python/en2/wsl_ubuntu/

 

==============

Caution: At this time (2023.01.04), PyStan/Anaconda does not run well in Windows. But, some combination of versions of pystan and Python does do the job. I installed pystan 2.17.1.0 in Python3.7 virtual environment, and had satisfaction. Remember that parameter n_jobs=1 must be set in sampling.

Pystan 2.17.1.0 can be installed by this code

conda install pystan==2.17.1.0

Notice that number of = sign is two.

 

PyStan, the Python interface to Stan, can be installed easily. But, in Windows, we must choose versions of PyStan which goes well with the version of Python. If you want to avoid choosing the version of PyStan, Anaconda would do everything needed, which includes preparation of C++ compiler for PyStan. Explanation for Windows and CentOS 8/CentOS Stream is presented first, then for Ubuntu.

 

In the Case of Windows or CentOS 8/CentOS Stream

PyStan needs a C++ compiler, but the standard C++ compilers of Windows or CentOS 8 cause troubles when they are called by Stan. However, Anaconda installs pystan with C++ compiler for it automatically. Anaconda can be installed easily. This website would give you explanation of how to install Anaconda and pystan.

Tips on installing Python in Windows are presented in this website.

For more information about PyStan, check this website.

 

 

In the Case of Ubuntu

In Ubuntu 18.04 LST, Python 2 and Python 3 are pre-installed. When python is called, Python2 is executed. To use Python 3, we must add 3 to python, like python3.

PyStan uses C++ compiler when being executed. C++ compiler can be installed by installing g++ as follows

sudo apt install g++

Before installing PyStan, module Cython should be installed by pip3. Note that 3 is added to pip to install for Python3.

When upgrading of pip command, use pip3 command to upgrade pip for Python3 (Figure 2).

Figure 2

 

After C++ compiler and module Cython are installed, pystan can be installed by pip3 command authorized by the administrator.

sudo pip3 install pystan

Then, you can use pystan by importing pystan as follows

import pystan

 

Up