How to resolve the algorithm Percolation/Mean run density step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Percolation/Mean run density step by step in the C programming language

Table of Contents

Problem Statement

Let

v

{\displaystyle v}

be a vector of

n

{\displaystyle n}

values of either 1 or 0 where the probability of any value being 1 is

p

{\displaystyle p}

; the probability of a value being 0 is therefore

1 − p

{\displaystyle 1-p}

. Define a run of 1s as being a group of consecutive 1s in the vector bounded either by the limits of the vector or by a 0. Let the number of such runs in a given vector of length

n

{\displaystyle n}

be

R

n

{\displaystyle R_{n}}

. For example, the following vector has

R

10

= 3

{\displaystyle R_{10}=3}

Percolation theory states that Any calculation of

R

n

/

n

{\displaystyle R_{n}/n}

for finite

n

{\displaystyle n}

is subject to randomness so should be computed as the average of

t

{\displaystyle t}

runs, where

t ≥ 100

{\displaystyle t\geq 100}

. For values of

p

{\displaystyle p}

of 0.1, 0.3, 0.5, 0.7, and 0.9, show the effect of varying

n

{\displaystyle n}

on the accuracy of simulated

K ( p )

{\displaystyle K(p)}

. Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Percolation/Mean run density step by step in the C programming language

The C program is designed to estimate the binomial probability p(1 - p) and compare it to the observed probability K obtained from a series of coin flips (represented by a random number generator). It runs multiple tests with varying probabilities and sample sizes to analyze the deviations from the expected binomial distribution.

  • Function run_test:

    • Inputs: probability p, sequence length len, number of runs runs.
    • Generates a sequence of len random 0s and 1s without storing them. Each element is generated by comparing a random number to a threshold thresh, calculated as p * RAND_MAX.
    • Counts the number of transitions from 0 to 1 (cnt) during the runs.
    • Returns the observed probability K as the ratio of transitions to the total number of elements and runs.
  • Main Function:

    • Sets up parameters for testing:
      • Varying probabilities (p) from 0.1 to 0.9.
      • Varying sequence lengths (n) from 100 to 100000.
    • Loops through these parameters and performs the following for each combination:
      • Calls run_test to obtain the observed probability K.
      • Calculates the expected binomial probability p(1 - p) (p1p).
      • Prints out p, n, K, p1p, and the difference between K and p1p, along with the percentage difference.
  • Output:

    • A table displaying the results for each combination of p and n.
    • The table shows the probability, sequence length, observed probability, expected binomial probability, the difference between the two, and the percentage difference.

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>

// just generate 0s and 1s without storing them
double run_test(double p, int len, int runs)
{
	int r, x, y, i, cnt = 0, thresh = p * RAND_MAX;

	for (r = 0; r < runs; r++)
		for (x = 0, i = len; i--; x = y)
			cnt += x < (y = rand() < thresh);

	return (double)cnt / runs / len;
}

int main(void)
{
	double p, p1p, K;
	int ip, n;

	puts(	"running 1000 tests each:\n"
		" p\t   n\tK\tp(1-p)\t     diff\n"
		"-----------------------------------------------");
	for (ip = 1; ip < 10; ip += 2) {
		p = ip / 10., p1p = p * (1 - p);

		for (n = 100; n <= 100000; n *= 10) {
			K = run_test(p, n, 1000);
			printf("%.1f\t%6d\t%.4f\t%.4f\t%+.4f (%+.2f%%)\n",
				p, n, K, p1p, K - p1p, (K - p1p) / p1p * 100);
		}
		putchar('\n');
	}

	return 0;
}


  

You may also check:How to resolve the algorithm 21 game step by step in the Pascal programming language
You may also check:How to resolve the algorithm Create a file step by step in the Clojure programming language
You may also check:How to resolve the algorithm Monty Hall problem step by step in the Swift programming language
You may also check:How to resolve the algorithm Dragon curve step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the Prolog programming language