I thought a little because the Trace Plot of the stan parameter is hard to see.

Introduction

Currently, I am doing modeling and parameter estimation using stan, but since the Trae plot is crushed and difficult to see, there was a way to make it a little easier to see, so I will share it. (Maybe it's natural for those who know ...) Also, please let me know if there is a better way.

environment

OS Windows10
Python 3.7.4
PyStan 2.19.1

immediately...

stan_model_ver1.py


import arviz
import pandas as pd

data = pd.read_csv("~~.csv")

dat = {"Defined in dictionary"}

model = StanModel(file="~~.stan")

fit = model.sampling(data=dat, n_jobs=-1, seed=999, iter=1000,chains=1)

arviz.plot_trace(fit)

Something like this is drawn with. stan_Figure_1.png

However, if there are many parameters, it is hard to see ...

Therefore,

stan_model_ver2.py


import arviz
import pandas as pd

data = pd.read_csv("~~.csv")

dat = {"Defined in dictionary"}

model = StanModel(file="~~.stan")

fit = model.sampling(data=dat, n_jobs=-1, seed=999, iter=1000,chains=1)

'''====Change below ==='''
fit_df = fit.to_dataframe()

index = fit_df["draw"]
lenght = len(fit_df.keys())-7
for i in range(lenght):
    ob = fit_df[fit_df.keys()[i+3]]
    plt.subplots(figsize=(15, 7))
    plt.title(f"{fit_df.keys()[i+3]}")
    plt.subplot(1, 2, 1)
    sns.distplot(ob)
    plt.subplot(1, 2, 2)
    plt.plot(index, ob)
    plt.savefig(f"figure/stan_figure/{fit_df.keys()[i+3]}.png ")
    plt.show()

will do.

It takes a little time to create a data frame and visualize each parameter one by one, Y[1,2].png

In this way, it becomes easier to see each stage.

bonus?

If the number of parameters is large, even if you try to check the convergence state of each parameter by print (fit.stansummary ()), it may not be displayed. (Especially considering the time series model) In such a case

omake.py


summary_df = fit.stansummary()

file = open('summary_stan.txt', 'w')
string = summary_df
file.write(string)

If you save it as a text file like this, you can check the status of the parameters you want to see.

Recommended Posts

I thought a little because the Trace Plot of the stan parameter is hard to see.
I made a function to see the movement of a two-dimensional array (Python)
I want to see a list of WebDAV files in the Requests module
I thought about why Python self is necessary with the feeling of a Python interpreter
I just changed the sample source of Python a little.
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
NikuGan ~ I want to see a lot of delicious meat! !!
I made a function to check the model of DCGAN
I tried a little bit of the behavior of the zip function
Does TensorFlow change the image of deep learning? What I thought after touching a little
Is there a secret to the frequency of pi numbers?
Two solutions to the problem that it is hard to see the vector field when writing a vector field with quiver () of matplotlib pyplot
I tried to make a site that makes it easy to see the update information of Azure
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
A story that is a little addicted to the authority of the directory specified by expdp (for beginners)
On Linux, the time stamp of a file is a little past.
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
I want to sort a list in the order of other lists
I tried to restore because the Oracle database initialization parameter setting failed.
I want to identify the alert email. --Is that x a wildcard? ---
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
I tried to verify the result of A / B test by chi-square test
Python: I want to measure the processing time of a function neatly
I created a script to check if English is entered in the specified position of the JSON file in Python.
I tried to confirm whether the unbiased estimator of standard deviation is really unbiased by "throwing a coin 10,000 times"
[python] [meta] Is the type of python a type?
I managed to do it because the custom of attaching a zip with a password to an email and saying "I will send you the password separately" is troublesome.
I made a tool to estimate the execution time of cron (+ PyPI debut)
The story of IPv6 address that I want to keep at a minimum
I failed to install django with pip, so a reminder of the solution
I made an appdo command to execute a command in the context of the app
I want to set a life cycle in the task definition of ECS
I want to add silence to the beginning of a wav file for 1 second
I made a tool to automatically back up the metadata of the Salesforce organization
I made a script to record the active window using win32gui of Python
I tried to create a model with the sample of Amazon SageMaker Autopilot
[Introduction to statistics] What kind of distribution is the t distribution, chi-square distribution, and F distribution? A little summary of how to use [python]
How to calculate the volatility of a brand
I thought a little about TensorFlow's growing API
I tried to touch the API of ebay
I tried to correct the keystone of the image
I want to customize the appearance of zabbix
I did a little research on the class
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
Python's itertools is a little useful to know
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
I can't find the clocksource tsc! ?? The story of trying to write a kernel patch
The story of Linux that I want to teach myself half a year ago
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)
The concept of reference in Python collapsed for a moment, so I experimented a little.
I want to take a screenshot of the site on Docker using any font
It was a little difficult to do flask with the docker version of nginx-unit
How to check in Python if one of the elements of a list is in another list
I realized that it is nonsense to use the module without thinking because it is convenient.
I made a command to wait for Django to start until the DB is ready
[Probability] I will explain because the robot problem of Center 2020 Mathematics 1 ・ A was interesting.