Statistics Toolkit
Build an app that takes a dataset of numbers and calculates mean, median, mode, standard deviation, variance, range, permutations, and combinations.
π― Learning Goals
- βΉ Understand measures of Central Tendency and Dispersion
- βΉ Calculate Variance and SD from raw data
- βΉ Master Combinatorics (Permutation & Combination)
- βΉ Build logic to parse comma-separated text into lists
π Why This Matters
Statistics is how we make sense of the world β from predicting election results to testing new medicines. In the age of 'Big Data', being able to analyze a dataset is one of the most valuable skills you can have.
πUnderstanding Statistics Basics
Theory MasterclassStatistics is the science of collecting, organizing, and analyzing data to draw conclusions. Mean (Average): Add all values and divide by count. Mean = Ξ£x / n Median: The middle value when data is sorted. If even count, average the two middle values. Mode: The value that appears most frequently. A dataset can have no mode, one mode, or multiple modes. Range: Difference between the largest and smallest values. Range = max - min Variance: Measures how spread out the data is. Var = Ξ£(x - mean)Β² / n Standard Deviation: Square root of variance. SD = βVariance Permutation: Number of ways to arrange r items from n items (order matters). P(n,r) = n! / (n-r)! Combination: Number of ways to choose r items from n items (order doesn't matter). C(n,r) = n! / (r! Γ (n-r)!)
Mathematical Foundation
π¨Part A β Designer View (UI Design)
Open MIT App Inventor β Switch to Designer view. Follow each step below to build the interface.
Set up the main screen
Set Screen1 title to "Statistics Toolkit". Dark background with modern styling.
Create data input area
Add a Label: "Enter numbers separated by commas" Add a TextBox named DataInput with Hint "e.g., 5, 12, 7, 3, 9, 12" Set MultiLine = true and Width = Fill Parent. This lets users enter an entire dataset at once.
Add calculation buttons
Add rows of buttons using HorizontalArrangements: Row 1: "Mean", "Median", "Mode" Row 2: "Std Dev", "Variance", "Range" Row 3: "Calculate All" The "Calculate All" button runs every calculation and displays all results.
Create Permutation/Combination section
Add a separator Label: "β Permutation & Combination β" Add a HorizontalArrangement: TextBox for n, Label "P/C", TextBox for r Add 2 Buttons: "nPr" and "nCr"
Create result display
Add a Label: "Results" as header. Add ResultLabel with FontSize 16 and MultiLine-like display. This shows formatted output of all statistical measures.
π§©Part B β Blocks View (Logic & Calculation)
Switch to Blocks view. Now add the logic that makes your app actually work.
Parse the comma-separated input into a list
When any calculate button is clicked: 1. Get the text from DataInput 2. Use "split text at" block with separator "," 3. This creates a list of text strings 4. Loop through the list and convert each to a number 5. Store in a global list variable called "dataList" This is the most important step β all calculations use this list.
Calculate Mean
Create a procedure "calculateMean": 1. Initialize sum = 0 2. Use "for each item in list" loop 3. Add each item to sum 4. mean = sum / length(dataList) Display: "Mean = " + mean
Calculate Median
Create a procedure "calculateMedian": 1. Sort the list (use a simple bubble sort with nested loops) 2. Find the middle index: mid = length(list) / 2 3. If length is odd: median = item at position (mid + 1) 4. If length is even: median = (item at mid + item at mid+1) / 2 Tip: To sort in App Inventor, you need to manually implement sorting using loops and swapping.
Calculate Mode
Create a procedure "calculateMode": 1. Create two lists: "uniqueValues" and "frequencies" 2. Loop through dataList 3. For each value, check if it's already in uniqueValues 4. If yes, increment its frequency. If no, add it with frequency 1. 5. Find the maximum frequency 6. The value(s) with maximum frequency = mode
Calculate Variance and Standard Deviation
Create a procedure "calculateVariance": 1. First calculate the mean 2. Initialize sumSquaredDiff = 0 3. Loop through dataList: diff = item - mean sumSquaredDiff = sumSquaredDiff + (diff Γ diff) 4. variance = sumSquaredDiff / length(dataList) 5. stdDev = sqrt(variance)
Build the Factorial helper for P and C
Create a procedure "factorial" with parameter n: 1. If n <= 1, return 1 2. Else: result = 1, loop from 1 to n, multiply result by each number 3. Return result This is needed for nPr and nCr calculations.
Calculate nPr and nCr
When nPrButton.Click: P = factorial(n) / factorial(n - r) When nCrButton.Click: C = factorial(n) / (factorial(r) Γ factorial(n - r)) Show error if r > n: "r cannot be greater than n!"
Build 'Calculate All' button
When CalculateAllButton.Click: Run all procedures and format the output: "Mean: 7.5 Median: 7 Mode: 12 Range: 9 Variance: 10.25 Std Dev: 3.2"
π§ͺTesting Your App
- βTest with dataset: 1, 2, 3, 4, 5 β Mean=3, Median=3, no mode
- βTest with dataset: 2, 2, 3, 4 β Mode=2
- βTest: 5P2 = 20, 5C2 = 10
- βTest with a single number β Mean = Median = Mode = that number
- βTest with negative numbers: -3, -1, 0, 2, 5
πBonus Challenges
Extra credit β impress your instructor
- β Add a bar chart visualization using Canvas component
- β Support entering data by clicking 'Add' button instead of commas
- β Add quartile calculations (Q1, Q2, Q3) and IQR
- β Show a frequency distribution table for the dataset