Freethought & Rationalism ArchiveThe archives are read only. |
03-29-2003, 01:02 PM | #11 |
Veteran Member
Join Date: Nov 2001
Location: NCSU
Posts: 5,853
|
DNAUnion,
Please use the "code" blocks for displaying code. Could you please give a simple statement what probability your trying to calculate with your programs. (I've looked at Rhode's paper and am not sure exactly what he was trying ot do either.) I'd just like a stament like, "To calculate the probability that four things chosen out of nine things have two things that match." Or whatever is appropriate for your program. |
03-29-2003, 01:06 PM | #12 | ||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
Perhaps it's just that you can't "speak" VFP: maybe you can only decipher a "real" programming language like C++. Well here, I took the time to recode it in that language. Now you can examine the code and point out my errors. Code:
// calcprob.cpp // This program models choosing lettered tiles from an urn in // order to calculate an empirical probability #include <iostream> #include <stdlib.h> // needed for the rand function #include <time.h> // needed to get current time to seed rand function using namespace std; long GetRandomNumber(int nMin, int nMax); int main() { const int nDiscardTilesOnceChosen = 1; const int nLetteredTiles = 9; const int nTargetTiles = 4; const int nMatchesNeededForSuccess = 1; const int nTrialsPerIteration = 4; const long lIterations = 1000000; int nTrial = 0; int nMatches = 0; int nIndex = 0; int nLooper = 0; int nFoundOne = 0; long lIteration = 0; long lSuccessfulIterations = 0; char cLetter = ' '; char cUrn[nLetteredTiles][3]; // Before doing anything else, intialize the random number generator // using the system clock srand((unsigned)time(NULL)); // Initialize array (fill the urn with lettered tiles) // The columns of the multidimensional array breakdown as follows: // [1] = LETTER: a unique symbol on a tile that gets placed into the urn. // As the program currently stands, this value is not used. // [2] = CHOSEN: has this letter/tile already been chosen from the urn? // If so, it may have been disarded and so not available // any more, or it may have been replaced and available to // be selected again. Which occurs for an already selected // tile depends upon the value of the const variable // nDiscardTilesOnceChosen. // [3] = TARGET: is this letter/tile one of the targets? for (nIndex = 0; nIndex < nLetteredTiles; nIndex++) { cLetter = 64 + nIndex; cUrn[nIndex][1] = cLetter; cUrn[nIndex][2] = 'F'; cUrn[nIndex][3] = 'F'; } // Choose x number of tiles from the Urn to serve as targets for (nLooper = 1; nLooper <= nTargetTiles; nLooper++) { nFoundOne = 0; while (nFoundOne == 0) { nIndex = (int) GetRandomNumber(0, nLetteredTiles - 1); if (cUrn[nIndex][3] == 'F') { cUrn[nIndex][3] = 'T'; nFoundOne = 1; } } } // Begin selecting tiles from the Urn. for (lIteration = 1; lIteration <= lIterations; lIteration++) { // New iteration: need to clear all CHOSEN flags for (nIndex = 0; nIndex < nLetteredTiles; nIndex++) { cUrn[nIndex][2] = 'F'; } nMatches = 0; // Give the user some output throughout the process cout << "Iteration " << lIteration << " of " << lIterations << endl; for (nTrial = 1; nTrial <= nTrialsPerIteration; nTrial++) { // Pull a single tile out of the urn nFoundOne = 0; while (nFoundOne == 0) { nIndex = (int) GetRandomNumber(0, nLetteredTiles - 1); if (nDiscardTilesOnceChosen == 0) { // Doesn't matter if the tile has been // chosen previously because selected // tiles are placed back into the urn. nFoundOne = 1; } else if (cUrn[nIndex][2] == 'T') { // This tile has already been chosen and // discarded: it can't be selected again. nFoundOne = 0; } else if (cUrn[nIndex][2] == 'F') { // This tile has not been chosen previously. nFoundOne = 1; } } // A tile has been chosen: flag it as such cUrn[nIndex][2] = 'T'; // Does the chosen tile match one of the targets? if (cUrn[nIndex][3] == 'T') { nMatches += 1; } // No need to continue pulling tiles for this iteration // if we've obtained enough matches for success if (nMatches >= nMatchesNeededForSuccess) { nTrial = nTrialsPerIteration + 1; } } // Did we get enough matches for this iteration? if (nMatches >= nMatchesNeededForSuccess) { lSuccessfulIterations += 1; } } cout << "Tiles discarded after being chosen? "; cout << (nDiscardTilesOnceChosen == 1?"Yes":"No") << endl; cout << "Number of lettered tiles in Urn: "; cout << nLetteredTiles << endl; cout << "Number of target tiles: "; cout << nTargetTiles << endl; cout << "Number of matches needed: "; cout << nMatchesNeededForSuccess << endl; cout << "Trials per iteration: "; cout << nTrialsPerIteration << endl; cout << "Total iterations: "; cout << lIterations << endl; cout << "Successful iterations: "; cout << lSuccessfulIterations << endl; cout << "Empirical probability: "; cout << ((float)lSuccessfulIterations / lIterations) * 100 << "%" << endl; return (0); } long GetRandomNumber(int nMin, int nMax) { long lRandomNumber; lRandomNumber = rand(); while (lRandomNumber < nMin || lRandomNumber > nMax) { lRandomNumber = rand(); } return lRandomNumber; } |
||
03-29-2003, 01:13 PM | #13 |
Veteran Member
Join Date: Mar 2002
Location: anywhere
Posts: 1,976
|
Actually, my point 4 was supposed to show that a monte carlo analysis was completely redundant for the problem you are solving... much less having to do it in Visual Foxpro. So, really, that you can program in C neither impresses me, nor does it address the overall theme of my post.
|
03-29-2003, 01:15 PM | #14 | |
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
DNAunion: I don't think Principia will uncover any errors in my code. Using it to determine an empirical probability for what I discussed earlier (see below quote) turned up a result of 96.0258%, which is virtually identical to the theoretical probability of 96.03% I calculated by hand in the below quote.
Quote:
|
|
03-29-2003, 01:20 PM | #15 | |
Veteran Member
Join Date: Mar 2002
Location: anywhere
Posts: 1,976
|
Let me give you an example. I will condense the following verbose and inelegant analysis of yours:
Quote:
P(no matches to first 4 residues) = 5C4/9C4 = 5/126, where nCr is shorthand for n!/(n-r)!/r!. Therefore, P(at least one match in the first 4 residues) = 1 - P(no matches to first 4 residues) = 1 - 5/126. And guess what, the analysis is not that much more difficult for the remaining cases. Anyways, this demonstration is moot beyond supporting what I meant in point 4, since points 1-3 show that your concerns are just mere nitpicking. |
|
03-29-2003, 01:24 PM | #16 | ||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
It did, however, address "derogatory" comments you made in your point #4. |
||
03-29-2003, 01:26 PM | #17 | |
Veteran Member
Join Date: Mar 2002
Location: anywhere
Posts: 1,976
|
Let me put point 4 another way, using another example of DNAunion's:
Quote:
|
|
03-29-2003, 01:28 PM | #18 | |
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
|
|
03-29-2003, 01:30 PM | #19 | |
Veteran Member
Join Date: Mar 2002
Location: anywhere
Posts: 1,976
|
I got so tickled by DNAunion's algebra problem, which he asked me to solve for him, that I decided to construct another analogous problem:
Quote:
|
|
03-29-2003, 01:32 PM | #20 |
Veteran Member
Join Date: Mar 2002
Location: anywhere
Posts: 1,976
|
OK, unless there is more substantive discussion on Rode's SIPF hypothesis, I guess I am done with this thread.
|
Thread Tools | Search this Thread |
|