Bitcoin Forum
October 23, 2018, 11:37:57 PM
 News: Make sure you are not using versions of Bitcoin Core other than 0.17.0 [Torrent], 0.16.3, 0.15.2, or 0.14.3. More info.
 Home Help Search Donate Login Register
 Pages: [1]
 Author Topic: How do you calculate probability?  (Read 10179 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
1540337877
Hero Member

Offline

Posts: 1540337877

Ignore
 1540337877

1540337877
 Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1540337877
Hero Member

Offline

Posts: 1540337877

Ignore
 1540337877

1540337877
 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 »