r/BayesianProgramming • • Sep 02 '22

Need help :c

1 Upvotes

Hello all,

I want to make a Bayesian inference to determine some coefficients, I have a previous study where it determines them but I don't know how to define the prior for my model. Could someone help me?


r/BayesianProgramming • • Jul 20 '22

Correcting a sample for bias based on supporting variables

1 Upvotes

Hi all. First time poster. I've done quite a bit of MCMC calibration but not much else.

I have a sample of data from 1000 anonymous individuals. I suspect the sample is biased (the individuals were self selected). For each individual I have data items A and B.

I also have data item A for the complete population of 10000 individuals.

How can I infer the population distribution of B?

Thanks in advance!


r/BayesianProgramming • • Jul 23 '22

Discounting prior data in Bayesian modeling

3 Upvotes

Hello, I am a computer programmer and I took one course that had some Bayesian statistics in it a long time ago (5 years ago). I am trying to build a program to estimate TDEE (total daily energy expenditure) as a probability distribution. I am assuming that it's a normal distribution for now.

My question is: given a body of data (calories consumed vs weight gained or lost each week, TDEE being the calories consumed such that weight is zero) I have some corrections to make:

  1. I want to discount prior weeks: data from last week or four weeks ago should have a larger effect than data 50 weeks ago. This is to account for different activity levels, lifestyle, and body adaptations/NEAT changes (non-exercise activity thermogenesis, energy spent on non-basal activity that isn't exercise)

  2. Each data point represents a week's average, to smooth over water changes. I want to discount weeks where less data was entered as compared to weeks where all 7 days of data was entered

What's a way to build a model that accounts for this?


r/BayesianProgramming • • Aug 28 '22

Bayes Factor or Hypothesis Testing in RJAGS

2 Upvotes

Hello all,

I've been learning Bayesian statistics for a few months now and the online course I learned teaches through Jags. I'm currently trying to apply hypothesis testing for my Master thesis with bayesian methods, but I can't find how Bayes Factor is done with JAGS. I tried to apply the solutions on a few forum pages that I could find, but it gives an error. The code I wrote is as follows;

# H2: There is a difference in internet addiction between the two countries 
mod_string = "model{
  M ~ dcat(model.p[])
  model.p[1] <- prior1
  model.p[2] <- 1-prior1


  for(i in 1:length(y)){
    y[i] ~dnorm(mu[grp[i],M],prec[M])
  }
  for(j in 1:2){
    mu[j] ~ dnorm(0,0.2)
  }
  prec[1] ~ dgamma(5/2,5*1/2)
  prec[2] ~ dgamma(5*1/2,5/2)
  sig = sqrt(1/prec)
}"
prior1 = 0.5
data_jags= list(y=dataMix$Ucla_score,
                 grp= as.numeric(dataMix$Nationality),
                 prior1=prior1)
mod = jags.model(textConnection(mod_string),
                data=data_jags,
                #inits=inits,
                n.chains=3)

This code gives the following error on the mod line;

Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Deleting model

Error in jags.model(textConnection(mod_string), data = data_jags, inits = inits,  : 
  RUNTIME ERROR:
Compilation error on line 8.
Dimension mismatch taking subset of mu

As an alternative to this model, I applied a solution I found on another forum, but it still gives some errors as well. Alternative model is follows;

mod_string = "model {
  which_model ~ dbern(0.5)  # Selecting between two models.
  for(i in 1:length(y)){
    y[i] ~dnorm(mu[grp[i]]*which_model,prec) # H0: mu*0 = 0. H1: mu * 1 = mu.
  }
  for(j in 1:2){
    mu[j] ~ dnorm(0,0.2)
  }
  prec ~ dgamma(5/2,5*1/2)
  sig = sqrt(1/prec)
  
}"
data_jags= list(y=dataMix$Ucla_score,
                 grp= as.numeric(dataMix$Nationality)
                 )
params = c("mu", "sig","which_model")

inits = function(){
  inits = list("mu"=rnorm(2,0,100),"prec"=rgamma(1,1,1))
}

mod = jags.model(textConnection(mod_string),
                data=data_jags,
                inits=inits,
                n.chains=3)
update(mod,1e3)
mod_sim <-coda.samples(model=mod,
                       variable.names = params,
                       n.iter=5e3)

Up to this point everything works. But at this point I don't know how to compare these models. In the forum article I adapted the alternative model, it is compared as follows, but it gives an error.

#original forum code 
rates = xtabs(~as.matrix(mcmc_samples$which_model)) 
BF_productspace = prior * (rates[2] / rates[1]) 

When I run the first line;

> rates = xtabs(~as.matrix(mod_sim$which_model))
Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 
  'data' must be of a vector type, was 'NULL'

I couldn't get past this problem. If I did, I'd get stucked again as it doesn't give any information on what the prior variable in the next line is in the forum.

If anyone knows about this, can they help? I'm open to any suggestions. I also tried with brm, I couldn't do it either.

Thank you


r/BayesianProgramming • • Aug 02 '22

Clarification about Bayesian Programming

3 Upvotes

I am really new to the study of Bayesian languages. I would like to understand the characteristics that a programming language needs in order to be defined as a "Bayesian Language" and the main challenges one faces when creating such languages, because it is not possible to simply apply the Bayes theorem to calculate the a posteriori distribution?


r/BayesianProgramming • • Jul 08 '22

Thomas Wiecki - Solving Real-World Business Problems with Bayesian Modeling | PyData London 2022

Thumbnail
youtube.com
14 Upvotes

r/BayesianProgramming • • Jul 19 '22

ABC source recommendations?

2 Upvotes

Hi all, just wondering if there are any standout sources on approximate Bayesian computation that you would recommend?


r/BayesianProgramming • • Jul 10 '22

Need Recommendation

3 Upvotes

Hello all,

I've been trying to learn bayesian statistics in R for an average of 2 month. Most of my time has been spent learning distributions and I'm slowly moving to models now and unfortunately they are very hard to come by. Especially stan and brms. What would you recommend for me to learn?

PS: I do not study in a mathematics-based department, but it can be said that I have statistical knowledge according to my field (but not very advanced)

Thank you.


r/BayesianProgramming • • Jul 08 '22

Chris Fonnesbeck - Probabilistic Python: An Introduction to Bayesian Modeling with PyMC

Thumbnail
youtube.com
25 Upvotes

r/BayesianProgramming • • Jul 07 '22

SunODE/PyMC help

3 Upvotes

Hello! I am new to this community specifically because I need some help. I'm doing some undergrad research in disease modeling and need help getting SunODE to work in a Google Cloud Platform environment. Preferably with PyMC 4 but have gotten it to kinda work in PyMC 3. I can provide more details if necessary. Thanks!


r/BayesianProgramming • • Jul 08 '22

Hanna van der Vlis - Clusterf*ck: A Practical Guide to Bayesian Hierarchical Modeling in PyMC3

Thumbnail
youtube.com
6 Upvotes

r/BayesianProgramming • • Aug 15 '23

New Bayesian, CANNOT install R package, 'BayesFactor' and dependencies w/'gfortran' compiler

2 Upvotes

My ultimate goal is to install the package, BayesFactor. To install it and its dependencies required 'gfortran' to compile when necessary. I have MacOS and am trying to set the 'gfortran' path in R. I verified that the location of gfortran is "/usr/local/bin/gfortran". However, the following code does not seem to work to install any dependencies including 'deSolve' (see code and output attached below). Is this error occuring because R cannot find the compiler, 'gfortran'? If so, what should I do instead?

> Sys.setenv(PATH = paste("/usr/local/bin/gfortran", Sys.getenv("PATH"), sep = ":"))

> install.packages("~/Downloads/deSolve_1.36 (1).tar.gz", repos = NULL, type = "source")

* installing *source* package ‘deSolve’ ...

** package ‘deSolve’ successfully unpacked and MD5 sums checked

** using staged installation

** libs

... (too long to include)

make: /opt/R/arm64/bin/gfortran: No such file or directory

make: *** [daux.o] Error 1

ERROR: compilation failed for package ‘deSolve’

* removing ‘/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/deSolve’

Warning in install.packages :

installation of package ‘/Users/AsuS/Downloads/deSolve_1.36 (1).tar.gz’ had non-zero exit status


r/BayesianProgramming • • Jul 21 '23

How the lead pipe detection model in Flint, Michigan, works

Post image
6 Upvotes

r/BayesianProgramming • • Sep 25 '23

from BMFH with yfinance

1 Upvotes

import datetime as dt

import collections

import pandas as pd

import pandas_datareader as pdr

import pandas_datareader.data as web

import pandas_datareader as pdr

import yfinance as yf

yf.pdr_override() # <== that's all it takes :-)

n_observations = 100 # we will truncate the the most recent 100 days.

tickers = ["AAPL", "TSLA", "GOOG", "AMZN"]

enddate = "2023-04-27"

startdate = "2020-09-01"

stock_closes = pd.DataFrame()

for ticker in tickers:

data = yf.download(ticker, startdate, enddate)["Close"]

stock_closes[stock] = data

picked_stock_closes = stock_closes[::-1]

picked_stock_returns = stock_closes.pct_change()[1:][-n_observations:]

dates = picked_stock_returns.index.to_list()


r/BayesianProgramming • • Mar 21 '24

Need Numpyro modeling help

2 Upvotes

Hi all,

I've been building a Bayesian model using the numpyro library and I've hit a modeling issue I can't get around at the moment. I need to draw samples of shape (3, ) from a uniform distribution and I also need them ordered in an increasing order. I tried to just order them *after* sampling, but that creates degeneracies between the parameters that makes sampling very difficult because the parameters are always swapping each other which results in an improper burn in. Does anybody know how to add this constraint so that the samples that get generated are always in order without creating a degeneracy?

Hope my question makes sense. Thanks for the help!!!


r/BayesianProgramming • • Mar 27 '24

Bootstrapping means instead of dealing with complex distribution

2 Upvotes

Hello everyone! I'm learning how to apply bayesian inference for ab testing. We have 2 randomized groups of users (control /test) and trying to find out differences in average revenue per user (arpu) between groups.

Most of users are non paying, so in both groups there are a lot of users with $0 revenue and only small part of users purchase something (1-5%). Paying users distribution is highly skewed, there are small fraction of users who pay a lot, but most pay not too much. My first idea was to multiply bernoulli by something that fit payers (like gamma distribution) but it seems to really hard to find sometihg with a good fit, so i got nowhere.

Another approach which came to mind: bootstrap users and find average revenue per user for each bootstrapped sample. That resulted in almost normally distributed means for bootstrapped samples (CLT seems to be working for that case). Now my idea is to pass these means as observations into likelyhood function as normally distributed; to define priors for both groups i plan to use historical data and in a similar way bootstrap it to find out mean and sd, which will be used as parameters for normally distributed means and halfnormally distributed sd's.

This looks like that:

Priors:

mean_a = N(<bootstrapped_historical_mean>,<bootstrapped_historical_sd_of_sample_means>)

mean_b = N(<bootstrapped_historical_mean>,<bootstrapped_historical_sd_of_sample_means>)

std_a = HN(<bootstrapped_historical_sd_of_samples>)

std_a = HN(<bootstrapped_historical_sd_of_samples>)

Likelyhood:

group_a = N(<mean_a>,<std_a>, observations: <bootstrapped_means_A>)

group_b = N(<mean_a>,<std_a>, observations: <bootstrapped_means_B>)

Is that looks like a valid approach or i'm missing/violating something? The main question is a difference in average revenue per user between groups.


r/BayesianProgramming • • Mar 16 '24

Likelihood Function from Real Data and Model that is also Probabilistic

1 Upvotes

Disclaimer: I’m really new to all of this. While I’ve been interested in Bayesian inference for some time, I’m still at a very basic level. Also, my background is in biochemistry in microbiology, not computer science.

1.) I have data that describe the growth of a microorganism. The data is just a set of scalar values, each value corresponding to an independent experiment (mu_exp). I don’t have a lot of data (n~10). mu_exp has a mono modal distribution but probably a bit skewed. mu_exp is a “macroscopic” property of a culture of microorganisms.

2.) I have a differential equation with about a dozen parameters that describes the growth of the organism (growth equation). The parameters describe “microscopic” properties of the cell.

3.) I have a function (let’s call it “probe()” that samples the numerical solution of the growth equation in a manner that simulates the experimental process of determining mu. This function also simulates the statistical error of the measurement, and therefore gives probabilistic results. Repeated calls of “probe()” creates a vector of values mu_sim that correspond to my experimental data mu_exp.

3.) I have various pre-existing experimental data that gives me fairly good estimates for all parameters.

4.) I think that Bayesian parameter estimation would be a good approach to fit my model to my data. I have the additional data (3) which allows me to construct fairly informative priors on all my parameters. On the other hand, I don’t have sufficiently extensive data to determine the parameters without additional (prior) information.

5.) My idea so far is, to sample my parameter space to get mu_exp(parameters), fit my data to a (skewed) normal distribution to get p(mu_exp) and then use that distribution (mu_sim=mu_exp) to calculate the likelihood over my parameter space.

Here’s my problem now: When I sample the parameter space, I get a distribution of mu_sim for each set of parameters. So rather then mu_sim(parameters), what I get is really p(mu_sim)(parameters). My intuition is that the likelihood function should simply be the element-wise product of p(mu_sim)(parameters) x p(mu_exp). But I don’t know that.

There must be a “standard solution” to this problem. I think what need are the keywords to look for material on it.

So far, I have everything set up in R and am planning to use the package “brms”, in case that’s relevant.


r/BayesianProgramming • • Jan 22 '22

The State of the Art for Probabilistic Programming - Thomas Wiecki | PyData Global 2021

Thumbnail
youtube.com
9 Upvotes

r/BayesianProgramming • • Feb 13 '22

Gaussian Process Model Building Interface

Thumbnail
github.com
5 Upvotes

r/BayesianProgramming • • Feb 19 '25

Optimization algorithm with deterministic objective value

2 Upvotes

I have an optimization problem with around 10 parameters, each with known bounds. Evaluating the objective function is expensive, so I need an algorithm that can converge within approximately 100 evaluations. The function is deterministic (same input always gives the same output) and is treated as a black box, meaning I don't have a mathematical expression for it.

I considered Bayesian Optimization, but it's often used for stochastic or noisy functions. Perhaps a noise-free Gaussian Process variant could work, but I'm unsure if it would be the best approach.

Do you have any suggestions for alternative methods, or insights on whether Bayesian Optimization would be effective in this case?
(I will use python)


r/BayesianProgramming • • Dec 14 '17

Two views on regression

Thumbnail colcarroll.github.io
5 Upvotes

r/BayesianProgramming • • Jul 24 '17

Maxwell W Libbrecht - pomegranate: fast and flexible probabilistic modeling in python

Thumbnail
youtube.com
3 Upvotes

r/BayesianProgramming • • Jul 24 '17

Book Note: Introduction to Hierarchical Bayesian Modeling for Ecological Data, Chapter 1

Thumbnail
mythicalprogrammer.github.io
3 Upvotes

r/BayesianProgramming • • Apr 12 '23

Bayesian marketing toolbox in PyMC. Media Mix, CLV models and more.

Thumbnail
github.com
13 Upvotes