Code Monkey home page Code Monkey logo

bap3's People

Contributors

aloctavodia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bap3's Issues

Chapter 2 Exercise #1

I’m in need of some clarification on Chapter 2, Exercise #1: are the beta parameters in question from page 34 (Code 1.6), or are they referenced elsewhere?

It wasn’t entirely clear to me.

Thanks!

Kurtosis on page 23, third edition

kurtosis shown in the figure 1.9 is not the actual kurtosis but the "excess kurtosis" which is the kurtosis relative to a normal distribution. the normal distribution has a kurtosis of 3. this figure shows it as zero which you get if you subtract 3. same is true for other shown distributions as well. it should be corrected in the text.

Pg 105 typo

Page 105 states

... will need to properly index the vectors $\mu_p$ and $\nu_p$ to match the total number of players

but I believe "players" should be changed to "positions"

Incorrect Statement? Pg 94, commenting on Code 3.1/ 3.2

At the bottom of page 94, under code block 3.1, there is a line:

We are leaving 𝜎 without hyperpriors; in other words, we are assuming that the variance between observed and theoretical values should be the same for all groups.

However, we have a fully unpooled model with dims appearing in the prior. Thus, I believe the statement should actually be that we are assuming each group has its own unique 𝜎. To get the same variance for each group, I think we'd have to get rid of the dims argument in the prior for 𝜎 (and the corresponding [idx] in the likelihood).

Not sure what pz is?

Sorry if this is the wrong place for this, as it's probably not an error as much as a misunderstanding. But I have followed all the installation instructions and have activated a bap3 environment in conda, and when I try to write commands that use "pz" such as "pz.BetaBinomal()" I am getting an error. I have scoured Google and ChatGPT in search of answers but just can't get this code to run correctly. Am I missing something?

Chapter 1 ex 7, unclear & preliz "error" in solution

To me, before looking at the solution, it wasn't clear what the goal of the exercise was. Since this is all quite new it would have helped me if it said something along the lines of "Draw samples of different sizes from a SkewNormal distribution with parameters you choose, with sizes like 10, 100 and 1000, and see ..."

For me the solution of the ipynb on github didn't work. I got an error on "skn.rv_frozen.stats("mv")": AttributeError: 'SkewNormal' object has no attribute 'rv_frozen'

Using skn.moments("mv") did work.

I'm using preliz version 0.8.1, which is currently the latest version (not 0.3.6 as listed in the book)

preliz: name 'FloatSlider'

Screenshot 2024-02-26 at 5 42 54β€―PM

Using VSCode I'm getting this error but while using colab notebooks everything seems fine. I'm on a Mac computer
Any help would be appreciated.

Please explain what is the Bernoulli distribution

Thank you for this very interesting book that opened my mind to Bayesian statistics!
One small feedback I want to give is regarding the Bernoulli distribution.
This distribution is introduced in code 2.2 p. 52, but its relationship with the Binomial distribution is not clearly explained.
Of course, it is possible to understand what is Bernoulli with a small search on Wikipedia, but I thought for completeness, it would be good to say it in a sentence in the book.

Why no log-normal distributions in the examples?

Thanks again for the good reading!
I have not yet been to the end of the book, but something which surprised me is the absence of the log-normal distribution in the examples given.
I thought the log-normal was a very natural distribution for always-positive variables (some people call them Jeffreys variables, if I am not mistaken).
And indeed, the log-normal distribution appears around us: the distribution of temperatures (in K) in the universe, the distribution of resistance values in electronic goods, the distribution of earnings in the society, etc.
Is there a reason for omitting this important distribution?

pg 254 wrong Greek letter for inverse link function

Page 254 states, "We call $\phi$ the inverse link function and $\phi$ is some other function we use to potentially transform the data..."

I believe the first $\phi$ should be $\psi$ based on the equation given on page 254

Typo page 34

Chapter 1, Getting the posterior, p34, "Reordering it, and noticing this has the form of a Beta distribution, we get:" ... the subscript of the beta term is incorrect, the +N-y shouldn't be subscripted (I think?)

Code 1.2 pz.BetaBinomial: AttributeError: module 'preliz' has no attribute 'BetaBinomial'

Hi @aloctavodia,

I am writing because I got a problem in the Code 1.2. I installed it with pip install preliz==0.0.2. However, when I code Β΄pz.BetaBinomial(alpha=10, beta=10, n=6).plot_interactive()Β΄ I got: AttributeError: module 'preliz' has no attribute 'BetaBinomial'. Do you suggest some way to solve it? I keep attentive.

Best regards from Chile.
Thanks in advance for sharing your knowledge.
Bryan

PS: Let me know if I can write you in Spanish.

Pg. 124, "a few bulk points"?

On pg 124, there is this like explaining why we want a shifted exponential:

In my experience, this is fine for data with none to moderate outliers, but for data with extreme outliers (or data with a few bulk points), like in Anscombe’s third dataset, it is better to avoid such low values.

What does "a few bulk points" mean? Is this implying:

  1. A dataset with a small number of data points in the "expected" regime, with emphasis on the small number part? If so, we might perhaps update the sentence to say or data with *only* a few bulk points
  2. Or, is "bulk points" referring to the "not expected" part of the data, talking perhaps about a cluster of "outliers" (but they're all close together)?
  3. Or, something else?

Question regarding plot for the simple regression example in chapter 4

I understand the code behind figure 4.4. Looking around in Arviz I discovered the plotting funtion plot_lm that should be able to do something similar to the plots in figure 4.4. I would like to replace the Python code for fig 4.4 with a single method call :-)

In the attached notebook I have created a slimmed down version of the example and added a call to plot_lm. However I have problems getting it to work. It looks like some naming convention issue for coords in the inferencedata object.

In the notebook I describe both what problem I experience and how I fix it. Can you please advice if there is a better way of doing this. Thanks.

Chp_04.ipynb.md

Typo p. 115 for noise term?

Thanks again for this excellent book!
At bottom of p. 115, it is written "The noise term \epsilon".
Am I mistaken or should it be "The noise term \sigma"?

Typo? Page 82/ Code Block 2.27: Normal -> Gamma

The likelihood used to be a Normal (code block 2.26), but now is inexplicably a Gamma (text was only talking about showing off pymc's coordinates/ dimensions specification of models vs using shape). This is also this way in cell 36 of Chapter 2's code notebook.

pg 194-195

The code in 6.14 uses the temperature variable but it should use hour instead. The caption for figure 6.5 says it's for the "bikes model with temperature and humidity" but it's for the polynomial bikes models with hour.

Possible typos in Code 2.26 of page 81 in BAP3 book

I think that there is a typo in line 2 of Code block 2.26 of page 81.:
πœ‡ = pm.Normal("πœ‡", mu=0, sigma=10, shape=4)
I'm not sure if a normal distribution centered around zero is a logical prior for this example.

The text right above the code block says that the code should be "almost the same as model_g", who's mean was defined in Code 2.12 of page 66 as:
πœ‡ = pm.Uniform('πœ‡', lower=40, upper=70)

But Code 2.27 of page 82 is described as an alternative syntax to Code 2.26 and the mean was defined there as:
πœ‡ = pm.HalfNormal("πœ‡", sigma=5, dims="days")

Is a correction needed to avoid confusion?

pg 133 "model interactions"

On pg 133 is states, "We have been using the linear motif to model the mean of a distribution and, in the previous section, we used it to model interactions."

Interactions have not been introduced to this point. Not sure if this is a typo and it's supposed to say something else like probably, or if it's a holdover from a previous version where interactions were previously discussed.

Fig 3.6

Figure 3.6 indicates a half-normal prior for $\nu$ but the code in 3.5 and described in the text uses a Gamma distribution.

chap_01.ipynb warning "Possibly did not call parent GridSpec with the "figure" keyword"

When I run the cell content as

x, y = np.mgrid[-4:4:.01, -4:4:.01]
pos = np.empty(x.shape + (2,))
pos[:, :, 0] = x; pos[:, :, 1] = y
rv = pz.MvNormal([0, 0], [[1, 0.8], 
                          [0.8, 1]])

x_value = pos[:, :, 0][:,0]
x_density = rv.pdf(pos)

left, width = 0.1, 0.65
bottom, height = 0.1, 0.65
bottom_h = left_h = left + width + 0.02

rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.2]
rect_histy = [left_h, bottom, 0.2, height]


plt.figure(1, figsize=(8, 8))

ax_joint = plt.axes(rect_scatter)
ax_x = plt.axes(rect_histx)
ax_y = plt.axes(rect_histy)

ax_joint.imshow(x_density, cmap='cet_gray_r', origin='lower', extent=[-3, 3, -3, 3])

ax_joint.plot(x_value, x_density[400]*2, 'k:', lw=2)
ax_joint.plot(x_value, x_density[500]*2+1, 'k:', lw=2)
ax_joint.plot(x_value, x_density[300]*2-1, 'k:', lw=2)

ax_x.fill_between(x_value, x_density.sum(1), color='C2')
ax_y.fill_betweenx(x_value, x_density.sum(1), color='C2')


for ax in [ax_joint, ax_x, ax_y]:
    ax.grid(False)
    ax.set_facecolor('w')
    ax.set_xticks([])
    ax.set_yticks([])
ax_joint.set_xlim(-3, 3)
ax_joint.set_ylim(-3, 3)
ax_x.set_xlim(-3, 3)
ax_y.set_ylim(-3, 3)
ax_x.set_xlim(-3, 3)
ax_joint.set_ylabel('$B$', rotation=0, labelpad=20, fontsize=18)
ax_joint.set_xlabel('$A$', fontsize=18)

ax_joint.text(-2.5, 2.5, '$p(A, B)$', fontsize=18, color='k', weight='medium')
ax_y.text(10, 0, '$p(B)$', fontsize=18, color='k', weight='medium')
ax_x.text(-0.2, 15, '$p(A)$', fontsize=18, color='k', weight='medium')
ax_joint.text(1, -2, ' ... $p(A \mid B)$', fontsize=18, color='k', weight='medium')
plt.savefig('../fig/joint_marginal_cond.png')

jupyter issue 2 warnings

/tmp/ipykernel_5463/4085251966.py:52: UserWarning: There are no gridspecs with layoutgrids. Possibly did not call parent GridSpec with the "figure" keyword
plt.savefig('../fig/joint_marginal_cond.png')
/home/osvaldo/anaconda3/envs/bap3/lib/python3.11/site-packages/IPython/core/pylabtools.py:152: UserWarning: There are no gridspecs with layoutgrids. Possibly did not call parent GridSpec with the "figure" keyword
fig.canvas.print_figure(bytes_io, **kw)

I looked up the matplot API and found if I changed

plt.figure(1, figsize=(8, 8))

ax_joint = plt.axes(rect_scatter)
ax_x = plt.axes(rect_histx)
ax_y = plt.axes(rect_histy)`

to

_, axes_arr = plt.subplots(1, 3, figsize=(8, 8))
ax_joint, ax_x, ax_y = axes_arr
ax_joint.set_position(rect_scatter)
ax_x.set_position(rect_histx)
ax_y.set_position(rect_histy)

The warning problems can be fixed.

Estimating difference in ARPU (using knowledges of chapter 2)

People, hey!

I've already read first 2 chapters of the book (BAP3) – it's super useful.

After that I decided to experiment with models and my goal is to estimate difference between ARPU for different coutnries.
I started with code below, but then was stucked because sampling countinues infinite amount of time.
If someone can help me with an advice πŸ€• regarding this model, explain the problem, I will really appreciate it.

All data stored in pandas dataframe called data where rows = users. It contain following columns:
player_id – unique user id
country_code – US or JP
revenue_7 – cumulative revenue to 7th day of user's life
is_payer – 0 or 1 computed from revenue_7, depending on revenue amount (zero or more than zero)

Currently my model looks like that:

country = np.array(['US', 'JP'])
country_idx = pd.Categorical(data['country_code'], categories=country).codes
coords = {'country': country, 'country_flat': country[idx]}

# ChatGPT suggested me to use that for ignoring of zero values, it allowed me to use Gamma dist
revenue_observed = np.where(data['is_payer'] == 1, data['revenue_7'], np.nan)

with pm.Model(coords=coords) as model:
    
    p = pm.Beta('p', alpha=1, beta=1, dims='country')
    y = pm.Bernoulli('y', p=p[country_idx], observed=data['is_payer'])

    mu = pm.HalfNormal('mu', sigma=10, dims='country')
    sigma = pm.HalfNormal('sigma', sigma=15, dims='country')
    revenue = pm.Gamma('revenue', mu=mu[country_idx], sigma=sigma[country_idx], observed=revenue_observed)

    arpu = pm.Deterministic('arpu', p * mu, dims='country')
    
    idata = pm.sample()
    idata.extend(pm.sample_posterior_predictive(idata))

Why is coinflip likelihood a Bernoulli in Chapter 2 when it was a Binomial in Chapter 1?

Why is the distribution for the likelihood of the coin flip example in Chapter 2 a Bernoulli when it was a Binomial distribution in the same coinflip example in Chapter 1? To me a Binomial seems more intuitive since the coin flip is preformed multiple times (4 trials in the example). Clarity on this becomes even more relevant when performing Exercise nbr 1 of Chapter 2 which requires comparing the coin flip models between chapters 1 and 2.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.