Expected Value Advanced

Expected Value Advanced preview image

2 collaborators

Uri_dolphin3 Uri Wilensky (Author)
Dor Abrahamson (Author)

Tags

mathematics 

Tagged by Reuven M. Lerner almost 11 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 453 times • Downloaded 102 times • Run 1 time
Download the 'Expected Value Advanced' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


WHAT IS IT?

Expected Value Advanced illustrates expected-value analysis under the special condition that the sample size varies. This model extends the ProbLab model Expected Value, where the sample size is fixed.

Expected-value analyses look at the 'value' of outcomes in probability experiments in terms of some utilitarian framework, such as money or points. As in life, some events are more significant to us --- they are "worth" more for our endeavors. In that sense, expected-value simulations go beyond looking just at chance --- how often or how rarely something happens --- they introduce a "worth" factor (weight, coefficient) associated with experimental outcomes. Value and worth are not synonymous; sometimes a low value has a high worth. For instance, in golf, the lowest value has the highest worth. These simulations examine not only the issue of what I should expect from a phenomenon involving random behavior, but also, what it would be worth for me. For instance, if I draw 12 coins randomly from a sack containing an equal number of pennies, nickels, dimes, and quarters, we could talk about which 12 'coins' I could expect to get (a nominal classification) --- 3 'pennies,' 3 'nickels,' 3 'dimes,' and 3 'quarters,' on average. But we could extend the discussion to how much money I would get, that is, what would be the 'value' of my draw. To do this, we'd have to multiply the expected frequency of each type of coin by its value:

3 * 1 + 3 * 5 + 3 * 10 + 3 * 25 = 123 cents.

A more general way of putting this all together -- the sample size (12), the probabilities of each coin (1/4 for each of them), and the value of each coin (1, 5, 10, and 25) -- is:

12 * (1/4 * 1 + 1/4 * 5 + 1/4 * 10 + 1/4 * 25) = 123 cents.

Thus, the term 'expected value' may be defined as the sum of the products of the probability of each possible event and the value of that event multiplied by the sample size.

This model is a part of the ProbLab curriculum. The ProbLab curriculum is currently under development at the CCL. For more information about the ProbLab curriculum please refer to http://ccl.northwestern.edu/curriculum/ProbLab/.

HOW IT WORKS

The analogy in the model is a lake with fish swimming around. Each type of fish is worth a certain number of dollars (1, 2, 3, 4 or 5) [other currencies or point systems apply just as well]. The distribution of types of fish by amount-of-worth -- how many $1-fish, $2-fish, ..., or $5-fish there are in the pond --- is set by the sliders on the left. With the sliders, we can set the distribution of fish by type and, therefore, the chance of catching each type of fish. That is, the higher you have set a given slider as compared to other sliders (see the % IN POPULATION monitors), the higher the chance of catching a fish with that worth. For instance, the more $2-fish there are, the higher is the chance of catching a $2-fish in a random sample. Note that the more valuable the fish, the lighter its body color. You can press CLICK SELECTION and then click in the view to "catch" a random sample, or press RANDOM SELECTION to have the choosing done for you. The computer selects randomly. You, too, can select "blindly," if you turn on the BLIND? switch.

Note that the sampling in this model is of arrays, e.g., a 2-by-3 array of 6 squares. There are as many fish in this model as there are squares. One might expect to catch 6 fish when one samples from 6 squares. However, when the WANDER button is pressed, the fish wander randomly, and so sometimes 6 squares have more than 6 fish and sometimes they have less. You can think of each selection as a fishing net that is dipped into the lake --- the fisher doesn't know how many fish will be in the net. This feature of the model creates variation in sample size. Thus, one idea that this model explores is that even under variation in sample size, we still receive outcomes that correspond to the expected value that we calculate before taking samples.

PEDAGOGICAL NOTE

The idea of 'expected value' is that we can formulate an educated guess of the dollar worth of the fish we catch. It's similar to asking, "How much does the average fish cost?" We need to somehow take into account both the chance of getting each type of fish and its dollar value. The computer program will do much of the calculations for us, but here's the gist of what it does:

Let's say that the ratio units we set up for $1, $2, $3, $4 and $5 fish were, respectively,

1 : 6 : 5 : 0 : 4.

The number '6,' for example, indicates our ratio setting for fish worth 2 dollars. You can immediately see that the chance of getting a $2-fish is greater than the chance of getting a $3-fish, because the chance of getting a $2-fish (6 units) has more ratio units than the $3-fish (5 units). But in order to determine precisely the chance is of getting each type of fish, we need to state the ratio units relative to each other. We need a common denominator. In this particular setting, there is a total of 16 'ratio units':

1 + 6 + 5 + 0 + 4 = 16.

Now we can say that if we catch a fish, there is, for instance, a 4-in-16 chance that it is a $5-fish. That is a 25% chance of catching a fish that is worth exactly 5 dollars. We can also say that this relative proportion of $5-fish in the lake contributes .25 * 5, that is, $1.25, to the mean value of a single fish in the lake. Similarly, we can say there is a 5-in-16 chance of getting a $3-fish, a 6-in-16 chance of getting a $2-fish, etc. If we sum up all products of 'value' and 'probability,' we get the expected value per single fish:

(1 * 1/16) + (2 * 6/16) + (3 * 5/16) + (4 * 0/16) + (5 * 4/16) = 48/16 = 3 dollars per fish.

This tells us that if you pick any single fish under these settings, you should expect to get a value of 3 dollars. If you were to select a sample of 6 fish, then you would expect to pocket 18 dollars (6 fish * 3 dollars-per-fish).

HOW TO USE IT

Begin by setting fish-value ratios (or just use the default settings). Click SETUP and watch the information updated in the view. Below are more features of the model that will let you change the way it looks and runs.

If you change any of the sliders you will have to press SETUP for the changes to take effect.

Sliders

$1-FISH, $2-FISH, $3-FISH, $4-FISH, $5-FISH -- ratio-unit settings for the distribution of fish by value. Note that the ratios are set in proportion to each other. The labels under the sliders indicate that the higher-valued fish have the lighter fish body color.

HEIGHT-OF-SAMPLE -- sets the height of the sample selection.

WIDTH-OF-SAMPLE -- sets the width of the sample selection.

The two previous sliders determine the selection area (width x height).

Switches

SETUP-APART? -- when set to 'On,' and the SETUP button is pressed, the fish populations group by value, wait 3 seconds, and then swim to a random locations in the display.

BLIND? -- if 'On,' you will only see the fish you caught in the currently-selected sample; if 'Off,' you will always see all of the fish.

SHOW-VAL? -- when set to 'On,' selected fish display their monetary value (worth).

Buttons

SETUP -- initializes variables, re-colors the fish, and resets monitors and graphs.

CLICK SELECTION -- waits for the user to select an area in the pool, counts up the total number of fish in that area as well as their values, calculates, and graphs totals of these values.

RANDOM SELECTION-- randomly chooses sample areas in the pool.

WANDER -- moves all fish in random directions.

Monitors

% IN POPULATION -- shows the percentage of each type of fish in the population. For instance, if the % IN POPULATION monitor for $3-FISH is at 50%, then half of all the fish in the lake will have a value of exactly 3 dollars.

EXPECTED VALUE CALCULATION -- This monitor reports the calculation of the expected number of dollars per sample. The calculation first determines the value of the 'average fish, ' given the $-unit slider settings for the population distribution by value, and then multiplies this value by the number of fish in an average sample of size 'height * width.' Thus, 'average value' * 'sample-size' = total is the expected value of sample.

EXPECTED VALUE -- shows the result of the expected-value calculation (see above).

MEAN VALUE PER SAMPLE -- shows the cumulative mean value of samples over all samples taken.

CURRENT SAMPLE VALUE -- shows the total value of the current sample selection.

CURRENT NUMBER OF FISH -- shows the number of fish in the current sample selection.

AVERAGE NUMBER OF FISH PER SAMPLE -- shows the average number of fish over all samples taken.

SQUARES -- monitors the number of squares in the selection area. The value of SQUARES is the product of HEIGHT-OF-SAMPLE and WIDTH-OF-SAMPLE

RUNS -- monitors the number of times that the user or computer sampled from the pool.

Plots

VALUE PER SAMPLE -- plots the number of occurrences of specific value totals in the samples. For instance, it shows how many times your sample was worth exactly $4. It also plots the expected value (in red) and the mean value per sample (in green),

NUMBER PER SAMPLE -- plots the number of fish in the samples. It also plots the average number of fish per sample (in green) as well as the number of squares, that is, the expected average number of fish in samples (in red).

MEAN VALUE OVER TIME -- plots, over time, the values of each sample (in black), as well as the expected value (in red), which does not change after Setup.

THINGS TO NOTICE

In NetLogo, the location of a turtle in terms of the world coordinate system is determined by the location of the center of its shape. So when one selects an area in this model, a fish may be sampled even though it is not completely inside the selected area (for instance, its tail might be sticking out of that area).

Fish may be unevenly distributed throughout the 'lake.' This feature distinguishes the Expected Value Advanced model from the Expected Value model, where the elements (tiles) are evenly distributed (1 tile per patch). The model monitors the number of fish per sample in the NUMBER PER SAMPLE plot.

The SQUARES monitor changes with the HEIGHT-OF-SAMPLE and WIDTH-OF-SAMPLE sliders to show the size of the selection array of squares (NetLogo "patches"). Because in this version of the model there are exactly as many fish as there are squares, SQUARES shows the expected average number of fish per sample. It is only "expected" and not fixed, both because the fish are not distributed uniformly in the view when you setup and because the fish may optionally move (if you have pressed WANDER).

For equal HEIGHT-OF-SAMPLE and WIDTH-OF-SAMPLE settings, mouse clicks are in the center of the sample array. For other settings, the click in not in the center.

At setup, the distribution of fish by value in the sample space is often an approximation and not completely accurate. The program sets the probabilities according to the ratios, but it still produces a very small error. There are a fixed number of patches and fish (in the default setting of the model there are 121 squares and 121 fish). This number cannot precisely accommodate all the different possible ratio settings. For instance, we cannot have two equal halves. That is, there will be settings where the program will make approximations. These approximations will lead to some minor degree of experimental error.

Look at the plot MEAN VALUE OVER TIME. The more samples you take, the closer the red line gets to the black line. Can you explain this?

THINGS TO TRY

Run the model under different setting of the switches. Does it take longer for the model to converge on the expected values when you are not working entirely randomly?

Change the height and width of the selection area. Run the experiment, looking at the MEAN VALUE OVER TIME plot. Does this plot behave differently for different height and width settings?

As noted above, the relative ratios of the $1-fish, $2-fish, etc. in the lake are determined by relative values of all of the sliders on the left. In other words, if you set the ratio units to 1 : 1 : 1 : 1 : 1, it is going to mean the same to the computer as the setting of 2 : 2 : 2 : 2 : 2 or 5 : 5 : 5 : 5 : 5. The sliders are designed to allow an exploration of a rich range of proportions of the different fish. Try extending the maximum value of the ratio sliders, to obtain an even richer range of proportions of fish populations.
In the Code tab, go to the 'setup-misc-globals' procedure and change the color value assigned to the 'c-color' local variable. This will change the fish base color.

EXTENDING THE MODEL

Currently, samples wrap around the view. Edit the code in the select-area procedure so the selection does not wrap around. One way to go about this may be to shift the selected area. For instance, if the user clicks near the right side of the window, a procedure could translate the location of the mouse-click as many patches to the left as necessary.

Add another ratio-unit slider, either for 0 value or beyond 5. It could also be a decimal value between 1 and 5, such as 1.7. It could even be a 'negative value,' which could be interpreted as an added expense, like catching a whale that breaks your fishing rod.

NETLOGO FEATURES

This utilizes the mouse-clicking capabilities of NetLogo. Note that in order to use the mouse-clicking functionality a forever button must be running, so that there are active procedures to "catch" your clicks.

RELATED MODELS

This model is considered more advanced than Expected Value. Both models utilize the idea of 'expected value,' but Expected Value Advanced supplements this with variation in sample size.

The SETUP-APART? functionality arranges the raw data (the fish themselves) in "histograms" as in 9-Block Stalagmite. Also, the BLIND? functionality produces an effect that is similar to that in HubNet SAMPLER, where the population is hidden and only the sample is visible.

CREDITS AND REFERENCES

This model is a part of the ProbLab curriculum. The ProbLab Curriculum is currently under development at Northwestern's Center for Connected Learning and Computer-Based Modeling. For more information about the ProbLab Curriculum please refer to http://ccl.northwestern.edu/curriculum/ProbLab/.

HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

  • Abrahamson, D. and Wilensky, U. (2004). NetLogo Expected Value Advanced model. http://ccl.northwestern.edu/netlogo/models/ExpectedValueAdvanced. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
  • Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.

COPYRIGHT AND LICENSE

Copyright 2004 Uri Wilensky.

CC BY-NC-SA 3.0

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

This model was created as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227.

Comments and Questions

Click to Run Model

globals
[
  ;;current coordinates of the picked point
  picked-x picked-y

  ;;bounds (measures how many patches away from the picked point)
  ;;area = (dimension-left + dimension-right + 1) * (dimension-top + dimension-bottom + 1)
  dimension-left dimension-bottom dimension-right dimension-top

  ;;the list of all of the totals per sample (value)
  all-totals
  ;;the list of all of the "number of fish" per sample (number)
  all-numbers

  ;;the total in the current viewing area
  current-total

  ;;list of the colors given for each number of marbles
  colors

  ;;true if clicked before last clearing
  just-down?

  ;;stores values at setup for easier use in procedures
  ratio-list   #fish

  ;;this is a mathematical combination of slider settings to compare
  ;;against the current combination of slider settings to see if any of them had been changed
  setup-constant

  ;;for moving red lines at setup
  prev-exp-val  prev-squares
]

breed [ fish a-fish]

;;orig-color holds the fish's given color
;;selected? is true when the fush is currently selected
fish-own [ orig-color selected? ]

;;blue-color holds the patch's given color
patches-own [ index  blue-color ]

to startup
  setup
end 

;;creates the fish, initializes variables and graphs

to setup
  clear-all

  setup-misc-globals
  setup-dimensions
  reset-ticks

  ;;makes background light blue
  ask patches [ set pcolor blue + 1 + random 3  set blue-color pcolor ]

  ;;creates the fish (# of fish = # of patches) and colors them
  create-fish #fish
  [
    pick-random-place
    setxy picked-x picked-y
    set size .75
    set selected? false
  ]
  setup-fish-color
  clear
end 

;;sets the global variables to their initial values

to setup-misc-globals
  set-default-shape fish "fish-eye-fin"
  set all-totals []
  set all-numbers []
  set just-down? false

  set ratio-list (list $1-fish $2-fish $3-fish $4-fish $5-fish)
  let setup-values (sentence ratio-list height-of-sample width-of-sample)
  ;;calculates the setup-constant -- a combination of slider settings to compare against the current constant
  ;;it is a faster way of checking whether any slider settings have been changed
  set setup-constant sum (map [?1 ^ ?2] setup-values [1.21 1.22 1.23 1.24 1.25 1.26 1.27])
  set #fish count patches

  set prev-exp-val monitor-exp-val
  set prev-squares (width-of-sample * height-of-sample)

  ;;sets the colors to what they need to be (to show the value) [lighter = more valuable]
  let c-color green
  set colors (list (c-color - 3) (c-color - 1.5) c-color (c-color + 1.5) (c-color + 3))
end 

;;if pressed, looks for a mouse-click in the view;
;;when clicked, selects area of the sample and
;;counts up the value and number of fish in the sample

to click-select
  ifelse mouse-down?
  [
    set just-down? true

    ;;sets the 'previous' variables
    let prev-x picked-x
    let prev-y picked-y

    ;sets currently-picked variables
    set picked-x [pxcor] of patch mouse-xcor mouse-ycor
    set picked-y [pycor] of patch mouse-xcor mouse-ycor

    ;;checks so point is different than one before
    ;;-- no holding mouse down and getting more than one selection
    if not (prev-x = picked-x and prev-y = picked-y)
    [
      ;;clears the view from previous click
      clear
      ;;appends the numbers for average, selects area in the view
      select-area
      set all-numbers lput (count fish with [ selected? = true ]) all-numbers
      set all-totals lput current-total all-totals
      ;;updates the histogram and plot
      plot-graphs
    ]
  ]
  ;;clear if no click and haven't cleared yet
  [
    if just-down? [ clear  set picked-x -1000 ]
  ]
end 

;;randomly selects coordinates and runs the simulation
;;it is the computer's click-select, randomly choosing the picked point

to random-select
  ;;choose a random patch location
  set picked-x random-pxcor
  set picked-y random-pycor

  ;;selects the area and updates the numbers for average and plots
  select-area
  set all-numbers lput (count fish with [ selected? = true ]) all-numbers
  set all-totals lput current-total all-totals

  ;;clears the view from previous selection
  clear
  tick

  ;;updates the histogram and plot
  plot-graphs
end 

;;picks a random point in the valid bounds

to pick-random-place
  set picked-x random-xcor
  set picked-y random-ycor
end 

;;re-sets the patches and fish to original state

to clear
  ;;re-colors patches to lake color
  ask patches [ set pcolor blue-color ]

  ;;re-sets fish to original state
  ask fish [ set selected? false ]
  if show-val?
    [ ask fish [ set label "" ] ]
  ifelse not blind?
    [ ask fish [ set hidden? false ] ]
    [ ask fish [ set hidden? true ]  ]
end 

;;selects the area, highlighting the patches with yellow
;;puts the fish's value as the label, if show-val? is 'On'

to select-area
  ;;re-sets current-total
  set current-total 0
  ;;colors patches gray
  ask patches [ set pcolor gray - 1 ]

  ;;loop that selects the area, using dimension-top, bottom, right, left
  ;;wrapping around is allowed
  let cur-y (- dimension-bottom)
  let cur-x (- dimension-left)
  ask patch picked-x picked-y
  [
    while [cur-y <= dimension-top]
    [
      set cur-x (- dimension-left)
      while [cur-x <= dimension-right]
      [
        ask (fish-at cur-x cur-y)
        [
          ;adds this fish's value to total
          set current-total current-total + position color colors + 1
          ;;shows the fishs value, if show-val? is set to 'On'
          if show-val?
          [
            set label-color red
            set label position color colors + 1
          ]
          set selected? true
          set hidden? false
        ]
        ask patch-at cur-x cur-y [ set pcolor yellow ]
        set cur-x cur-x + 1
      ]
      set cur-y cur-y + 1
    ]
  ]
  ;;hide turtles which are not selected
  ask fish with [ selected? = false ] [ set hidden? true ]
end 

;;sets the colors of the fish

to setup-fish-color
  ;;makes all fish have no value
  ask fish [ set orig-color -1 ]
  let cur-val 0

  ;;finds the number of fish of each type that need to be colored:
  ;;makes the percentages, multiplies them by the number of fish
  let list-of-nums map [? / (sum ratio-list) * #fish] ratio-list
  ;;rounds the numbers of fish with a specific value
  let r-list-of-nums map [round ?] list-of-nums

  ;;tweaks the values, either in the positive or in the negative direction,
  ;;for the sum to equal to the number of fish on the display
  let nums setup-fish-color-polish list-of-nums r-list-of-nums

  ifelse not setup-apart?
  [
    ;;prints out the values on random fish that are not already taken
    foreach nums
    [
      ask n-of ? fish with [ orig-color = -1 ]
      [ set orig-color item cur-val colors ]
      set cur-val cur-val + 1
    ]
    ask fish [ set color orig-color ]
  ]
  [
    ;;shows fish apart, then moves them to new, random location
    histogram-patches nums
    ask fish [ die ]
    ask patches
    [
      sprout-fish 1 [ set size .75  set selected? false
                      set orig-color pcolor  set color pcolor ]
      set pcolor gray - 1
    ]
    display  wait 3
    ask patches [ set pcolor blue-color ]
    ;;disperses the fish
    swim-to-new
  ]
end 

;;tweaks the values so that they add up to the number of fish
;;(in the positive or negative direction)

to-report setup-fish-color-polish [ list-of-nums r-list-of-nums ]
  ;;tweaks the values, either in the positive or in the negative direction,
  ;;for the sum to equal to the number of fish on the display:
  ;;if the sum is below the number of fish, then searches for the highest remainder to round up
  while [sum r-list-of-nums < #fish]
  [
    let remainders map [remainder (? * #fish) #fish] list-of-nums
    ;;finds the position of the maximum remainder
    let pos-of-max position (max remainders) remainders
    ;;updates the list of numbers of each type of fish
    set r-list-of-nums (replace-item pos-of-max r-list-of-nums ((item pos-of-max r-list-of-nums) + 1))
    set list-of-nums (replace-item pos-of-max list-of-nums (floor (item pos-of-max list-of-nums) + 1))
  ]
  ;;if the sum is above the number of fish, then searches for the lowest remainder to round up
  ;;to prevent a bug that would make 0 always be the minimum remainder, all the 0's are changed
  ;;to large numbers, for them to not become negative
  while [sum r-list-of-nums > #fish]
  [
    let remainders map [remainder (? * #fish) #fish ] list-of-nums
    ;;remainders1 makes sure that the 0 is not deemed the minimum remainder, but
    ;;looks for minimum remainder above 0
    let remainders1 []
    foreach remainders
    [
      ifelse ? = 0
      [ set remainders1 (lput #fish remainders1) ]
      [ set remainders1 (lput ? remainders1) ]
    ]
    ;;finds the position of the minimum remainder
    let pos-of-min position (min remainders1) remainders1
    ;;updates the list of numbers of each type of fish
    set r-list-of-nums (replace-item pos-of-min r-list-of-nums ((item pos-of-min r-list-of-nums) - 1))
    set list-of-nums (replace-item pos-of-min list-of-nums (floor (item pos-of-min list-of-nums)))
  ]
  report r-list-of-nums
end 

;;sets the selecting area variables, given height and width

to setup-dimensions
  let h height-of-sample - 1
  let w width-of-sample - 1
  set dimension-left floor (w / 2)        ;sets how many patches on left
  set dimension-right floor (w / 2)       ;sets how many patches on bottom
  if (w mod 2 != 0) [ set dimension-right dimension-right + 1 ]
  set dimension-bottom floor (h / 2)      ;sets how many patches on right
  set dimension-top floor (h / 2)         ;sets how many patches on top
  if (h mod 2 != 0) [ set dimension-bottom dimension-bottom + 1 ]
end 

;;fish method run by the [Wander] button
;;makes fish wander around aimlessly, if not currently selected

to wander-around
  every .5
  [
    if not selected?
    [
      ;;moves one, changes direction
      fd 1
      rt random 360
    ]
  ]
end 

;;updates the histogram

to plot-graphs
  set-current-plot "Value per Sample"
  set-current-plot-pen "Count"
  ;;changes range
  if length all-totals > 5 and length all-totals mod 10 = 0
  [ set-plot-x-range (floor (min all-totals / 5)) * 5 (ceiling ((max all-totals + 1) / 5)) * 5 ]
  plot-pen-reset
  histogram all-totals
  let maxbar modes all-totals
  let maxrange length filter [ ? = item 0 maxbar ] all-totals
  set-plot-y-range 0 max list 10 maxrange
  ;;plots the "average" line
  set-current-plot-pen "Mean"
  plot-pen-reset
  plot-vert-line ((sum all-totals) / (length all-totals))
  ;;plots the expected value
  set-current-plot-pen "ExpVal"
  plot-pen-reset
  plot-vert-line monitor-exp-val

  set-current-plot "Number per Sample"
  set-current-plot-pen "default"
  if length all-numbers > 5 and length all-numbers mod 10 = 0
  [ set-plot-x-range (floor (min all-numbers / 5)) * 5 (ceiling ((max all-numbers + 1) / 5)) * 5 ]
  plot-pen-reset
  histogram all-numbers
  set maxbar modes all-numbers
  set maxrange length filter [ ? = item 0 maxbar ] all-numbers
  set-plot-y-range 0 max list 10 maxrange
  ;;plots the "average" line
  set-current-plot-pen "Mean"
  plot-pen-reset
  plot-vert-line ((sum all-numbers) / (length all-numbers))
  ;;plots the number of squares in the selection
  set-current-plot-pen "Squ"
  plot-pen-reset
  plot-vert-line height-of-sample * width-of-sample

  set-current-plot "Mean Value Over Time"
  set-current-plot-pen "Mean"
  plot sum all-totals / length all-totals
  set-current-plot-pen "ExpVal"
  if length all-totals > 10 [ plot monitor-exp-val ]
  if plot-y-min < 0 [ set-plot-y-range 0 plot-y-max ]
end 

;;generates the expected value calculation for the monitor

to-report monitor-exp-val-calculation
  let my-ratio-list 0

  set my-ratio-list ratio-list

  ;; the "% in Population" monitors pick items from ratio-monitor-list
  let ratio-monitor-list map [ (word ? "/" (sum my-ratio-list) " = "
                                     precision (100 * ? / (sum my-ratio-list)) 1
                                     "%") ]
                             my-ratio-list
  let exp-val-calc word (width-of-sample * height-of-sample) " * ("
  foreach [ 1 2 3 4 ]
  [
    set exp-val-calc (word exp-val-calc
                           ? " * "
                           item (? - 1) my-ratio-list
                           "/"
                           (sum my-ratio-list)
                           " + ")
  ]
  ;; we separated out the "5 case" because we don't add a "+", but we do add a ") ="
  set exp-val-calc (word exp-val-calc 5 " * "
                         item 4 my-ratio-list
                         "/" (sum my-ratio-list) ") = ")

  report exp-val-calc
end 

;;generates the expected value for the monitor

to-report monitor-exp-val
  let my-ratio-list 0

  set my-ratio-list ratio-list

  report (width-of-sample * height-of-sample)
         * sum map [ ? * item (? - 1) my-ratio-list / (sum my-ratio-list) ] [ 1 2 3 4 5 ]
end 

;;shows the populations separately

to histogram-patches [ list-of-nums ]
  ;;indexes the patches (from left to right, down to up), if had not already done so
  ask patches [ set index ((pxcor + max-pxcor) * (max-pxcor * 2 + 1) + (pycor + max-pycor)) ]

  ;;shows the separated populations
  let patch-now 0
  let temp 0
  foreach list-of-nums
  [
    repeat ?
    [
      ask patches with [index = patch-now] [ set pcolor item temp colors ]
      set patch-now patch-now + 1
    ]
    set temp temp + 1
  ]
end 

;;disperses fish population

to swim-to-new
  let list-of-moves []
  let steps (round (#fish / 4))
  ;;finds the movement amount of each fish per step, changes heading
  ask fish
  [
    pick-random-place
    set list-of-moves (lput (distancexy picked-x picked-y  / steps) list-of-moves)
    facexy picked-x picked-y
  ]
  ;;moves the fish
  repeat steps [ ask fish [ fd (item who list-of-moves) wait .05] ]
end 

;;plots a vertical line at x-coord

to plot-vert-line [ x-coord ]
  plotxy x-coord plot-y-min
  plot-pen-down
  plotxy x-coord plot-y-max
  plot-pen-up
end 

;;reports the output for the % in Population monitors, given the $index
;;also checks if changes were made to the current ratios slider from the
;;original setup position

to-report monitor-%-in-pop [ which ]
  let ratios (list $1-fish $2-fish $3-fish $4-fish $5-fish)
  report (word item (which - 1) ratios
               "/" sum ratios " = "
               precision (100 * item (which - 1) ratios / sum ratios) 1
               "%")
end 


; Copyright 2004 Uri Wilensky.
; See Info tab for full copyright and license.

There are 15 versions of this model.

Uploaded by When Description Download
Uri Wilensky almost 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky over 11 years ago Updated version tag Download this version
Uri Wilensky over 11 years ago Updated to version from NetLogo 5.0.3 distribution Download this version
Uri Wilensky over 12 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky almost 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky almost 14 years ago Expected Value Advanced Download this version

Attached files

File Type Description Last updated
Expected Value Advanced.png preview Preview about 11 years ago, by Reuven M. Lerner Download

This model does not have any ancestors.

This model does not have any descendants.