Comments (17)
Here is the entire script leading up to the problem:
Priors
params N, p0, p1, reuse prior_N
qs = np.linspace(0, 1, num=51)
prior_p0 = make_uniform(qs, name='p0')
prior_p1 = make_uniform(qs, name='p1')
Assemble into joint prior w/ 3-dim
put first two into DataFrame
joint2 = make_joint(prior_p0, prior_N)
joint2.shape
Stack and put result into a Pmf
joint2_pmf = Pmf(joint2.stack())
joint2_pmf.head(3)
Use make_joint again to add in 3rd param
joint3 = make_joint(prior_p1, joint2_pmf)
joint3.shape
joint3.head(3)
result is a DataFrame w/ values of N & p0 in a multi-index that goes down the rows
and values of p1 in an index that goes across the columns
apply stack again
joint3_pmf = Pmf(joint3.stack())
joint3_pmf.head(3)
joint3_pmf.shape
Loop that computes the likelihoods
likelihood = joint3_pmf.copy()
observed = data.sum()
x = data.copy()
for N, p0, p1 in joint3_pmf.index:
x[0] = N - observed
y = compute_probs(p0, p1)
likelihood[N, p0, p1] = multinomial.pmf(x, N, y)
from thinkbayes2.
Hi. To make sure I understand the question, are you reporting an error in my code or asking for help debugging?
from thinkbayes2.
Hi - I used to code as it was written in the book and received an error; however, I'm not sure if there is a code error or a different issue and wanted to see if you had a suggestion as to why the code won't work as written.
from thinkbayes2.
Here's the notebook where you can run the code (which works for me):
https://colab.research.google.com/github/AllenDowney/ThinkBayes2/blob/master/notebooks/chap15.ipynb
In the notebook you can print some of the intermediate values, or check the shape of the arrays/DataFrames. That should help with debugging.
from thinkbayes2.
Shapes of the arrays match what was expected in the book. As do the heads up until joint3_pmf. At that point, the triplet doesn't show up, only a pair. It seems there is some issue with make_joint, Pmf, and stack, which has worked fine until now.
from thinkbayes2.
When you run the code in the notebook I linked, does it work?
from thinkbayes2.
Yes, it does. I understand the problem is on my end.
Do you have any thoughts as to why this issue emerged in this instance of the three-parameter, but never before?
from thinkbayes2.
If you display joint3_pmf.index
, does it have three columns?
from thinkbayes2.
Yes - here's the first row:
MultiIndex([((0.0, 0.0), 32)
from thinkbayes2.
MultiIndex([((0.0, 0.0), 32),
((0.0, 0.0), 37),
((0.0, 0.0), 42),
((0.0, 0.0), 47),
((0.0, 0.0), 52),
((0.0, 0.0), 57),
((0.0, 0.0), 62),
((0.0, 0.0), 67),
((0.0, 0.0), 72),
((0.0, 0.0), 77),
...
((1.0, 1.0), 302),
((1.0, 1.0), 307),
((1.0, 1.0), 312),
((1.0, 1.0), 317),
((1.0, 1.0), 322),
((1.0, 1.0), 327),
((1.0, 1.0), 332),
((1.0, 1.0), 337),
((1.0, 1.0), 342),
((1.0, 1.0), 347)],
length=166464)
from thinkbayes2.
from thinkbayes2.
Thank you Allen - I will go through the code again. After running into the issue, I re-wrote the code and still had the problem and then copied it directly, but still no joy.
from thinkbayes2.
Maybe put it in a Colab notebook. That would eliminate the environmental explanation. In the process, you might find the problem. If not, you could share the notebook with me.
from thinkbayes2.
I will do that.
from thinkbayes2.
Here's the script:
Three parameter model
Priors
params N, p0, p1, reuse prior_N
prior_N
qs = np.arange(32, 350, step=5)
prior_N = make_uniform(qs, name='N')
prior_N.head(3)
prior_p0, prior_p1
qs = np.linspace(0, 1, num=51)
prior_p0 = make_uniform(qs, name='p0')
prior_p1 = make_uniform(qs, name='p1')
prior_p0.head(3)
prior_p1.head(3)
Assemble into joint prior w/ 3-dim
put first two into DataFrame
joint2 = make_joint(prior_p0, prior_N)
joint2.shape
joint2.head(10)
Stack and put result into a Pmf
joint2_pmf = Pmf(joint2.stack())
joint2_pmf.head(10)
Use make_joint again to add in 3rd param
joint3 = make_joint(prior_p1, joint2_pmf)
joint3.shape
joint3.head(10)
result is a DataFrame w/ values of N & p0 in a multi-index that goes down the rows
and values of p1 in an index that goes across the columns
apply stack again
joint3_pmf = Pmf(joint3.stack())
joint3_pmf.head(10)
joint3_pmf.index
joint3_pmf.shape
Loop that computes the likelihoods
likelihood = joint3_pmf.copy()
observed = data.sum()
x = data.copy()
from scipy.stats import multinomial
for N, p0, p1 in joint3_pmf.index:
x[0] = N - observed
y = compute_probs(p0, p1)
likelihood[N, p0, p1] = multinomial.pmf(x, N, y)
from thinkbayes2.
It's an environment issue, works in Jupyter but not Spyder unfortunately.
from thinkbayes2.
What's interesting is that this does not work in Jupyter but does work in Spyder.
The following was copied directly - no modification.
Result is that the contour plot won't render. However, Spyder did not have this issue.
observed = k01 + k10 + k11
for N, p in joint_pmf.index:
k00 = N - observed
x = [k00, k01, k10, k11]
q = 1-p
y = [qq, qp, pq, pp]
likelihood[N, p] = multinomial.pmf(x, N, y)
TypeError Traceback (most recent call last)
Input In [24], in <cell line: 3>()
6 q = 1-p
7 y = [qq, qp, pq, pp]
----> 8 likelihood[N, p] = multinomial.pmf(x, N, y)
TypeError: 'numpy.float64' object does not support item assignment
from thinkbayes2.
Related Issues (20)
- Chapter 8 missing cells in zip file HOT 3
- 'Compile' book from code HOT 1
- Chapter 10 Solutions - Definition / Implementation mismatch for the logistic HOT 1
- Chapter 6 Goblin Exercise HOT 2
- Example 6-8 number of Outperforming Portfolios HOT 2
- Chapter 20 Counting Cells Measurement Error Specification HOT 5
- What is considered to be the "data" in the M&M solution? (Ch 2: Bayes's Theorem) HOT 2
- Chapter 1 download data code isn't working HOT 5
- TypeError: Series.__init__() got an unexpected keyword argument 'normalize' HOT 2
- gss_bayes.csv HOT 2
- Oliver Problem - Chapter 6 - General Question HOT 1
- Linda problem should use conditional probability when it comes to sex. HOT 2
- Formulas Not Displaying Properly in github.io version HOT 3
- Chapter 5 Prison Sentences HOT 7
- Chapter 1: caseid definition in the gss_bayes dataset HOT 1
- Chapter 8 - unexpected overlapping curves HOT 1
- Chapter 2 - nomenclature adjustment HOT 2
- Chapter 4 - Proper Prior Probability Initialisation
- Chapter 4 - Typo in Plot Legend
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from thinkbayes2.