Transcript
Page 1: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

Principals and Practice of Cryptocurrencies

Cornell CS 5437, Spring 2016

Simulation

Page 2: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

2

Example – Gossip

β€’ A messages is generated at one machine

β€’ Message processing time: random 0 - 1sec

β€’ After processing, node chooses random neighbor and sends message

β€’ Sending time: random 0 – 1sec

Page 3: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

3

Why A Simulation?

Formal analysis

Working system

experiment

Simulation Proof of concept

experiment

Page 4: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

4

Why A Simulation?

Formal analysis

Working system experiment

Simulation Proof of concept experiment

β€’ How complex a model? β€’ Over-detailed --> overly complicated β€’ Insufficient details --> inaccurate (or wrong)

Formal Analysis Simulation Experiment

Simple models Complex models Real world

Basic understanding, parameter interaction

Per-run results –cheap runs

Per-run results –expensive runs

Scale to arbitrary system parameters

Cheap scaling Expensive scaling

Page 5: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

5

Event Driven Simulation

Time-driven simulation: second by second

Event-driven simulation: event by event

Which is more accurate?

Page 6: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

6

Time Event

1 Message π‘š arrived at node 𝑖

1.3 Message π‘š arrived at node 𝑗

Event Driven Simulation

Time-driven simulation: second by second

Event-driven simulation: event by event

No tradeoff between efficiency

and accuracy

Page 7: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

7

Process-Oriented Simulation

β€’ Use an object per entity β€’ Store individual object state, and let object react to events

Time Entity Event

1 node 𝑖 Message π‘š arrived

1.3 node 𝑗 Message π‘š arrived

Page 8: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

8

InputIncludes both model details and runtime inputs

β€’ Synthetic, e.g., β€’ Topology of huge system β€’ Input arrival times

β€’ Traces, measurement-based, e.g., β€’ Input arrival times β€’ Processing times

Page 9: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

9

Output Data collection: β€’ Output as much data as possible (probably in a log), but not

too much – it can easily explode. For example: β€’ Message arrival time at each node:

Can then calculate 90th percentile propagation time without re-running

β€’ But not every send / processing-start event

Tips: β€’ Output meaningful data during long runs β€’ Output execution parameters in log

Page 10: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

10

Executions Multiple executions β€’ Each with different random inputs β€’ Warmup

β€’ Do not consider for statistics β€’ E.g., when multiple messages are propagated together,

let queues stabilize β€’ Or a single long run, divided to sections

Page 11: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

11

Restart and Avoiding It

β€’ Memoizeβ€’ Carefully while you’re debugging…

β€’ Checkpoint β€’ For crash handlingβ€’ If you decide continue, to avoid restart

Page 12: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

Probability

Page 13: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

13

Probability

β€’ An experiment produces a random results, πœ”β€’ The sample space Ξ© is all possible results, πœ” ∈ Ξ©β€’ An Event is a subset of the sample space, 𝐸 βŠ‚ Ξ©

Page 14: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

15

Probability β€’ Every event 𝐸 has a probability 0 ≀ 𝑃 𝐸 ≀ 1β€’ The conditional probability of A given B is the probability of A given

the B is true; 𝑃 𝐴 𝐡 =𝑃 𝐴∩𝐡

𝑃 𝐡

β€’ A random variable is a function from the sample space to a discrete or continuous range

β€’ A random variable has a Cumulative Distribution Function (CDF): 𝐹𝑋 π‘₯ = 𝑃 𝑋 ≀ π‘₯

𝐹𝑋 π‘₯π‘₯β†’βˆ’βˆž

0, 𝐹𝑋 π‘₯π‘₯β†’βˆž

1β€’ A discrete random variable has a probability per value β€’ A continuous random variable has a probability distribution function

𝑓𝑋 π‘₯ = 𝐹𝑋′ π‘₯

Page 15: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

16

Probability β€’ The mean of a variable 𝑋 is

𝐸 𝑋 = βˆ’βˆž

∞

π‘₯𝑓𝑋 π‘₯ 𝑑π‘₯

β€’ The variance of a variable 𝑋 is var 𝑋 = 𝐸 𝑋 βˆ’ 𝐸 𝑋 2 = 𝐸 𝑋2 βˆ’ 𝐸 𝑋 2

Page 16: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

17

Example – Bernoulli distribution

β€’ Bernoulli distribution: E.g., due to a biased coin toss, resulting in heads (πœ”1) or tails (πœ”2)

𝑋 = 1 heads0 tails

𝑃 𝑋 = 1 = 𝑝, 𝑃 𝑋 = 0 = 1 βˆ’ 𝑝𝐸 𝑋 = 1 β‹… 𝑝 + 0 β‹… 1 βˆ’ 𝑝 = 𝑝var 𝑋 = 𝑝 1 βˆ’ 𝑝

Page 17: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

18

Example – Normal Distribution E.g., height, measurement errors

𝑁(πœ‡, 𝜎2)

𝑓 π‘₯ =1

𝜎 2πœ‹π‘’βˆ’π‘₯βˆ’πœ‡ 2

2𝜎2

𝐸 𝑋 = πœ‡var 𝑋 = 𝜎2

The standard normal distribution is 𝑁(0, 1).

Page 18: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

19

Example – Normal Distribution The law of large numbers: The average of Independent and Identically Distributed (IID) random variables converges to the mean of the distribution they are sampled from.

The central limit theorem: the average of IID random variables is approximately normally distributed.

Page 19: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

20

Example – Exponential Distribution

E.g., interval between phone calls

𝑓 π‘₯ = πœ†π‘’βˆ’πœ†π‘₯ π‘₯ β‰₯ 00 otherwise

𝐹 π‘₯ = 1 βˆ’ π‘’βˆ’πœ†π‘₯ π‘₯ β‰₯ 00 otherwise

𝐸 𝑋 = 1/πœ†var 𝑋 = 1/πœ†2

Page 20: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

21

Memorylessness

𝑷 𝑿 > 𝒔 + 𝒕|𝑿 > 𝒕 =𝑃 𝑋 > 𝑠 + 𝑑 ∩ 𝑋 > 𝑑

𝑃 𝑋 > 𝑑

=𝑃 𝑋 > 𝑠 + 𝑑

𝑃 𝑋 > 𝑑=1 βˆ’ 𝐹(𝑠 + 𝑑)

1 βˆ’ 𝐹(𝑑)=π‘’βˆ’πœ† 𝑠+𝑑

π‘’βˆ’πœ†π‘‘

= π‘’βˆ’πœ†π‘  = 1 βˆ’ 𝐹 𝑠 = 𝑷 𝑿 > 𝒔

For an exponential random variable, 𝐹 π‘₯ = 1 βˆ’ π‘’βˆ’πœ†π‘₯:

It doesn’t matter how long you have been waiting for the bus

Page 21: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

22

Psuedo Random Number Generator

β€’ No real randomness β€’ PRNG has state and output

β€’ State changes on every output request (loops, but usually not an issue)

β€’ Can be reset with given seed

1 >>> import random

2 >>> random.random()

3 0.28651040107085957

4 >>> random.random()

5 0.1796791064694051

6 >>> random.seed(42)

7 >>> random.random()

8 0.6394267984578837

9 >>> random.random()

10 0.02501075522266693

11 >>> random.seed(42)

12 >>> random.random()

13 0.6394267984578837

14 >>> random.random()

15 0.02501075522266693

Page 22: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

23

Use Object PRNG

1 >>> randA = random.Random(42)

2 >>> randB = random.Random(42)

3 >>> randA.random()

4 0.6394267984578837

5 >>> randB.random()

6 0.6394267984578837

7 >>> randA.random()

8 0.025010755222666936

9 >>> randB.random()

10 0.025010755222666936

β€’ Program modular β€’ Reset global PRNG in

each module? β€’ No – object PRNG per

module β€’ Seed module’s PRNG

on init

Page 23: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

24

PRNG for Simulation

β€’ Different seeds for different runs β€’ Reproducibility (mostly for debugging)

β€’ Manually change between runs β€’ Seed time, but record seed for reproduction

Page 24: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

25

Inverse Transform Sampling

0.5

3

1

PDF

CDF

CDF-1

1

3𝑓 𝑑 = 0.5 1 ≀ 𝑑 < 30 otherwise

𝐹 𝑑 = 0 𝑑 < 1

0.5(𝑑 βˆ’ 1) 1 ≀ 𝑑 < 31 𝑑 β‰₯ 3

πΉβˆ’1 𝑒 = 1 + 2𝑒

>> randA = random.Random()

>> x = 1 + 2 * randA.random()

Page 25: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

26

Statistical Analysis

Given a finite set of measurements

β€’ Estimate the properties of the sampled space β€’ Estimate the estimation accuracy

Stop when the accuracy is sufficient.

Page 26: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

27

Statistical Analysis Take a sample of size 𝑛 {π‘₯_𝑖, 1 ≀ 𝑖 ≀ 𝑛} of independent measurements. E.g., simulation propagation times from 𝑛 runs.

Sample are taken from a population with probability distribution with mean πœ‡ and variance 𝜎2. (πœ‡ and 𝜎 are unknown)

β€’ The sample mean is:

π‘₯ =1

𝑛

𝑖=1

𝑛

π‘₯𝑖

β€’ The sample variance is:

𝑆2 =1

𝑛 βˆ’ 1

𝑖=1

𝑛

π‘₯𝑖 βˆ’ π‘₯ 2

Page 27: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

28

Statistical Analysis But the sample mean is a random variable.

The mean of π‘₯ is πœ‡.

So π‘₯ is an estimator of πœ‡

β€’ π‘₯ is not πœ‡, and β€’ 𝑆 is not 𝜎

Page 28: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

30

Statistical Analysis What is the variance of π‘₯?

Page 29: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

33

Statistical Analysis What is the variance of π‘₯?

var π‘₯ =𝜎2

𝑛

More samples --> smaller variance --> π‘₯ probably closer to πœ‡

But we can only take a finite number of samples 𝑛. And we don’t have 𝜎, only 𝑆. So we need to estimate 𝜎2.

Page 30: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

34

Statistical Analysis What is the mean of 𝑆2?

Page 31: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

35

Statistical Analysis

𝑆2 =1

𝑛 βˆ’ 1

𝑛=1

𝑛

π‘₯𝑖 βˆ’ π‘₯ 2 =1

𝑛 βˆ’ 1

𝑛=1

𝑛

π‘₯𝑖 βˆ’1

𝑛

𝑗

π‘₯𝑗

2

=

=1

𝑛 βˆ’ 1

𝑛=1

𝑛

(π‘₯π‘–βˆ’πœ‡) βˆ’1

𝑛

𝑗

(π‘₯π‘—βˆ’πœ‡)

2

=

=1

𝑛 βˆ’ 1

𝑖

π‘₯𝑖 βˆ’ πœ‡ 2 +1

𝑛2

𝑗

(π‘₯π‘—βˆ’πœ‡)

2

βˆ’2

𝑛(π‘₯π‘–βˆ’πœ‡)

𝑗

(π‘₯π‘—βˆ’πœ‡) =

=1

𝑛 βˆ’ 1

𝑖

π‘₯𝑖 βˆ’ πœ‡ 2 +1

𝑛(𝑛 βˆ’ 1)

𝑗

(π‘₯π‘—βˆ’πœ‡)

2

βˆ’2

𝑛 𝑛 βˆ’ 1

𝑖

(π‘₯π‘–βˆ’πœ‡)

𝑗

(π‘₯π‘—βˆ’πœ‡) =

Page 32: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

36

Statistical Analysis

1

𝑛 βˆ’ 1

𝑖

π‘₯𝑖 βˆ’ πœ‡ 2 +1

𝑛(𝑛 βˆ’ 1)

𝑗

(π‘₯π‘—βˆ’πœ‡)

2

βˆ’2

𝑛 𝑛 βˆ’ 1

𝑖

(π‘₯π‘–βˆ’πœ‡)

𝑗

(π‘₯π‘—βˆ’πœ‡) =

=1

𝑛 βˆ’ 1

𝑖

π‘₯𝑖 βˆ’ πœ‡ 2 βˆ’1

𝑛(𝑛 βˆ’ 1)

𝑖

π‘₯𝑖 βˆ’ πœ‡ 2 +

𝑖

𝑗≠𝑖

(π‘₯𝑖 βˆ’ πœ‡)(π‘₯𝑗 βˆ’ πœ‡) =

=1

𝑛

𝑖

π‘₯𝑖 βˆ’ πœ‡ 2 βˆ’1

𝑛(𝑛 βˆ’ 1)

𝑖

𝑗≠𝑖

(π‘₯𝑖 βˆ’ πœ‡)(π‘₯𝑗 βˆ’ πœ‡)

𝐸 𝑆2 =1

𝑛

𝑖

𝐸 π‘₯𝑖 βˆ’ πœ‡ 2 βˆ’1

𝑛 𝑛 βˆ’ 1

𝑖

𝑗≠𝑖

𝐸 π‘₯𝑖 βˆ’ πœ‡ π‘₯𝑗 βˆ’ πœ‡ = 𝜎2

Page 33: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

37

Statistical Analysis What is the mean of 𝑆2? 𝜎2.

Page 34: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

38

Confidence Interval With the standard normal distribution 𝑁(0, 1), define 𝑧𝛼/2 to be the

point for which the integral to the right is 𝛼/2. (tables online)

𝑃 βˆ’z𝛼/2 ≀ 𝑧 ≀ 𝑧𝛼/2 = 1 βˆ’ 𝛼

0 𝑧𝛼/2

Area is 𝛼/2

For example, for 𝛼 = 0.05, 𝑧0.025 = 1.96

Page 35: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

39

ConfidenceSo how accurate is the estimate π‘₯? If the π‘₯𝑖’s are normally distributed: π‘₯𝑖 = 𝑁(πœ‡, 𝜎), let

𝑍 = π‘₯ βˆ’ πœ‡

πœŽπ‘›

𝑍 is normally distributed 𝑍 = 𝑁 0, 1 . So

𝑃 βˆ’π‘§π›Ό/2 ≀ 𝑍 = π‘₯ βˆ’ πœ‡

πœŽπ‘› ≀ 𝑧𝛼/2 = 1 βˆ’ 𝛼

𝑃 π‘₯ βˆ’z𝛼/2𝜎

𝑛≀ πœ‡ ≀ π‘₯ +

z𝛼/2𝜎

𝑛= 1 βˆ’ 𝛼

For confidence level 1 βˆ’ 𝛼,

the confidence interval is π‘₯ βˆ’z𝛼/2𝜎

𝑛, π‘₯ +

z𝛼/2𝜎

𝑛

Page 36: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

40

Confidence Interval β€’ Since we don’t have 𝜎, we approximate with S. β€’ Although the π‘₯𝑖’s are not necessarily normally distributed, according

to the central limit theorem, it’s a good approximation for their sum. Thumb rule: 𝑛 β‰₯ 30.

Page 37: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

41

Example

Index Value

1 1.9

2 2.0

3 1.9

4 2.0

5 2.1

6 1.9

7 2.1

8 2.1

9 2.1

10 2.1

π‘₯ =1

𝑛

𝑖=1

𝑛

π‘₯𝑖 = 2.05

𝑆2 =1

𝑛 βˆ’ 1

𝑖=1

𝑛

π‘₯𝑖 βˆ’ π‘₯ 2 = 0.009

𝑧0.05/2 = 1.96

π‘₯ Β± 𝑧0.05/2𝑆

𝑛= 1.99,2.11

Page 38: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

42

Statistical Analysis

Given a finite set of measurements

β€’ Estimate the properties of the sampled space β€’ Estimate the estimation accuracy

Stop when the accuracy is sufficient. E.g., the confidence interval is of length 0.01sec with a confidence level of 95%.

Page 39: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

Difficulty and Block Interval

Page 40: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

44

Bitcoin’s Block Difficulty

β€’ Hash (SHA2562) of legal block is smaller than a target β€’ Target is a 256bit value β€’ Stored as a 32bit field called bits in blocks

0x180BC409 0BC409 β‹… 28 18βˆ’3

Bits: Target

Page 41: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

45

Bitcoin’s Block Difficulty

β€’ Hash (SHA2562) of legal block is smaller than a target β€’ Target is a 256bit value β€’ Stored as a 32bit field called bits in blocks β€’ Largest target (targetmax) is defined by bits 0x1d00ffff:

0x00000000FFFF0000000000000000000000000000000000000000000000000000

β€’ Difficulty is defined with respect to the largest target:

difficulty =largest target

target

Page 42: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

46

Bitcoin’s Block Difficulty

How long does it take to find a value smaller than 0x00000000FFFF0000000000000000000000000000000000000000000000000000

Or simply: 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

?

But wait – the nonce field size…

Page 43: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

47

Bitcoin’s Block DifficultyHow is the target adjusted?

β€’ Once every 2016 blocks (2016/7/24/6 = 2 weeks) β€’ 2016 blocks ago was 𝑑0β€’ Last block was 𝑑𝑓‒ Total time is Ξ” = 𝑑𝑓 βˆ’ 𝑑0 seconds

β€’ Difficulty before π·π‘œπ‘™π‘‘β€’ Estimate of total hashes calculated: 2016 Γ— 232π·π‘œπ‘™π‘‘

We want to find 𝐷𝑛𝑒𝑀 such that it will take the system 10 minutes on average to find a block. HW

Page 44: Principals and Practice of Cryptocurrencies simulation.pdfΒ Β· S 5437 al, 2016 15 Probability β€’ Every event has a probability 0 Q𝑃 Q1 β€’ The conditional probability of A given

Co

rnel

l CS

54

37

Β© It

tay

Eyal

, 20

16

50

Agenda

β€’ Difficulty calculation β€’ Time to find a block β€’ Difficulty automatic tuning β€’ Minimum of two exponentials and fair mining


Top Related