Bitcoin Forum
August 21, 2018, 12:53:53 AM
 News: Latest stable version of Bitcoin Core: 0.16.2  [Torrent].
 Home Help Search Donate Login Register
 Pages: [1]
 Author Topic: How do you calculate probability?  (Read 3382 times)
mizerydearia
Hero Member

Offline

Activity: 574
Merit: 500

 July 19, 2010, 03:13:43 AM

.I am trying to correct some PHP code to calculate probability of generating a block for bitbot.  bd_ and llama have provided some debate regarding how to calculate it and I suggest it be moved to forum for further eyes to analyze their findings and to come to a conclusion on best formula to calculate probability.

For now, here's code I've been using:
Code:
//var p = target*rate*1000/Math.pow(2,256);
//var txt = "<table border='1'>
// <tr style='font-weight:bold'> <td>Probability</td> <td>Time</td></tr>\n"+"
// <tr> <td>Average</td> <td>"+getHumanTime(1/p)+"</td></tr>\n"+getRow(0.50,p)+getRow(0.95,p)+"</table>";
//function getRow(prob,p) { return "<tr><td>"+100*prob+"%</td><td>"+getHumanTime(-Math.log(1-prob)/p)+"</td></tr>\n";

bcscale(256);
\$a = bcsub(bcpow(2,256),1);
\$b = bcmul(bcpow(2,32),\$difficulty);
\$target = bcdiv(\$a,\$b); // target
\$pph = bcdiv(\$target,bcpow(2,256)); // probability per hash
if (isset(\$_GET["r"]) && \$_GET["r"] != "") {
\$rate = \$_GET["r"];
if (is_numeric(\$rate)) {
function humantime(\$secs) {
if (\$secs<0) return false;
\$m = (int)(\$secs / 60); \$s = \$secs % 60; \$s = (\$s <= 9) ? "0\$s" : \$s;
\$h = (int)(\$m / 60); \$m = \$m % 60; \$m = (\$m <= 9) ? "0\$m" : \$m;
\$d = (int)(\$h / 24); \$h = \$h % 24;
return \$d."d \$h:\$m:\$s";
}
echo "http://bitcointalk.org/index.php?topic=471.0";exit;
// Where does this go?
// <bd_> To compute the number of hashes needed to reach a success probability of P_targ, you'll want n >= log(P_targ)/log(1-P)
// <bd_> So time needed is (log(P_targ)/log(1-P))/rate

// <bd_> To compute the probability per second, you'll want 1-(1-p)^rate, where p is the probability of a single hash being correct
//\$pps = bcdiv(bcmul(\$target,1000),bcpow(2,256)); // probability per second (according to bd_ this is wrong)
bcscale(16); // Next calculation takes too long for higher bits
\$pps = bcsub(1,bcpow(bcsub(1,\$pph),\$rate)); // probability per second
//echo "probabiliy per second == 1-(1-p)^rate == 1-(1-\$pph)^\$rate == \$pps";exit;
// Comment next line for 256-bits of data
bcscale(16);
\$ppr = bcmul(\$pps,\$rate);
//\$ppr = \$pps;
\$etaAvg = humantime(bcdiv(1,\$ppr));
\$eta25 = humantime(bcdiv(-log(.75),\$ppr));
\$eta50 = humantime(bcdiv(-log(.5),\$ppr));
\$eta75 = humantime(bcdiv(-log(.25),\$ppr));
\$eta95 = humantime(bcdiv(-log(.05),\$ppr));
\$eta99 = humantime(bcdiv(-log(.01),\$ppr));
echo "ProbabilityPerSecond(\$ppr) Avg(\$etaAvg) 25%(\$eta25) 50%(\$eta50) 75%(\$eta75) 95%(\$eta95) 99%(\$eta99)";
} else echo "This command requires either no argument or a numeric argument representing khash/sec.";
}
else {
bcscale(32);
\$pph = bcmul(\$pph,1);
echo "ProbabilityPerHash(\$pph)";
}

http://nullvoid.org/bitcoin/bc.php?q=gethashprobability&r=2000 where r is your khash/sec
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1534812833
Hero Member

Offline

Posts: 1534812833

Ignore
 1534812833

1534812833
 Report to moderator
llama
Member

Offline

Activity: 103
Merit: 10

 July 19, 2010, 03:38:52 AM

Actually, it seems that BD_ and I have come to an agreement!

To calculate average (expected value of generation time):

1/(pr)

To calculate how long until you have a T chance of producing at least 1 block:

-ln(1-T)/pr

Where:

p = probability of a given hash resulting in success (TARGET / 2^256)
r = rate in hashes / sec
T = target probability, such as 0.95
ln = log base e

I got to mine just by plugging variables and rearranging the poisson distribution.  BD_ can give you a different formula for the second equation, which he heroically derived from the binomial distribution, but which yields the same effective results.

 Pages: [1]
 « previous topic next topic »