# Khan/khan-exercises

Fetching contributors…
Cannot retrieve contributors at this time
541 lines (477 sloc) 30.2 KB
 Empirical rule
animal( 1 ) plural( animal( 1 ) )
roundTo( 1, animalAvgLifespan( 1 ) * ( randRange( 80, 120 ) / 100 ) ) roundTo( 1, animalStddevLifespan( 1 ) * ( randRange( 20, 120 ) / 100 ) )
randRangeNonZero( -3, 3 ) { "-3": 99.7, "-2": 95, "-1": 68, "1": 68, "2": 95, "3": 99.7 }[ Z ] { "-3": 99.85, "-2": 97.5, "-1": 84, "1": 16, "2": 2.5, "3": 0.15 }[ Z ]
The lifespans of ANIMALS in a particular zoo are normally distributed. The average ANIMAL lives MEAN years; the standard deviation is STDDEV years.

Estimate the probability of an( ANIMAL ) living longer than roundTo( 1, MEAN + STDDEV * Z ) years.

We can try to estimate using the empirical rule, also known as the 68-95-99.7 percent rule.

init({ range: [ [ MEAN - STDDEV * 3.5, MEAN + STDDEV * 3.5 ], [ -1.5, 4.3 ] ], scale: [ 475 / ( STDDEV * 7 ), 40 ] }); style({ stroke: "#bbb" }, function() { line( [ MEAN - STDDEV * 4, 0 ], [ MEAN + STDDEV * 4, 0 ] ); }); graph.pdf = function( x ) { return gaussianPDF( MEAN, STDDEV, x ) * 4 / gaussianPDF( MEAN, STDDEV, MEAN ) + 0.2; }; style({ stroke: BLUE }, function() { plot( graph.pdf, [ MEAN - STDDEV * 3.5, MEAN + STDDEV * 3.5 ]); }); style({ stroke: PINK }, function() { graph.meanLine = line( [ MEAN, 0 ], [ MEAN, graph.pdf( MEAN ) ] ).toBack(); }); graph.meanLabel = label( [ MEAN, 0 ], MEAN, "below", { color: PINK } ); graph.zLine = []; graph.zLabel = [];

We know the lifespans are normally distributed with an average lifespan of MEAN years.

graph.meanLine.attr({ stroke: "#bbb" }); graph.meanLabel.css({ color: "#bbb" }); \$( "#meanHint" ).parent().removeClass( "hint_pink" ); style({ stroke: PINK }, function() { graph.zLine[ -1 ] = line( [ MEAN - STDDEV, 0 ], [ MEAN - STDDEV, graph.pdf( MEAN - STDDEV ) ] ).toBack(); graph.zLine[ 1 ] = line( [ MEAN + STDDEV, 0 ], [ MEAN + STDDEV, graph.pdf( MEAN + STDDEV ) ] ).toBack(); }); graph.zLabel[ -1 ] = label( [ MEAN - STDDEV, 0 ], roundTo( 1, MEAN - STDDEV ), "below", { color: PINK } ); graph.zLabel[ 1 ] = label( [ MEAN + STDDEV, 0 ], roundTo( 1, MEAN + STDDEV ), "below", { color: PINK } );

We know the standard deviation is STDDEV years, so one standard deviation below the mean is roundTo( 1, MEAN - STDDEV ) years and one standard deviation above the mean is roundTo( 1, MEAN + STDDEV ) years.

graph.zLine[ -1 ].attr({ stroke: "#bbb" }); graph.zLine[ 1 ].attr({ stroke: "#bbb" }); graph.zLabel[ -1 ].css({ color: "#bbb" }); graph.zLabel[ 1 ].css({ color: "#bbb" }); \$( "#zm1Hint" ).parent().removeClass( "hint_pink" ); \$( "#zp1Hint" ).parent().removeClass( "hint_pink" ); style({ stroke: PINK }, function() { graph.zLine[ -2 ] = line( [ MEAN - STDDEV * 2, 0 ], [ MEAN - STDDEV * 2, graph.pdf( MEAN - STDDEV * 2 ) ] ).toBack(); graph.zLine[ 2 ] = line( [ MEAN + STDDEV * 2, 0 ], [ MEAN + STDDEV * 2, graph.pdf( MEAN + STDDEV * 2 ) ] ).toBack(); }); graph.zLabel[ -2 ] = label( [ MEAN - STDDEV * 2, 0 ], roundTo( 1, MEAN - STDDEV * 2 ), "below", { color: PINK } ); graph.zLabel[ 2 ] = label( [ MEAN + STDDEV * 2, 0 ], roundTo( 1, MEAN + STDDEV * 2 ), "below", { color: PINK } );

Two standard deviations below the mean is roundTo( 1, MEAN - STDDEV * 2 ) years and two standard deviations above the mean is roundTo( 1, MEAN + STDDEV * 2 ) years.

graph.zLine[ -2 ].attr({ stroke: "#bbb" }); graph.zLine[ 2 ].attr({ stroke: "#bbb" }); graph.zLabel[ -2 ].css({ color: "#bbb" }); graph.zLabel[ 2 ].css({ color: "#bbb" }); \$( "#zm2Hint" ).parent().removeClass( "hint_pink" ); \$( "#zp2Hint" ).parent().removeClass( "hint_pink" ); style({ stroke: PINK }, function() { graph.zLine[ -3 ] = line( [ MEAN - STDDEV * 3, 0 ], [ MEAN - STDDEV * 3, graph.pdf( MEAN - STDDEV * 3 ) ] ).toBack(); graph.zLine[ 3 ] = line( [ MEAN + STDDEV * 3, 0 ], [ MEAN + STDDEV * 3, graph.pdf( MEAN + STDDEV * 3 ) ] ).toBack(); }); graph.zLabel[ -3 ] = label( [ MEAN - STDDEV * 3, 0 ], roundTo( 1, MEAN - STDDEV * 3 ), "below", { color: PINK } ); graph.zLabel[ 3 ] = label( [ MEAN + STDDEV * 3, 0 ], roundTo( 1, MEAN + STDDEV * 3 ), "below", { color: PINK } );

Three standard deviations below the mean is roundTo( 1, MEAN - STDDEV * 3 ) years and three standard deviations above the mean is roundTo( 1, MEAN + STDDEV * 3 ) years.

graph.zLine[ -3 ].attr({ stroke: "#bbb" }); graph.zLine[ 3 ].attr({ stroke: "#bbb" }); graph.zLabel[ -3 ].css({ color: "#bbb" }); graph.zLabel[ 3 ].css({ color: "#bbb" }); \$( "#zm3Hint" ).parent().removeClass( "hint_pink" ); \$( "#zp3Hint" ).parent().removeClass( "hint_pink" ); graph.zLine[ Z ].attr({ stroke: PINK }); graph.zLabel[ Z ].css({ color: PINK }); style({ stroke: PINK, fill: PINK, arrows: "->" }, function() { line( [ MEAN + STDDEV * Z, -1 ], [ MEAN + STDDEV * 3.5, -1 ] ); ellipse( [ MEAN + STDDEV * Z, -1 ], [ 3 / ( 600 / ( STDDEV * 7 ) ), 3 / 40 ] ); });

We are interested in the probability of an( ANIMAL ) living longer than roundTo( 1, MEAN + STDDEV * Z ) years.

var shape = []; shape.push([ MEAN - STDDEV * abs( Z ), 0 ]); var step = STDDEV / 50; for ( var x = MEAN - STDDEV * abs( Z ); x <= MEAN + STDDEV * abs( Z ); x += step ) { shape.push([ x, graph.pdf( x ) ]); } shape.push([ MEAN + STDDEV * abs( Z ), graph.pdf( MEAN + STDDEV * abs( Z ) ) ]); shape.push([ MEAN + STDDEV * abs( Z ), 0 ]); shape.push([ MEAN - STDDEV * abs( Z ), 0 ]); style({ stroke: null, fill: BLUE, opacity: 0.3 }, function() { path( shape ); }); label([ MEAN, graph.pdf( MEAN - STDDEV * Z ) -0.3 ], EMPIRICAL + "\\%", "above", { color: "green" } ); style({ arrows: "->", stroke: "green" }, function() { line([ MEAN, graph.pdf( MEAN - STDDEV * Z ) -0.1 ], [ MEAN + STDDEV * Z, graph.pdf( MEAN + STDDEV * Z ) -0.1 ]); line([ MEAN, graph.pdf( MEAN + STDDEV * Z ) -0.1 ], [ MEAN - STDDEV * Z, graph.pdf( MEAN - STDDEV * Z ) -0.1 ]); });

The empirical rule (or the 68-95-99.7 rule) tells us that EMPIRICAL\% of the ANIMALS will have lifespans within plural( abs( Z ), "standard deviation" ) of the average lifespan.

style({ arrows: "->", stroke: "green" }, function() { path([ [ MEAN - STDDEV * abs( Z ), graph.pdf( MEAN - STDDEV * Z ) + 0.2 ], [ MEAN - STDDEV * abs( Z ), graph.pdf( MEAN - STDDEV * Z ) + 0.4 ], [ MEAN - STDDEV * 3.5, graph.pdf( MEAN - STDDEV * Z ) + 0.4 ] ]); }); label( [ MEAN - STDDEV * ( ( abs( Z ) + 3.5 ) / 2 ), graph.pdf( MEAN - STDDEV * Z ) + 0.3 ], roundTo( 2, ( 100 - EMPIRICAL ) / 2 ) + "\\%", "above", { color: "green" } ); style({ arrows: "->", stroke: "green" }, function() { path([ [ MEAN + STDDEV * abs( Z ), graph.pdf( MEAN + STDDEV * Z ) + 0.2 ], [ MEAN + STDDEV * abs( Z ), graph.pdf( MEAN + STDDEV * Z ) + 0.4 ], [ MEAN + STDDEV * 3.5, graph.pdf( MEAN + STDDEV * Z ) + 0.4 ] ]); }); label( [ MEAN + STDDEV * ( ( abs( Z ) + 3.5 ) / 2 ), graph.pdf( MEAN + STDDEV * Z ) + 0.3 ], roundTo( 2, ( 100 - EMPIRICAL ) / 2 ) + "\\%", "above", { color: "green" } );

The remaining roundTo( 2, 100 - EMPIRICAL )\% of the ANIMALS will have lifespans that fall outside the shaded area. Because the normal distribution is symmetrical, half (\color{green}{roundTo( 2, ( 100 - EMPIRICAL ) / 2 )\%}) will live less than roundTo( 2, MEAN - STDDEV * abs( Z ) ) years and the other half (\color{green}{roundTo( 2, ( 100 - EMPIRICAL ) / 2 )\%}) will live longer than roundTo( 2, MEAN + STDDEV * abs( Z ) ) years.

The probability of a particular ANIMAL living longer than roundTo( 1, MEAN + STDDEV * Z ) years is \color{green}{EMPIRICAL\%} + \color{green}{roundTo( 2, ( 100 - EMPIRICAL ) / 2 )\%}, or ANSWER\%.

The probability of a particular ANIMAL living longer than roundTo( 1, MEAN + STDDEV * Z ) years is \color{green}{ANSWER\%}.

{ "3": 99.85, "2": 97.5, "1": 84, "-1": 16, "-2": 2.5, "-3": 0.15 }[ Z ]

Estimate the probability of an( ANIMAL ) living less than roundTo( 1, MEAN + STDDEV * Z ) years.

graph.zLine[ -3 ].attr({ stroke: "#bbb" }); graph.zLine[ 3 ].attr({ stroke: "#bbb" }); graph.zLabel[ -3 ].css({ color: "#bbb" }); graph.zLabel[ 3 ].css({ color: "#bbb" }); \$( "#zm3Hint" ).parent().removeClass( "hint_pink" ); \$( "#zp3Hint" ).parent().removeClass( "hint_pink" ); graph.zLine[ Z ].attr({ stroke: PINK }); graph.zLabel[ Z ].css({ color: PINK }); style({ stroke: PINK, fill: PINK, arrows: "->" }, function() { line( [ MEAN + STDDEV * Z, -1 ], [ MEAN + STDDEV * -3.5, -1 ] ); ellipse( [ MEAN + STDDEV * Z, -1 ], [ 3 / ( 600 / ( STDDEV * 7 ) ), 3 / 40 ] ); });

We are interested in the probability of an( ANIMAL ) living less than roundTo( 1, MEAN + STDDEV * Z ) years.

The probability of a particular ANIMAL living less than roundTo( 1, MEAN + STDDEV * Z ) years is \color{green}{EMPIRICAL\%} + \color{green}{roundTo( 2, ( 100 - EMPIRICAL ) / 2 )\%}, or ANSWER\%.

The probability of a particular ANIMAL living less than roundTo( 1, MEAN + STDDEV * Z ) years is \color{green}{ANSWER\%}.

randRangeNonZero( -3, 2 ) randRangeNonZero( Z1 + 1, 3 ) max( abs( Z1 ), abs( Z2 ) ) min( abs( Z1 ), abs( Z2 ) ) { "-3": 99.7, "-2": 95, "-1": 68, "1": 68, "2": 95, "3": 99.7 }[ Z_MAX ] { "-3": 99.7, "-2": 95, "-1": 68, "1": 68, "2": 95, "3": 99.7 }[ Z_MIN ] { "3": 99.85, "2": 97.5, "1": 84, "-1": 16, "-2": 2.5, "-3": 0.15 }[ Z1 ] { "3": 99.85, "2": 97.5, "1": 84, "-1": 16, "-2": 2.5, "-3": 0.15 }[ Z2 ] TOTAL2 - TOTAL1

Estimate the probability of an( ANIMAL ) living between roundTo( 1, MEAN + STDDEV * Z1 ) and roundTo( 1, MEAN + STDDEV * Z2 ) years.

AREA
graph.zLine[ -3 ].attr({ stroke: "#bbb" }); graph.zLine[ 3 ].attr({ stroke: "#bbb" }); graph.zLabel[ -3 ].css({ color: "#bbb" }); graph.zLabel[ 3 ].css({ color: "#bbb" }); \$( "#zm3Hint" ).parent().removeClass( "hint_pink" ); \$( "#zp3Hint" ).parent().removeClass( "hint_pink" ); graph.zLine[ Z1 ].attr({ stroke: PINK }); graph.zLabel[ Z1 ].css({ color: PINK }); graph.zLine[ Z2 ].attr({ stroke: PINK }); graph.zLabel[ Z2 ].css({ color: PINK }); style({ stroke: PINK, fill: PINK, arrows: "" }, function() { line( [ MEAN + STDDEV * Z2, -1 ], [ MEAN + STDDEV * Z1, -1 ] ); ellipse( [ MEAN + STDDEV * Z2, -1 ], [ 3 / ( 600 / ( STDDEV * 7 ) ), 3 / 40 ] ); ellipse( [ MEAN + STDDEV * Z1, -1 ], [ 3 / ( 600 / ( STDDEV * 7 ) ), 3 / 40 ] ); });

We are interested in the probability of an( ANIMAL ) living between roundTo( 1, MEAN + STDDEV * Z1 ) and roundTo( 1, MEAN + STDDEV * Z2 ) years.

var shape = []; shape.push([ MEAN - STDDEV * abs( Z_MAX ), 0 ]); var step = STDDEV / 50; for ( var x = MEAN - STDDEV * abs( Z_MAX ); x <= MEAN + STDDEV * abs( Z_MAX ); x += step ) { shape.push([ x, graph.pdf( x ) ]); } shape.push([ MEAN + STDDEV * abs( Z_MAX ), graph.pdf( MEAN + STDDEV * abs( Z_MAX ) ) ]); shape.push([ MEAN + STDDEV * abs( Z_MAX ), 0 ]); shape.push([ MEAN - STDDEV * abs( Z_MAX ), 0 ]); style({ stroke: null, fill: BLUE, opacity: 0.3 }, function() { path( shape ); }); label([ MEAN, graph.pdf( MEAN - STDDEV * Z_MAX ) -0.3 ], EMPIRICAL1 + "\\%", "above", { color: "green" } ); style({ arrows: "->", stroke: "green" }, function() { line([ MEAN, graph.pdf( MEAN - STDDEV * Z_MAX ) -0.1 ], [ MEAN + STDDEV * Z_MAX, graph.pdf( MEAN + STDDEV * Z_MAX ) -0.1 ]); line([ MEAN, graph.pdf( MEAN + STDDEV * Z_MAX ) -0.1 ], [ MEAN - STDDEV * Z_MAX, graph.pdf( MEAN - STDDEV * Z_MAX ) -0.1 ]); });

The empirical rule (or the 68-95-99.7 rule) tells us that EMPIRICAL1\% of the ANIMALS will have lifespans within plural( abs( Z_MAX ), "standard deviation" ) of the average lifespan.

label([ MEAN, graph.pdf( MEAN - STDDEV * Z_MIN ) -0.3 ], EMPIRICAL2 + "\\%", "above", { color: "green" } ); style({ arrows: "->", stroke: "green" }, function() { line([ MEAN, graph.pdf( MEAN - STDDEV * Z_MIN ) -0.1 ], [ MEAN + STDDEV * Z_MIN, graph.pdf( MEAN + STDDEV * Z_MIN ) -0.1 ]); line([ MEAN, graph.pdf( MEAN + STDDEV * Z_MIN ) -0.1 ], [ MEAN - STDDEV * Z_MIN, graph.pdf( MEAN - STDDEV * Z_MIN ) -0.1 ]); }); label([ MEAN + STDDEV * ( ( Z_MIN + Z_MAX ) / 2 ), graph.pdf( MEAN - STDDEV * Z_MIN ) -0.3 ], roundTo( 2, ( ( EMPIRICAL1 - EMPIRICAL2 ) / 2 ) ) + "\\%", "above", { color: "orange" } ); label([ MEAN - STDDEV * ( ( Z_MIN + Z_MAX ) / 2 ), graph.pdf( MEAN - STDDEV * Z_MIN ) -0.3 ], roundTo( 2, ( ( EMPIRICAL1 - EMPIRICAL2 ) / 2 ) ) + "\\%", "above", { color: "orange" } ); style({ arrows: "->", stroke: "orange" }, function() { line([ MEAN + STDDEV * ( ( Z_MIN + Z_MAX ) / 2 ), graph.pdf( MEAN - STDDEV * Z_MIN ) -0.1 ], [ MEAN + STDDEV * Z_MIN, graph.pdf( MEAN + STDDEV * Z_MIN ) -0.1 ]); line([ MEAN + STDDEV * ( ( Z_MIN + Z_MAX ) / 2 ), graph.pdf( MEAN - STDDEV * Z_MIN ) -0.1 ], [ MEAN + STDDEV * Z_MAX, graph.pdf( MEAN + STDDEV * Z_MIN ) -0.1 ]); line([ MEAN - STDDEV * ( ( Z_MIN + Z_MAX ) / 2 ), graph.pdf( MEAN + STDDEV * Z_MIN ) -0.1 ], [ MEAN - STDDEV * Z_MIN, graph.pdf( MEAN - STDDEV * Z_MIN ) -0.1 ]); line([ MEAN - STDDEV * ( ( Z_MIN + Z_MAX ) / 2 ), graph.pdf( MEAN + STDDEV * Z_MIN ) -0.1 ], [ MEAN - STDDEV * Z_MAX, graph.pdf( MEAN - STDDEV * Z_MIN ) -0.1 ]); });

It also tells us that EMPIRICAL2\% of the ANIMALS will have lifespans within plural( Z_MIN, "standard deviation" ) of the mean. That leaves EMPIRICAL1\% - EMPIRICAL2\% = roundTo( 2, EMPIRICAL1 - EMPIRICAL2 )\% of ANIMALS between Z_MIN and plural( Z_MAX, "standard deviation" ) of the mean, or roundTo( 2, ( EMPIRICAL1 - EMPIRICAL2 ) / 2 )\% on either side of the distribution.

The probability of a particular ANIMAL living between roundTo( 1, MEAN + STDDEV * Z1 ) and roundTo( 1, MEAN + STDDEV * Z2 ) years is \color{orange}{roundTo( 2, ( EMPIRICAL1 - EMPIRICAL2 ) / 2 )\%}. \color{orange}{roundTo( 2, ( EMPIRICAL1 - EMPIRICAL2 ) / 2 )\%} + \color{green}{EMPIRICAL2\%}, or roundTo( 2, AREA )\%.

The probability of a particular ANIMAL living between roundTo( 1, MEAN + STDDEV * Z1 ) and roundTo( 1, MEAN + STDDEV * Z2 ) years is \color{orange}{roundTo( 2, ( EMPIRICAL1 - EMPIRICAL2 ) / 2 )\%}. \color{green}{EMPIRICAL2\%} + \color{orange}{roundTo( 2, ( EMPIRICAL1 - EMPIRICAL2 ) / 2 )\%}, or roundTo( 2, AREA )\%.

The probability of a particular ANIMAL living between roundTo( 1, MEAN + STDDEV * Z1 ) and roundTo( 1, MEAN + STDDEV * Z2 ) years is \color{green}{roundTo( 2, AREA )\%}.

Something went wrong with that request. Please try again.