Bitcoin Forum
June 22, 2018, 04:35:18 AM *
News: Latest stable version of Bitcoin Core: 0.16.1  [Torrent]. (New!)
   Home   Help Search Donate Login Register  
Pages: [1]
Author Topic: How do you calculate probability?  (Read 3346 times)
Hero Member
Offline Offline

Activity: 574
Merit: 500

View Profile
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:
//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>";
//document.getElementById('answer').innerHTML = txt;
//function getRow(prob,p) { return "<tr><td>"+100*prob+"%</td><td>"+getHumanTime(-Math.log(1-prob)/p)+"</td></tr>\n";

$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 "";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
$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 {
$pph = bcmul($pph,1);
echo "ProbabilityPerHash($pph)";
} where r is your khash/sec
Hero Member
Offline Offline

Posts: 1529642118

View Profile Personal Message (Offline)

Reply with quote  #2

Report to moderator
Automated Bitcoin Fork Extraction Tool WE DO TOUGH WALLETS: BCH | BTG | BCD | SBTC | UBTC | B2X | BCX | BTF Electrum 2FA, Trezor, Ledger, SegWit, Bech32
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
Hero Member
Offline Offline

Posts: 1529642118

View Profile Personal Message (Offline)

Reply with quote  #2

Report to moderator
Offline Offline

Activity: 103
Merit: 10

View Profile
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):


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



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]
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!