GithubHelp home page GithubHelp logo

probmods / probmods2 Goto Github PK

View Code? Open in Web Editor NEW
291.0 23.0 76.0 10.2 MB

probmods 2: electric boogaloo

Home Page: https://probmods.org/v2

HTML 0.44% CSS 1.79% JavaScript 95.87% TeX 1.76% Shell 0.14%
probabilistic-programming tutorial webppl cognitive-science

probmods2's People

Contributors

andrewnam avatar bantic avatar benpeloquin7 avatar benpry avatar cmoebus avatar erindb avatar erocarrera avatar femtomc avatar hans avatar hawkrobe avatar jkhartshorne avatar jpchen avatar jungwonbyun avatar longouyang avatar madeofmistake avatar marielajennings avatar maxkrieger avatar mhtess avatar miketam1021 avatar mrakgr avatar mrhwick avatar mvdoc avatar ngoodman avatar rsarky avatar stuhlmueller avatar thorstink avatar undefdev avatar vincentderk avatar w4nderlust avatar zenkavi 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

probmods2's Issues

DPmem in "Non-parametric models"

In the "Non-parametric models" chapter, in section "Properties of DP Memoized Procedures", the two examples with code

var memoizedGaussian = DPmem(1, gaussian);
viz(repeat(10000, function() {return memoizedGaussian(0,1)}));
var memoizedGaussian = DPmem(1, gaussian);
viz(repeat(10000, function() {return gaussian(0, 1)}));
viz(repeat(10000, function() {return memoizedGaussian(0,1)}));

do not work since DPmem is undefined. Copying the code for DPmem from the previous examples doesn't seem to do the right thing: the graph resulting from the

viz(repeat(10000, function() {return memoizedGaussian(0,1)}));

line produces a graph with a single bar assigning all probability to null.

patterns of inference - medical diagnosis - lung disease

In patterns of inference, on the example Medical Diagnosis halfway down the page this is stated:

"To illustrate, observe how the probabilities of cold and lungDisease change when we observe cough is true:"

followed by:

"Both cold and lung disease are now far more likely that their baseline probability: the probability of having a cold increases from 2% to around 50%; the probability of having lung disease also increases from 1 in a 1000 to around 50%."

This seemed wrong to me immediately, considering the code is:

var cough = (cold && flip(0.5)) || (lungDisease && flip(0.5)) || flip(0.001);

lung disease and cold should have about the same prior probability.

the actual code looks like this:

var smokes = flip(.2);
var lungDisease = flip(0.001) || (smokes && flip(0.1));
var cold = flip(0.02);

which means lung disease is 2.1% given you know nothing about the patient or, expressed similarly, 21 in 1000 to around 50%

fix _.where in BDA chapter

I think we updated the webppl version to use lodash internally, but some of the code in the BDA chapter uses the underscore forms (e.g. _.where) so the code breaks.

Questions concerning 'Prelude: Thinking About Assembly Lines'

Hi,
I have three questions.

  • First, what does it mean to be a 'bad' widget? Is it a widget above or below a threshold ?
  • Second, what is picked from the assembly line ? The good or the bad widget ? Or vice versa, what is left on the assembly line? The bad or the good widget ?
  • Third, what is the logic of the array three ascending thresholds in 'condition(_.isEqual([.6, .7, .8], goodWidgetSeq)'. Wouldn't it be better to ask whether all elements in goodWidgetSeq would be greater than the sampled threshold ?
    Best, Claus

Code for de Finetti's theorem doesn't work

As far as I can tell, this gives an error:

var latent = sample(latentPrior)
var thunk = function() {return observe(latent)}
var sequence = repeat(2,thunk)

In particular, the error I get is "sample() expected a distribution". I'm confused by this, because I was under the impression that Gaussian() was a distribution!

Interestingly, what is highlighted is the observe function, not sample. Not sure what that means.

consider rewriting models to use factor instead of condition

In a number of places, we were forced to use a condition statement in Church, but a factor would be nicer, e.g.

var speaker = function(state, depth) {
  return Infer({method: 'enumerate'}, function() {
    var words = sample(sentencePrior)
    var listenerDist = listener(words, depth)
    condition(state == sample(listenerDist))
    return words
  })
};

could be rewritten as

var speaker = function(state, depth) {
  return Infer({method: 'enumerate'}, function() {
    var words = sample(sentencePrior)
    var listenerDist = listener(words, depth)
    factor(listenerDist.score(state))
    return words
  })
};

BDA Exercise 1 refactor to allow method = forward for prior

While grading the BDA exercises, it seems like a lot of the time the priors come out looking wonky. This is undesirable because this exercise is supposed to introduce the student to the beta family of priors. I suggest refactoring this exercise to have 2 Infers, one with method:"forward" to show the prior, and the other with MCMC or rejection for the posterior.

Fix de finetti template

The current version in chapter 5 uses observe:

var latent = sample(latentPrior)
var thunk = function() {return observe(latent)}
var sequence = repeat(2,thunk)

which is confusing because observe is now a primitive which means something else

add exercise for process models chapter

basic idea is to ask students how you might evaluate these inference models on data (i.e. building on BDA chapter). Some notes:

  • take a noisy-or (or sum) causal reasoning task... is it a fep/blicket? (note analogy to the initial A,B,C coin flipping example in algorithms chapter)
  • have fake 'dataset' of responses (e.g. 'yes'/'no' blicket) as well as RTs at different settings of base rates/number of choices (e.g. assume experimenter had a way of manipulating these)
  • part A: ask students to write linking function from inference model to these data
  • part B: ask students to fill in some code of a BDA model over inference algorithm and rejection sampling & enumeration (or MCMC?), changing base rate and number of choices
  • part C: infer different parameters for different participants?
  • part D: open ended question about "do you think any of these algorithms are a good description of how you intuitively solve this problem"

url in _config.yml

i'm not sure if the url should point to probmods.github.io/prombods2 or just to probmods.org?

Fixing Inference about inference chapter -- partial debugging and workaround

I'm going to be teaching this chapter next week (Feb 15) and was hoping to make an effort to try and fix the chapter before then :)

I tried to do a bit of debugging of the two currently broken vending machine inference examples. From what I can tell, the issue is that buttonsToOutcomeProbs(action) is called from within the inner inference, and the memoization of that call does not hold across samples during the rejection sampling of the inner inference. So, the rejection sampling process is drawing new probabilities each time it samples e.g., ['a'] from the prior (and these are in turn different than the ones returned from the outer inference and graphed). This actually makes some sense -- what if we wanted to make the inference about the probabilities within the inner rather than the outer Infer? The inner infer doesn't know it's an embedded inference. You can see what I'm talking about in the attached debugging file, which eliminates the outer inference.

This isn't an issue in the earlier examples in the chapter because those distributions are all parametric with a fixed number of a priori known actions, and so the probabilities are drawn outside of the inner inference and then passed to that inference.

I've attached some code with a work-around, where I just implemented the rejection sampling by hand without using Infer, but obviously that doesn't solve the broader problem of cases where you want memoization in the outer inference to also hold in the inner inference. I also fixed a couple of other more minor bugs in the models (I think the main one was that the condition in the second model was incorrect).

Besides that, there is also the currently non-converted sketch of the planning section at the end of the chapter. Thanks!

inference_debugging.txt
inference_about_inference_model0.txt
inference_about_inference_model1.txt

Using WebPPL packages

I'm trying to include some MDP/POMDP in a chapter for my fork of probmods2. And I've been banging my head against the code for a few hours, trying to figure out how agentmodels.org includes dependencies like webppl-agents.

Likely, this is because I don't really understand how github pages get compiled, what files & code are required by github pages, what is custom to probmods or agentmodels, etc. Moreover, different javascript packages are included through different methods, sometimes in the same file (ex: the chapter.html template in probmods uses a script tag -- which I understand -- and a structure called custom_js -- which I don't).

If there are any tutorials that I should look at, I'd love being pointed at them. Here's a specific question in the meantime:

I want to use code from webppl-agents in a chapter in my fork of probmods2. agentmodels contains a file called 'package.json' that contains the following:

"dependencies": {
"webppl": "github:probmods/webppl",
"webppl-agents": "github:agentmodels/webppl-agents",
"webppl-dp": "^0.1.1",
"webppl-editor": "github:probmods/webppl-editor",
"webppl-nn": "github:null-a/webppl-nn",
"webppl-timeit": "^0.3.0"
}

So it seems like maybe this is somehow involved in making webppl-agents available? But for the life of me, I can't figure out what calls 'package.json' or under what circumstances. More concerning, I noticed that much of the code from the webppl-agents package is actually included in agentmodels version of webppl.min.js. So I'm not sure that 'package.json' does anything at all.

I'd really appreciate any help in figuring out how to do this!

Ch06-LatentThresholdModelOfWidgetTester // rejections-sampling with two or more observations

In ch06 you provide the code for that model with one good widget-triple leaving the factory. The sizes of that triple are [.6,.7,.8]. I changed the code a bit so that my data or observations can have more triples. Testing that code with this triple [.6,.7,.8] it takes about 5 sec to get a result. But when I add another triple (e.g. [.6,.6,.6]) I get no result even when waiting an hour. Would somebody be so friendly to check my code ?

//==============================================================================================
// Inference of the posterior latent threshold distribution of a widget tester
// WPPL-code modified PCM 2017/02/02
//----------------------------------------------------------------------------------------------
var starttime = Date.now()
var nReplications = 10E2; var nWidgets = 3; var myMaxScore = -0
print("nReplications = "); print(nReplications)
var widgetMachine = Categorical({vs: [.2,.3,.4,.5,.6,.7,.8], ps: [.05,.1,.2,.3,.2,.1,.05]})
viz.hist(widgetMachine, {xLabel: 'SizeOfWidgetProduced'})
// this is the assumed prior latent threshold distribution of the widget tester:
var thresholdPrior = Categorical({vs: [.3,.4,.5,.6,.7], ps: [.1,.2,.4,.2,.1]})
viz.hist(thresholdPrior, {xLabel: 'LatentThresholdPriorOfWidgetTester'})
var observations = [ // these are 2 widgets leaving the factory with the certificate "good"
[.6,.7,.8], [.6,.6,.6]] // runtime with one observation = ca. 5 sec
print("observations (=certifiedGoodWidgetTriples):"); print(observations)
//----------------------------------------------------------------------
var makeGoodWidgetSeq = function(numWidgets, threshold) {
if(numWidgets == 0) { return [] }
else {
var widget = sample(widgetMachine) // production of one widget
return (widget > threshold ? // if widget is "good" then save it in a ...
[widget].concat(makeGoodWidgetSeq(numWidgets - 1, threshold)) : //... basket
makeGoodWidgetSeq(numWidgets, threshold)) }} // otherwise proceed with next
//----------------------------------------------------------------------
var latentThresholdPosteriorOfTester =
Infer({method: 'rejection', samples: nReplications, maxScore: myMaxScore}, function() {
var threshold = sample(thresholdPrior) // sample from tester's latent prior threshold distr
var goodWidgetSeq = makeGoodWidgetSeq(nWidgets, threshold) // nWidgets good widgets saved
// http://underscorejs.org/#isEqual // if widgets are above threshold a n d are accepted ...
condition(all(function(observation) {_.isEqual(observation, goodWidgetSeq)}, observations))
return [threshold].join("") }) // then note this threshold for posterior
//----------------------------------------------------------------------
viz.hist(latentThresholdPosteriorOfTester,
{xLabel: 'LatentThresholdPosteriorOfTester | certifiedGoodWidgetTriples'})
print('LatentThresholdPosteriorOfTester | certifiedGoodWidgetTriples')
display(latentThresholdPosteriorOfTester)
var stoptime = Date.now()
var runTimeSec = (stoptime-starttime)/1000
print("runTime in seconds ="); display(runTimeSec)
//----------------------------------------------------------------------------------------------

Ch06: condition in "Thinking About Assembly Lines"

Hi,
there is the line " condition(_.isEqual([.6, .7, .8], goodWidgetSeq))". I don't understand why the sequence of good widgets should have an ascending order. Isn't it sufficient that all members of the good widgets are above the sampled threshold of the widget tester ?
Best, Claus

fix BDA chapter

after moving earlier in the book, some major revisions are in order (consolidating Noah's comments in chapter markdown with other comments):

  • remove or move+shorten the spinning coin stuff, which is mostly about cogitive models and then bda of cognitive models.
  • start with the existing simple models illustrating the kinds of questions one asks with BDA and the metrics for answers (BF, CI, etc)
  • then do a slightly more complex model: regression analysis.
  • then do BDA of cognitive models.
  • then do full tug-of-war example. needs to be cleaned up for clarity.
  • consider splitting into two chapters, with model selection stuff in part 2 occurring after occam's razor chapter. LMER models also make more sense there....
  • need to add some discussion of CIs.
  • Include illustrations of each point with regression models, as wel as simpler models that are there now.
  • include some discussion of contamination models and other stadard BDA model idioms.
  • most of the editor.put and .get things are unecessary?
  • the data munging is pretty crazy. use more standard DF library?
  • dist.MAP() seems suspect....
  • the BDA of tug-of-war model is too complex and inefficient to be run effectively in the browser and could use either some optimization, or a simpler pedagogical case (with a pointer to the Rwebppl file for the more thorough model).
  • posterior predictive checks are not very well explained -- need to show an example of a good result, not just the bad one. (also having only two data points makes it hard to understand the scatter plot...)

Get footnotes working

Footnotes, when clicked, should show up in a little callout right in the original text location

ch06: Epistemic States

Hi,
I stumpled across a line "return T.get(bEffects, 1)})". I assume that this means a fetch of the second component of the posterior vector bEffects. But why "T.get" ? I did not find a proper docu of "T.get".
Where can I find an explanation ?
Best, Claus

style changes

PR #66 has a lot of changes in .css. I'm not sure if these are silent or actually change the styling. Should check out on different browsers? @jkhartshorne ?

fix nested inference models

a few of the more complex social inference models in Chap. 6 are giving trouble: you can't enumerate for the inner inference because of the continuous dirichlet samples, and it takes too long to build it every time with MCMC or rejection.

You could cache the inner inference, but a couple of the arguments are functions and even if you refactored it to construct the functions inside the inner inference, the relevant parameters are floats so caching wouldn't even help that much. Is this a case where variational would save us, or am I missing some bad inefficiency?

https://probmods.org/v2/chapters/06-inference-about-inference.html#epistemic-states

Meta-note: This is maybe the most interesting disadvantage i've seen compared to Church's rejection-query implementation, since there you can just directly take samples from the inner distribution without needing to first construct a reasonably representative distribution object.

fix conditioning exercises/solutions

  • Make it clear that solutions to exercise 1 should be webppl programs (with results!), not just working out math w/ equations
  • put alternative solution to 3b that doesn't require full unrolling of Bayes rule:
    • P(Alice is nice | Alice smiles twice) = P(Alice is nice AND Alice smiles twice) / P(Alice Smiles twice)
    • P(Alice is nice | Alice smiles twice) = 0.7 * 0.8 * 0.8 / (0.7 * 0.8 * 0.8 + 0.3 * 0.5 * 0.5)
    • P(Alice is nice | Alice smiles twice) = 0.86

Reorganize book

I'm intending to do a major (and long overdue) re-organization shortly. My proposed new outline is:

Introduction — A brief introduction to the philosophy.

Basics:
Generative models — Representing working models with probabilistic programs.
Conditioning — Asking questions of models by conditional inference.
Causal and statistical dependence —
Conditional dependence — conditional dependence, explaining away, screening off, etc.
Bayesian data analysis — Making scientific inferences about data and models
Algorithms for inference — The landscape of inference methods, efficiency tradeoffs of different algorithms.
Rational process modes — From competence to process,
[Models for sequences of observations — Generative models of the relations between data points **Delete: move iid, exch to learning section, move hmm,pcfg elsewhere.]

Learning:
Learning as conditional inference — How inferences change as data accumulate. (Include iid, exch, etc.)
Learning compositional hypotheses — RR, PLOT, etc (include PCFG here?)
Learning continuous functions. — Deep probabilistic models. GPs?
Hierarchical models — The power of abstraction.
Occam's Razor — Penalizing extra model flexibility.
Mixture models — Models for inferring the kinds of things.
Non-parametric models — What to do when you don't know how many kinds there are. **Get rid of this chapter? [add small summary of non-parametrics (dirichlet-discrete to dirichlet process via sequential sampling) to ch 11. get rid of ch 12.]

Social reasoning:
Agents as probabilistic programs — One-shot decision problems, softmax choice
Sequential decisions — Markov Decision Processes and Partially-Observable Markof Decision Processes
Inference about inference — social cognition, pragmatics

Appendix - JavaScript basics — A very brief primer on JavaScript.

fix inference algorithms questions

  • avoid two part questions (unless we enumerate the parts). See Question 2a.
  • 2a students are struggling with variability in marginal distributions - can we fiddle to get things a little more stable?

fix BDA exercises

  • problem 1 should use forward sampling for looking at the prior
    • While grading the BDA exercises, it seems like a lot of the time the priors come out looking wonky. This is undesirable because this exercise is supposed to introduce the student to the beta family of priors. I suggest refactoring this exercise to have 2 Infers, one with method:"forward" to show the prior, and the other with MCMC or rejection for the posterior.
    • Ask a more specific question about what a and b intuitively correspond to? Currently students mostly waxing poetic about the exact shapes of the 3 things we asked them to plot rather than abstracting away to a view of the whole family.
    • Also, maybe increasing strength {a: 10, b: 10} vs. {a: 30, b:30} and asking how predictive differs?
  • problem 2 wording confused several students
    • what is the "metaphor of model or theory evaluation."
    • what is the "paradigm" (intended to mean the psychophysics task paradigm mentioned in the example, but easy to misinterpret as statistical paradigm)
    • can we act more targeted, specific questions here?
  • problem 3
    • C is outdated (all straightforward variables to lift have already been lifted). either need to remove this part, or 'unlift' one of these variables in the chapter. also students often thought 'lifting' just meant putting more variables in the model (i.e. the same as part B). need to say more clearly what we mean by this.
    • need to rewrite to be more explicit about what we're getting at in other parts... (e.g. in part A, ask which condition was explained better by one or the other, and why?)
  • decide whether data in problem 4 should be generated with guessing? (so that there's actually a real answer to part F?) could change the data so that model predictions are too extreme relative to the data...?

add additional scaffolding to distribution appendix

Beta section still needs to be filled in. Also would be nice to have an intuitive image of the simplex like this for different settings of the alpha parameter (especially since the hierarchical models chapter splits out the relative numbers assigned to the categories, like [1,1,4], and the magnitude by which they're multiplied, which pushes them toward the extreme points or toward the center)

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.