PyStan and PyMC3 on Ubuntu on Windows (WSL)
A memorandum. 2022.08, 2023.06
PyStan and PyMC3 may cause some problems on Windows, but these problems can be avoided using Windows Subsystem for Linux (WSL). Here, I describe how I installed WSL, Ubuntu and Anaconda. For details about WSL, check the website
https://docs.microsoft.com/en-us/windows/wsl/install
To install WSL. run Windows PowerShell as Administrator by clicking PowerShell icon by the right button of your mouse and choosing the appropriate item.
Run the following code in the terminal window of PowerShell.
wsl -–install
After the installation of the WSL, installation of Ubuntu starts automatically.
After installation of Ubuntu, you are asked to set the username and the password.
Set the username and the password, then restart Windows.
After Windows restarts, Ubuntu icon is displayed in the start menu. Click this icon.
A window of a terminal of Ubuntu will appears.
To update and upgrade the Ubuntu, run the following commands in the Ubuntu terminal window
sudo apt
update && sudo apt upgrade
After updating and upgrading, close the Ubuntu terminal window by running command logout or exit.
To install Anaconda in Ubuntu, the install file must be downloaded.
The install file of Anaconda can be downloaded from the website
https://www.anaconda.com/download
by a browser of Windows, e.g.,Edge, into a folder of Windows, e.g., R:\Yasuharu\temp\.
To change the current folder to the folder in which the file was downloaded, in this case, R:\Yasuharu\temp\, run the following code.
cd
/mnt/r/Yasuharu/temp/
The folder R: is denoted by the lower case letter r. Drives of Windows are under the folder /mnt/ in WSL.
Install Anaconda by running the following command.
bash
Anaconda3-2022.05-Linux-x86_64.sh
At the end of installation, you are asked whether to initialize Anaconda or not. I recommend to choose yes.
After installing, close the Ubuntu terminal.
Restart the Ubuntu terminal.
Now, the Ubuntu terminal is running in Anaconda virtual environment, which is shown by command line starting with string (base), e.g.
(base)
yasuharu@PSY-PM-PC:~$
To update Anaconda, run the following command.
conda update –n base conda
Then, to update the packeges, run the following command.
conda
update --all
After these updates, install modules, which you use, by commands conda install or pip install, for example
conda install
pystan
When the above command is used in Python 3.8 virtual environment, PyStan 2.19.1.1 will be installed.
The following command
pip install
pystan
will install PyStan 3 in Python 3.10 or later virtual environment.
Before installing PyStan, you must install g++ on Ubuntu running the code
sudo apt
install g++
The following command
conda install
pymc3
install PyMC3 in Python 3.10 virtual environment.
If you change the current directve in Ubuntu to a folder or directory in Windows, then you can process files either in Windows or in Ubuntu.
By command cd, you can change the current directory to a folder in Windows. Drives in Windows are represented as directives in /mnt/ in Ubuntu, as shown by the following sample.
(base) yasuharu@PSY-PM-PC:~$ cd /mnt/
(base) yasuharu@PSY-PM-PC:/mnt$ ls
c
d f g
h i j
k l r
wsl
In the above, drives C, D, and so on are denoted by c, d, and so on. Capital case letters of drive names in Windows correspond to lower case letters of directory names in Ubuntu.
To use usb memories, they must be mounted.
Prepare a directory, on which a usb memory is mounted. For example, the following command makes a directory /mnt/usb_t/
sudo mkdir
/mnt/usb_t/
By the following code, a drive in Windows is mounted on a directive in Ubuntu.
sudo mount –t drvfs <drive name in Windows> <directive name in Ubuntu>
The option “-t drvfs” is necessary.
The following example shows mounting of drive T in windows on directory /mnt/usb_/ in Ubuntu.
Notice that drive T in Windows is denoted by t:, i.e., ‘t’ + ‘:’.
(base) yasuharu@PSY-PM-PC:/mnt$ ls
c
d f g
h i j
k l r
wsl
(base) yasuharu@PSY-PM-PC:/mnt$ sudo
mkdir /mnt/usb_t/
[sudo] password for yasuharu:
(base) yasuharu@PSY-PM-PC:/mnt$ ls
c
d f g
h i j
k l r
usb_t wsl
(base) yasuharu@PSY-PM-PC:/mnt$ sudo
mount -t drvfs t: /mnt/usb_t/
(base) yasuharu@PSY-PM-PC:/mnt$ cd
/mnt/usb_t/
(base) yasuharu@PSY-PM-PC:/mnt/usb_t$
ls
BinomialDistriStan
PointEstimates.zip
RegressionStan.zip
Temp20220421
samples_2.zip
BinomialDistriStan.zip PythonSamples
SDT_d_prime
TempFolder
temp0311
DownLoad
RSamples
Sample
UpDown3Cat
PointEstimates RegressionStan 'System Volume Information' samples_2
When you unmount the drive, use umount command.
See the following sample.
(base) yasuharu@PSY-PM-PC:/mnt/usb_t$
cd /mnt/
(base) yasuharu@PSY-PM-PC:/mnt$ sudo
umount /mnt/usb_t/
(base) yasuharu@PSY-PM-PC:/mnt$ ls
c
d f g
h i j
k l r
usb_t wsl
(base) yasuharu@PSY-PM-PC:/mnt$ cd
usb_t
(base) yasuharu@PSY-PM-PC:/mnt/usb_t$
ls
(base) yasuharu@PSY-PM-PC:/mnt/usb_t$
After unmounting, the directory is remains. If you want to remove it, you must use rmdir command as shown by the following sample.
(base) yasuharu@PSY-PM-PC:/mnt/usb_t$
cd /mnt/
(base) yasuharu@PSY-PM-PC:/mnt$ ls
c
d f g
h i j
k l r
usb_t wsl
(base) yasuharu@PSY-PM-PC:/mnt$ sudo
rmdir /mnt/usb_t/
(base) yasuharu@PSY-PM-PC:/mnt$ ls
c
d f g
h i j
k l r
wsl
(base) yasuharu@PSY-PM-PC:/mnt$
You can edit a script file in Windows using Windows soft ware, and run the code in Ubuntu, as shown in Figure 1.
Figure 1
Notice that the content of Ubuntu directory /mnt/usb_t/RegressionStan/ is the same as that of Windows folder T:Sample\RegressionStan.
The source file is shown in Listing 1.
When you run the script in Listing 1, any graphs are not displayed by matplotlib.pyplot.show() method. But, you can store graphs in files by savefile() method, as shown in Figure 2.
Figure 2
Listing 1. Python sample script, which uses Stan script in Listing 2.
import pystan
import matplotlib.pyplot as plt
import scipy.stats as scst
import numpy as np
RawData = [ ['City', 'Temperature',
'Latitude'],
['Sapporo',
15,
43.1],
['Sendai',
20,
38.3],
['Niigata',
17,
37.9],
['Kanazawa',
18,
36.6],
['Tokyo', 24,
35.7],
['Osaka', 22,
34.7],
['Fukuoka',
22,
33.6],
['Kochi', 24,
33.6],
['Kagoshima', 23,
31.6],
['Naha',
26,
26.2]
]
ID = []
Y = []
X = []
N = len(RawData) - 1
for i in range(N):
ID.append(RawData[i + 1][0])
Y.append(RawData[i + 1][1])
X.append(RawData[i + 1][2])
for v in zip(ID, Y, X):
print(v)
Data = {'N': N, 'Y': Y, 'X': X}
sm = pystan.StanModel(file =
'Regression.stan')
fit = sm.sampling(data = Data, n_jobs
= 1)
print(fit)
f = open('fit.txt', 'w')
f.write('fit = \n{}\n'.format(fit))
f.close()
Sampleb0 = fit['b0']
print('len(Sampleb0) = ',
len(Sampleb0))
plt.title('Samples of b0')
plt.xlabel('b0')
plt.ylabel('frequency')
plt.hist(Sampleb0)
plt.savefig('Figb0.png')
plt.show()
plt.close()
Sampleb1 = fit['b1']
nb1 = len(Sampleb1)
print('len(Sampleb1) = ', nb1)
npos = 0
for v in Sampleb1:
if v > 0.0:
npos
+= 1
plt.title(('Samples of b1\n') +
('Prop(b1 > 0) = {0:.3f}'.format(npos/nb1)))
plt.xlabel('b1')
plt.ylabel('frequency')
plt.hist(Sampleb1)
plt.savefig('Figb1.png')
plt.show()
plt.close()
Samplesgm = fit['sgm']
plt.title('Samples of sgm\n')
plt.xlabel('sgm')
plt.ylabel('frequency')
plt.hist(Samplesgm)
plt.savefig('Figsgm.png')
plt.show()
plt.close()
r, p = scst.pearsonr(Sampleb0,
Sampleb1)
print('r = ', r)
plt.xlabel('b0')
plt.ylabel('b1')
plt.title('r = {0:.3f}'.format(r))
plt.plot(Sampleb0, Sampleb1, 'b.')
plt.savefig('Figb0b1.png')
plt.show()
plt.close()
PredYs = fit['mu']
YsArray = []
for i in range(10):
YsArray.append([])
for v in PredYs:
for i in range(10):
YsArray[i].append(v[i])
MedPreds = []
for i in range(10):
MedPreds.append(np.percentile(YsArray[i], 50))
plt.xlabel('Data')
plt.ylabel('Prediction')
plt.plot(Y, MedPreds, 'bo')
for nm, y, pred in zip(ID, Y,
MedPreds):
plt.text(y + 0.1, pred +
0.1, nm)
plt.plot([14, 28], [14, 28], 'b-')
plt.savefig('FigPred.png')
plt.show()
plt.close()
Listing 2. Stan sample script.
data{
int N;
real Y[N];
real X[N];
}
parameters{
real b0;
real b1;
real<lower = 0> sgm;
}
transformed parameters{
real mu[N];
for (i in 1:N)
mu[i] = b0 + b1 * X[i];
}
model{
for (i in 1:N)
Y[i]
~ normal(mu[i], sgm);
}