Skip to content

Instantly share code, notes, and snippets.

@Johnicholas
Created April 13, 2015 12:38
Show Gist options
  • Save Johnicholas/6c5823592376c278c0b7 to your computer and use it in GitHub Desktop.
Save Johnicholas/6c5823592376c278c0b7 to your computer and use it in GitHub Desktop.
mark and recapture for generative text
// This is using the "Bayesian" formula taken from
// http://en.wikipedia.org/wiki/Mark_and_recapture
// to provide a crude estimate of "how many" outputs the grammar might generate.
//
// As I understand it, it is quite easy to technically have infinite outputs,
// and this technique "wrongly" will always give a finite answer.
// However, informally, we want a lot of visible variety,
// and technically-infinite doesn't provide guidance towards
// more visible variety, but abundance will.
//
// The idea is that as you work on a generative-text thing,
// you try to keep driving the abundance number up and up,
// (along with other good qualities, of course).
var seenFirst = [];
var effort = 1000;
for (var i = 0; i < effort; i += 1) {
seenFirst.push(myGrammar.flatten("#origin#"));
}
var firstSample = [];
seenFirst.forEach(function(item) {
if (firstSample.indexOf(item) < 0) {
firstSample.push(item);
}
});
var K = firstSample.length;
var seenSecond = [];
for (var i = 0; i < effort; i += 1) {
seenSecond.push(myGrammar.flatten("#origin#"));
}
var secondSample = [];
var k = 0;
seenSecond.forEach(function(item) {
if (secondSample.indexOf(item) < 0) {
secondSample.push(item);
if (firstSample.indexOf(item) >= 0) {
// we saw this one before
k += 1;
}
}
});
var n = secondSample.length;
var li = document.createElement("li");
li.appendChild(document.createTextNode("abundance is " +
Math.floor((K-1)*(n-1)/(k-2)) +
" plus or minus " +
Math.sqrt((K-1)*(n-1)*(K-k-1)*(n-k+1)/((k-2)*(k-2)*(k-3))).toP\
recision(4)
));
document.getElementById("results").appendChild(li);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment