Up

Installing Python, PyStan, and PyMC3 in Windows

 

It is easy to install Python in Windows. However, some modules for Python are difficult to install in Windows. One of them, PyStan (the Python interface to Stan) can be easily installed by Anaconda in Windows. In this website, in the first part, installation of Python is explained with important caution, and in the second part, installation of PyStan by Anaconda in Windows is explained. In the third part, how to use PyMC3 in Windows is explained.

 

 

Installing Python

 

If you install Python in the default folder, the pip command will not be called (2020.08). To avoid this inconvenience, Python should be installed in a folder created by you.

To create a folder, run File Explorer and open C drive(Figure A.1).

Figure A.1

 

Click on the New folder icon, and rename the new folder as you like. In Figure A.2, the new folder is named MyPython.

Figure A.2

 

Now, you can install Python in the MyPython folder.

Visit the website

 

https://www.python.org/

 

The following site (Figure A.3) appears.

Figure A.3

 

Put the mouse pointer on the gDownloadsh string, then the button for downloading (in Figure A.3, gPython 3.8.5h button) will be presented. Click this button.

The gSave Ash dialog form appears (Figure A.4).

Figure A.4

 

Click the gSaveh button. Downloading starts and the process is being displayed on the lower left corner (Figure A.5).

Figure A.5

 

After downloading finished, the string gOpen fileh is shown on the lower left corner (Figure A.5).

Click on the string gOpen fileh, then the downloaded file is executed and the Python installation form is presented (Figure A.6).

Figure A.6

 

Choose both options gInstall launcerch and gAdd Pythonch, then click on the string gCustomize installation.h

The gOptional Featuresh form will appear (Figure A.7). Select all the options. Of course, you may not need all the options. But, to select all of them is easy way.

Then, click on the gNexth button.

Figure A.7

 

On the next form, you choose the folder in which Python is to be installed (Figure A.8).

Figure A.8

 

Click on the gBrowseh button. The gBrowse For Folderh dialog box will be presented. Choose the folder in which you are installing Python. In Figure A.8, the folder gMyPythonh, which is created before downloading Python, is chosen.

Click the gOKh button. The gAdvanced Optionsh form is set as in Figure A.9.

Figure A.9

 

As the customized install location, the folder which is chosen in the dialogue box (Figure A.8) is set.

Click on the gInstallh button, then installation of Python starts.

When the installation is completed, the form shown in Figure A.10 will be shown.

Figure A.10

 

Click the gCloseh button.

After the installation of Python, the Start Menu contains Python-related items (Figure A.11).

Figure A.11

 

Click on the IDLE (Python 3.8 32-bit) item, IDLE (Integrated Development and Learning Environment) starts and the window shown in Figure A.12 appears.

Figure A.12

 

Press down a key on the keyboard, the corresponding character is shown at the location where the cursor is.

The position to input starts after the string g>>>h.

Type in script g1 + 2h, then press the Enter key.

The result is shown as in Figure A.13.

Figure A.13

 

The mode in which scripts are executed one by one as in Figure A.13 is called the interactive mode.

Python programs can be written in a file, which can be executed by the python command. This mode is called the script mode.

Python script files can be developed with the editor of Python IDLE.

Start the IDLE (Figure A.11) and choose the menu item gFile|New Fileh of IDLE shell (Figure A.14).

Figure A.14

 

A window of IDLE editor will be shown (Figure A.15).

Figure A.15

 

First, name the new file by menu item gFile|Save Ash of the new window of IDLE editor (Figure A.15).

Choose menu item gFile|Save Ash, the gSave Ash dialog form is shown (Figure A.16).

Figure A.16

 

Choose the folder in which the file is saved, and set the file name with the filename extension g.pyh

Click on the gSaveh button.

Now, write your script code (Figure A.17).

Figure A.17

 

Save your script in the file by choosing the menu item gFile|Saveh (Figure A.18), before executing the script. The new script must be saved in the file to be executed by Python.

Figure A.18

 

After having saved the file, choose the menu item gRun|Run Moduleh to execute the script (Figure A.19).

Figure A.19

 

When the script is executed, output by print statement is displayed in the Python IDLE window (Figure A.20).

Figure A.20

 

When you want to execute the script file, which has been already developed, open the file by choosing the menu item gFile|Openh in Figure A.14.

To execute the opened script file, choose the menu item gRun|Run Moduleh of the window, in which the file is opened (see Figure A.19).

 

 

Installation of Modules (Libraries)

 

Modules can be installed by the pip command, which is executed in a terminal window.

Since installation may require administrator privileges, run the terminal soft as Administrator.

Click on the menu item gCommand Prompth on the Start Menu by the right button. Then choose gMoreh, then gRun as administratorh (Figure A.21).

Figure A.21

 

When the terminal soft is called by Administrator, the string gAdministratorh is displayed in the title of the window (Figure A.22).

Figure A.22

 

Modules can be installed by the command pip as follows

 

pip install list-of-module(s) 

 

In Figure A.23, the pip command installs modules numpy, scipy, pandas, matplotlib, seaborn and xlrd.

Figure A.23

 

Execute the command in Figure A.23, then installation starts and ends as shown in Figure A.24.

Figure A.24

 

In the case of Figure A.24, the message gWARNING:cYou should consider upgradingch is displayed in the end.

Type in the command to upgrade gpiph as suggested, then execute it.

When the upgraded gpiph is successfully installed, the message is shown as in Figure A.25.

Figure A.25

 

 

You would find some sample scripts in Python in the following websites:

 

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

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

 

 

Installing Anaconda and PyStan

 

PyStan, the Python interface to Stan, requires C++ compiler for Stan scripts. The C++ compiler must be selected so that it compiles Stan script without errors. If we use Anaconda to install PyStan, the selection of the C++ compiler is automatically done by Anaconda and installed with PyStan. Installation of Anaconda is easy as shown below:

 

Anaconda can be down loaded from the website:

 

https://www.anaconda.com/products/individual

 

Visit the above website, and click on the gDownloadh button (Figure B.1).

Figure B.1

 

The form gAnaconda Installersh appears (Figure B.2).

Figure B.2

 

When you install Anaconda in Windows, click on the string g64-Bit Graphical Installers (466 MB)h under the string gWindowsh.

In the case of the browser Edge, the gSave Ash dialog window is presented (Figure B.3).

Figure B.3

 

Click on the gSaveh button, then downloading starts.

When downloading finishes, the string gOpen fileh is presented at the lower left corner of the window (Figure B.4).

Figure B.4

 

Click on the string gOpen fileh, then execution of the downloaded file starts and the form of Anaconda setup is presented (Figure B.5).

Figure B.5

 

Click on the gNexth button.

The gLicense Agreementh dialog form appears (Figure B.6).

Figure B.6

 

Click on the gI Agreeh button.

The gSelect Installation Typeh dialog form appears (Figure B.7).

Figure B.7

 

Choose the gJust Meh option, then click on the gNexth button.

The gChoose Installation Locationh dialog form is presented (Figure B.8).

Figure B.8

 

Click on the gNexth button (Figure B.8).

The gAdvanced Installation Optionsh dialog form is presented (Figure B.9).

Figure B.9

 

I recommend that both options are not chosen, so that effects on the system by the installation should be minimum.

Click on the gInstallh button.

When the installation finishes, the gInstallation Completeh form is presented (Figure B.10).

Figure B.10

 

Click on the gNexth button.

The form like in Figure B.11 is presented.

Figure B.11

 

Click on the gNexth button, then the gCompleting Anaconda Setuph form is presented (Figure B.12).

Figure B.12

 

If you do not read gtipsh and gresourcesh for now, remove checkmarks for them.

Click on the gFinishh button (Figure B.12).

 

After the installation of Anaconda, menu items of Anaconda are shown in Start Menu (Figure B.13).

Figure B.13

 

When you choose the menu item gAnaconda Powershell Prompth, the terminal soft is called (Figure B.14).

Figure B.14

 

The stringh(base)h indicates that this terminal is running in the virtual environment gbaseh. The virtual environment gbaseh has been created automatically when Anaconda is installed.

In this terminal, commands for the virtual environment can be called. Commands not for the virtual envirenmnet, e.g., cd, ls, and so on, can be also called.

Modules for Python can be installed by the conda command, although the pip command can be also used. In a virtual environment, usage of the conda command is recommended.

To install modules, the conda command is called in the following format:

 

conda install module-list

 

Names in the module-list should be separated by blank characters.

To install modules numpy, scipy, pandas, matplotlib, seaborn and xlrd, type in the code as in Figure B.15.

Figure B.15

 

When you install PyStan, type in the code as in Figure B.16.

Figure B.16

 

The C++ compiler, which is used for Stan script, is automatically installed when PyStan is installed.

 

If you have installed PyStan, letfs try it.

First, create a Stan script file and a Python script file, which uses the Stan script file.

Script files can be created by a text editor. Here, letfs use the Python IDLE editor, which can be installed according to the steps explained in the first part of this website.

Run Python IDLE (see Figure A.11), and create a new file (see Figure A.14).

Name this new file as gbinomial.stang, then write the Stan script as in Figure B.17. The folder, in which the created script files are to be saved, is arbitrary, but the following steps should be taken considering what folder you are using.

Create another new file, and name it gAnalysis.pyh. Write the Python script as in Figure B.17.

Move the current directory to the same directory (folder) as the one in which the script are saved. In Figure B.17, the current directory is moved to C:\WorkSpace\Samples by the cd command.

Then execute the Python script by the python command as follows:

 

python Analysis.py

 

Figure B.17

 

When sampling by MCMC finishes, the statistics of the posterior distributions are displayed by the print statement (Figure B.18).

Figure B.18

 

You would find some sample scripts, which use Stan, in the following websites:

 

http://y-okamoto-psy1949.la.coocan.jp/Python/sampleprgs/OneVarStan/en/

http://y-okamoto-psy1949.la.coocan.jp/Python/en/PickleStan/

 

 

Caution!

In Windows, successful combination of Python and PyStan is limited (2023.01.04).

I use PyStan 2.17.1.0 installed in Python 3.7 virtual environment of Anaconda in Windows 10.

PyStan can be installed by the code

 

conda install pystan==2.17.1.0

 

When sampling by MCMC of PyStan, the value of parameter n_jobs must be 1.

 

 

Using PyMC3 inWindows

 

In Windows, PyMC3 can be used on Jupyter Notebook, which can be installed in Anaconda by the command

 

conda install jupyter

 

Then, to use Jupyter Notebook, run the command

 

jupyter notebook

 

To use PyMC3, install modules pymc3 and ArviZ by executing the command

 

conda install pymc3 arviz==0.11.1

 

If arviz 0.11.2 is installed, arviz 0.11.2 will cause error when importing pymc3 and arviz (2021.4.30).

In this case, replace arviz 0.11.2 by arviz 0.11.1, executing the command

 

conda install arviz==0.11.1

 

Now, run Jupyter Notebook, and enjoy PyMC programming, e.g. the following script in Figure C.1.

Figure C.1

 

At present (2021.04.30), the module pymc3 cannot be installed by conda command in an environment with Python 3.9.4. The module pymc3 can be installed in an environment with Python 3.8.8. To create an environment with Python 3.8.8 or later, execute the following command

 

conda create n py38 python=3.8

 

The name py38 is any one which you like. Notice that number of = sign is one. When you set the version of Python by python=3.8, the latest version (3.8.8 at 2021.40.30) will be installed.

After the environment is created, to activate it, you execute the following command

 

conda activate py38

 

The name py38 is the one, which is designated when creating.

 

 

Up