
%mathpiper

Use("org/mathpiper/assembledscripts/proposed.rep/statistics.mpi");
Use("org/mathpiper/assembledscripts/proposed.rep/geogebra.mpi");

%/mathpiper

    %output,preserve="false"
      Result: True
.   %/output






%mathpiper,title="two boys."

boysAndGirls := {};
ForEach(trial, 1 .. 100)
[
    child1 := Round(Random());
    child2 := Round(Random());
    
    boysAndGirls := Append( boysAndGirls, {child1, child2} );
];

boysOnly := Remove(boysAndGirls,{0,0});

Echo("Trials :", boysOnly);

oneBoy := Count(boysOnly, {1,0}) + Count(boysOnly, {0,1});

twoBoys := Count(boysOnly, {1,1});

Echo("One boy: ", oneBoy);

Echo("Two boys: ", twoBoys);

Echo("At least one boy: ", Length(boysOnly),N( (oneBoy + twoBoys)/Length(boysAndGirls)));


N(twoBoys/ Length(boysOnly));


%/mathpiper

    %output,preserve="false"
      Result: 0.2564102564
      
      Side Effects:
      Trials :{{1,1},{0,1},{1,1},{0,1},{0,1},{0,1},{0,1},{1,1},{1,1},{1,1},{0,1},{1,1},{0,1},{0,1},{1,0},{1,1},{1,1},{1,0},{0,1},{0,1},{1,0},{1,0},{1,0},{1,0},{0,1},{0,1},{1,1},{1,1},{1,0},{1,0},{1,1},{1,0},{1,1},{1,0},{0,1},{0,1},{1,0},{0,1},{1,0},{0,1},{0,1},{1,0},{1,1},{0,1},{0,1},{1,0},{0,1},{0,1},{1,1},{0,1},{1,0},{0,1},{0,1},{1,0},{1,0},{0,1},{0,1},{1,0},{0,1},{0,1},{0,1},{1,0},{1,1},{1,0},{1,0},{1,0},{0,1},{1,0},{1,1},{1,0},{1,1},{0,1},{1,0},{0,1},{1,1},{1,1},{1,1},{1,0}} 
      One boy: 58 
      Two boys: 20 
      At least one boy: 78 0.78
.   %/output








%mathpiper,title="Gold example on pp.82 chapter 6."

ships := {{gold, gold}, {gold, silver}, {silver, silver}};

score := {};


Repeat(1000)
[
    ship := RandomPick(ships);
    
    ship := Shuffle(ship);
    
    If(Count(ship,gold) = 2, score := Append(score,success) );
    
     If(Count(ship,silver) = 1,
            If(ship[1] = gold, score := Append(score,failure)) 
     );
    
    

];
initialGoldCount := Length(score);
goldGoldCount := Count(score,success);

result := N(goldGoldCount/initialGoldCount);

Echo(initialGoldCount,,,goldGoldCount,,,result);

%/mathpiper










%mathpiper,title="Three door problem pp.83 chapter 6."

firstPickScore := {};
secondPickScore := {};


Repeat(1000)
[
    doors := Shuffle( {empty, prize, empty} );
    
    firstPick := doors[1];
    
    If(doors[2] = empty, secondPick := doors[3], secondPick := doors[2]);
    
    firstPickScore := Append(firstPickScore, firstPick);
    secondPickScore := Append(secondPickScore, secondPick);
];

Echo("First Pick: ", Count(firstPickScore,prize));
Echo("Second Pick: ", Count(secondPickScore,prize));


%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      First Pick: 263 
      Second Pick: 737
.   %/output









%mathpiper,title="Two of a kind problem pp.85 chapter 6."

pairsCount := 0;

deck := Concat(1 .. 13, 1 .. 13, 1 .. 13, 1 .. 13);

trials := 100;

Repeat(trials)
[
    deck := Shuffle(deck);
    
    hand := Take(deck,5);
    
    //Echo(hand);
    
    handPairCount := 0;
    ForEach(card,1 .. 13)
    [
        If(Count(hand,card) = 2, handPairCount := handPairCount + 1);
    ];
    
    If(handPairCount = 1, pairsCount := pairsCount + 1);

];


Echo("Probability of a single pair: ", N(pairsCount/trials) );

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      Probability of a single pair: 0.52
.   %/output








%mathpiper,title="Two pairs vs. three of a kind problem pp.90 chapter 6."

pairsCount := 0;
threeOfAKindCount := 0;

deck := Concat(1 .. 13, 1 .. 13, 1 .. 13, 1 .. 13);

trials := 1000;

Repeat(trials)
[

    deck := Shuffle(deck);

    hand := Take(deck,5);
    
    //Echo(hand);
    
    handPairCount := 0;
    handThreeOfAKindCount := 0;
    ForEach(card,1 .. 13)
    
    [
        If(Count(hand,card) = 2, handPairCount := handPairCount + 1);
        If(Count(hand,card) = 3, handThreeOfAKindCount := handThreeOfAKindCount + 1);
    ];
    
    If(handPairCount = 2, pairsCount := pairsCount + 1);
    If(handThreeOfAKindCount = 1, threeOfAKindCount := threeOfAKindCount + 1);

];

Echo("Probability of two pairs: ", N(pairsCount/trials) );
Echo("Probability of three of a kind: ", N(threeOfAKindCount/trials) );

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      Probability of two pairs: 0.052 
      Probability of three of a kind: 0.028
.   %/output









%mathpiper,title="Birthday match from page 97 chapter 7."

birthdayMatchCounter := 0;
trials := 50;
Repeat(trials)
[
    birthdays := RandomIntegerVector(25,1,365);

    dayCounter := 1;
    While(dayCounter <= 365)
    [
        If(Count(birthdays,dayCounter) >= 2, [birthdayMatchCounter := birthdayMatchCounter + 1; dayCounter := 366;]);
        dayCounter := dayCounter + 1;
    ];

];
    
    
    Echo(birthdayMatchCounter,,,trials);

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      31 , 50
.   %/output








%mathpiper,title="Birthday the same as August 1st"

birthdayMatchCounter := 0;

trials := 1000;
Repeat(trials)
[
    targetPersonBirthday := 213;
    
    birthdays := RandomIntegerVector(25,1,365);

    If( Contains(birthdays, targetPersonBirthday), birthdayMatchCounter++);

];
    
    
    Echo(birthdayMatchCounter,,,trials,,,N(birthdayMatchCounter/trials));

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      81 , 1000 , 0.081
.   %/output




%mathpiper,title="Three daughters example pp.97 chapter 7."

trials := 100;

successes := 0;

Repeat(trials)
[
    sample := RandomSymbolVector( {{boy,1/2}, {girl,1/2} },4);
    //Echo(sample);
    
    If(Count(sample,girl) = 3, successes := successes + 1);
    
];

Echo(successes,,,trials);

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      27 , 100
.   %/output







%mathpiper,title="5 shot basketball example pp.102 chapter 7."

trials := 100;

successes := 0;

Repeat(trials)
[
    sample := RandomSymbolVector({{hit,1/4}, {miss,3/4}},5);
    
    If(Count(sample,hit) >= 3, successes := successes + 1);
    
];

Echo(successes,,,trials);

%/mathpiper








%mathpiper,title="Archery example pp.104 chapter 7."

trials := 100;

successes := 0;

Repeat(trials)
[
    sample := RandomSymbolVector({{black,10/100}, {white,60/100}, {miss,30/100}},3);
    
    If(Count(sample,black)= 1 And Count(sample,white) = 2, successes := successes + 1);
    
];

Echo(successes,,,trials);

%/mathpiper









%mathpiper,title="Sum of hammers and handle lengths pp. 108"

sample := {};
trials := 100;
Repeat(trials)
[
    handleLength := RandomSymbol({{10.0,20/100},{10.1,30/100},{10.2,30/100},{10.3,20/100}});
    headLength := RandomSymbol({{2.0,20/100},{2.1,20/100},{2.2,30/100},{2.3,20/100},{2.4,10/100}});
    
    totalLength := handleLength + headLength;
    
    sample := Append(sample, totalLength);
    
];


givenLength := 12.4;
overOrEqual := 0;
ForEach(element, sample)
[
    If(element >= givenLength, overOrEqual := overOrEqual + 1);

];

mean := Mean(sample);

Echo(overOrEqual,,,trials,,,mean);

%/mathpiper







%mathpiper,title="Flipping pennies pp.110 chapter 7."

trials := 2;
emptyCount := 0;

Repeat(trials)
[
    stack1 := 10;
    stack2 := 10;
    
    iterations := Repeat(200)
    [
        flip := RandomSymbol({{head,1/2},{tail,1/2}});
        
        If(flip = head, [stack1++; stack2--;], [stack1--; stack2++;] );
        
        If(stack1 = 0 Or stack2 = 0, Break() );
        
    ];
    
    Echo({stack1,,,stack2,,,iterations});
    If(stack1 = 0 Or stack2 = 0, emptyCount++);
    
];

{emptyCount,trials};


%/mathpiper






%mathpiper,title="capacirators example pp. 112 chapter 7."

daysToEmptyList := {};
trials := 50;
Repeat(trials)
[
    warehouseCount := 12;
    
    dayCounter := 0;
    
    daysToEmpty := Repeat()
    [
        morning := RandomSymbol({{used,6/10},{not_used,4/10}});
        If(morning = used, warehouseCount--);
        If(warehouseCount = 0, Break());
        
        afternoon := RandomSymbol({{used,6/10},{not_used,4/10}});
        If(afternoon = used, warehouseCount--);
        If(warehouseCount = 0, Break());
        
        
        If(Mod(dayCounter,3) = 0, warehouseCount := warehouseCount + 2);
        
        //Echo(warehouseCount, dayCounter, Mod(dayCounter,3));
        
        dayCounter++;
    
    ];
    
    daysToEmptyList := Append(daysToEmptyList, daysToEmpty);
    
];

Echo("Average days to empty: ", Mean(daysToEmptyList));


%/mathpiper







%mathpiper,title="Random walk example at the end of chapter 7."

targetPositions := {{3,2}, {-1,-4}};

successCount := 0;

trials := 500;
Repeat(trials)
[
    currentPosition := {0,0};
    walkPath := {};
    Repeat(12)
    [
        step:= RandomSymbol({{{1,0},1/4},{{-1,0},1/4},{{0,1},1/4},{{0,-1},1/4}});
        
        currentPosition := currentPosition + step;
        
        //walkPath := Append(walkPath,currentPosition);
        
        If(Contains(targetPositions, currentPosition), [successCount++; Break();]);
        
    ];
    //Write(walkPath);
];

{successCount, trials, N(successCount/trials)};
%/mathpiper

    %output,preserve="false"
      Result: {35,500,0.07}
.   %/output




%mathpiper,title="case 1 example pp. 119 chapter 8."

balls := {1,2,3,4,5,6};

successCount := 0;

trials := 1000;
Repeat(trials)
[
    sample := Sample(balls,3);
    
    If(sample = {1,2,3}, successCount++);
];

Echo(successCount,,,trials,,,N(successCount/trials));

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      36 , 5000 , 0.0072
.   %/output




%mathpiper,title="case 2 example pp. 120 chapter 8."

balls := {1,2,3,4,5,6};

successCount := 0;

trials := 1000;
Repeat(trials)
[
    sample := Sample(balls,3);
    
    If(IsSubset({1,2,3},sample), successCount++);
];

Echo(successCount,,,trials,,,N(successCount/trials));

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      49 , 1000 , 0.049
.   %/output






%mathpiper,title="case 3 example pp. 121 chapter 8."

balls := {1,2,3,4,5,6};

successCount := 0;

trials := 1000;
Repeat(trials)
[
    sample := Sample(balls,3);
    
    If( (IsOdd(sample[1]) And IsEven(sample[2])) Or (IsOdd(sample[2]) And IsEven(sample[3])), successCount++);
];

Echo(successCount,,,trials,,,N(successCount/trials));

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      588 , 1000 , 0.588
.   %/output






%mathpiper,title="case 4 example pp. 120 chapter 8."

balls := {1,2,3,4,5,6};

successCount := 0;

trials := 1000;
Repeat(trials)
[
    sample := Sample(balls,3);
    
    If(Length(Select("IsOdd",sample)) = 2, successCount++);
];

Echo(successCount,,,trials,,,N(successCount/trials));

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      423 , 1000 , 0.423
.   %/output





%mathpiper,title="case 5a example pp. 121 chapter 8."

balls := {1,2,3,4,5,6};

successCount := 0;

trials := 1000;
Repeat(trials)
[
    sample := Sample(balls,3);
    
    If(sample[1] = 1 Or sample[2] = 2 Or sample[3] = 3, successCount++);
];

Echo(successCount,,,trials,,,N(successCount/trials));

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      421 , 1000 , 0.421
.   %/output




%mathpiper,title="case 5b example pp. 121 chapter 8."

balls := {1,2,3,4,5,6};

successCount := 0;

trials := 10000;
Repeat(trials)
[
    sample := Sample(balls,6);
    
    If(sample = {1,2,3,4,5,6}, successCount++);
];

Echo(successCount,,,trials,,,N(successCount/trials));

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      19 , 10000 , 0.0019
.   %/output








%mathpiper,title="example 8-1 50 girls and boys pp. 122."

class := Concat(FillList(boy,25), FillList(girl,25));

samples := {};

trials := 100;

Repeat(trials)
[
    class := Shuffle(class);
    
    sample := Take(class,5);
    
    samples := Count(sample,girl) : samples;
    
];

successCount := Count(samples,4);

Echo(successCount,,,trials,,,N(successCount/trials));

%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side Effects:
      14 , 100 , 0.14
.   %/output


%mathpiper,title="Extra code for example 8-1"
GeoGebraHistogram(samples);

%/mathpiper

    %output,preserve="false"
      Result: true
.   %/output






%mathpiper,title="Example 8-2 9 spades pp. 125"

samples := {};

trials := 100;

deck := Concat(FillList(spades,13), FillList(diamonds,13), FillList(hearts,13), FillList(clubs,13));
//test := {};
//counter := 0;
Repeat(trials)
[

    (deck := Shuffle(deck));
    
    (hand := Take(deck,13));
    
    //test := Count(hand, spades) : test;
    
    (If(Count(hand, spades) = 9, Count(hand, clubs) : samples));
    
    //If(Mod(counter,100) = 0, SysOut("Trial # ",,, counter));
    //counter++;
];

samples;

%/mathpiper

    %output,preserve="false"
      Result: {}
.   %/output




%mathpiper,title="Extra code for example 8-2"
//The probability of obtaining 9 spades seems to be very low.

N( (Combinations(13,9) * Combinations(39,4)) / Combinations(52,13) );

%/mathpiper

    %output,preserve="false"
      Result: 0.00009261135311
.   %/output



%mathpiper,title="Extra code for example 8-2"
GeoGebraHistogram(test);

%/mathpiper

    %output,preserve="false"
      Result: true
.   %/output


