Showing posts with label diversity. Show all posts
Showing posts with label diversity. Show all posts

Saturday, August 22, 2026

Python (TI-84 Evo) and HP 32S: Shannon-Wiener Diversity Index

Python (TI-84 Evo) and HP 32S: Shannon-Wiener Diversity Index



Introduction



The Shannon-Wiener Diversity Index measures a level of how diverse a group of populations are by the following formula:


H = -Σ( p_i * ln(p_i), i = 1 to n) = Σ( -p_i * ln(p_i), i = 1 to n)


where p_i = (population of segment i) ÷ (total population)


As stated by M. Robert F. (see Sources), there are three levels of diversity:


Low Diversity: H < 1.5

Medium Diversity: 1.5 ≤ H ≤ 2.5

High Diversity: H > 2.5




Python: diverse.py

This script was done with a TI-84 Evo. The script uses only the math module and should be able to use on any calculator with Python.


# Math Calculations

from math import *


print("Shannon-Wiener Diversity\nIndex")

print("Enter a list of populations.")

lpop=eval(input("list: "))

# sum

s=sum(lpop)

# proportions

lpro=[i/s for i in lpop]

# -p log p

llog=[-i*log(i) for i in lpro]

# diversity index

h=sum(llog)


# results

print("diversity index:\n{0:.5f}".format(h))

if h>2.5:

  print("high diversity")

elif h<1.5:

  print("low diversity")

else:

  print("medium diversity")


HP 32S Program: Shannon Wiener Diversity Index


This was programmed on an original HP 32S, and the code should be same for the later HP 32SII and Swiss Micros DM32.


Instructions:

If need be, clear the variables

Enter the population size in the variables A, B, C, D etc… up to 25.

Enter the number of populations, XEQ A


Four labels are used:

LBL A: program start, initialization

LBL Z: determine total population (pop = A + B + C + D….)

LBL Y: determine proportion of each population (A = A/pop, B = B/pop, C = C/pop, …)

LBL X: calculate the Shannon-Weiner Diversity Index: -Σ(p_i * ln(p_i) from i=1 to n)



Code:

A01 LBL A

A02 ENTER

A03 ENTER

A04 ENTER

A05 CL x

A06 STO Z

A07 R↓

A08 STO i


Z01 LBL Z

Z02 RCL(i)

Z03 STO+ Z

Z04 R↓

Z05 DSE i

Z06 GTO Z

Z07 STO i

Z08 RCL Z


Y01 LBL Y

Y02 STO÷ (i)

Y03 DSE i

Y04 GTO Y

Y05 R↓

Y06 STO i

Y07 CL x

Y08 STO Z


X01 LBL X

X02 RCL (i)

X03 ENTER

X04 LN

X05 ×

X06 +/-

X07 STO+ Z

X08 DSE i

X09 GTO X

X10 RCL Z

X11 RTN


Size:

LBL A: 12.0 bytes

LBL Z: 12.0 bytes

LBL Y: 12.0 bytes

LBL X: 16.5 bytes

Total: 52.5 bytes


This does not count the 9.5 bytes needed for each variable used (Z, i, A, B, C, etc.). This program takes up to 25 populations (A through Y).



Examples


Example 1:


4 populations (n = 4).


A = 40

B = 30

C = 70

D = 20


Index: 1.2820 (low diversity index)


Example 2:


9 populations (n = 9).


A = 41

B = 36

C = 40

D = 97

E = 64

F = 37

G = 16

H = 18

I = 36


Index: 2.06303 (medium diversity index)



Sources



M. Robert F. The Math Behind Biology Lightning Source4 LLC. LaVergne, TN. 2026. pp. 6-8


Nolan, K.A. and J.E. Callahan. 2006. Beachcomber biology: The Shannon-Weiner Species Diversity Index. Pages 334-338, in Tested Studies for Laboratory Teaching, Volume 27 (M.A. O’Donnell, Editor). Proceedings of the 27th Workshop/Conference of the Association for Biology Laboratory Education (ABLE), 383 pages. 2006. ISBN 1-890444-09-X. Article: https://www.ableweb.org/biologylabs/wp-content/uploads/volumes/vol-27/22_Nolan.pdf. Retrieved May 6, 2026.





Eddie


All original content copyright, © 2011-2026. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.



Python (TI-84 Evo) and HP 32S: Shannon-Wiener Diversity Index

Python (TI-84 Evo) and HP 32S: Shannon-Wiener Diversity Index Introduction The Shannon-Wiener Diversity Index measures a leve...