2.2 Libraries and Clients Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.

Slides:



Advertisements
Similar presentations
Beginning Probability
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
3.6: Probabilities Through Simulations Objective: To simulate probabilities using random number tables and random number generators CHS Statistics.
Review of Basic Probability and Statistics
Descriptive statistics Experiment  Data  Sample Statistics Sample mean Sample variance Normalize sample variance by N-1 Standard deviation goes as square-root.
Using random numbers Simulation: accounts for uncertainty: biology (large number of individuals), physics (large number of particles, quantum mechanics),
Probability Distributions Finite Random Variables.
Descriptive statistics Experiment  Data  Sample Statistics Experiment  Data  Sample Statistics Sample mean Sample mean Sample variance Sample variance.
Programming Assignment 8 CS113 Spring Programming Assignment 8 Implement the sorting routine of your choice. Compare average performance of your.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
© 2013 Pearson Education, Inc. Active Learning Lecture Slides For use with Classroom Response Systems Introductory Statistics: Exploring the World through.
© 2010 Pearson Education Inc.Goldstein/Schneider/Lay/Asmar, CALCULUS AND ITS APPLICATIONS, 12e – Slide 1 of 15 Chapter 12 Probability and Calculus.
CompSci 100E Gambler's Ruin  One approach. Monte Carlo simulation.  Flip digital coins and see what happens. o Pseudorandom number generation ojava.util.Random.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
5-1 Business Statistics: A Decision-Making Approach 8 th Edition Chapter 5 Discrete Probability Distributions.
Binomial Distributions Calculating the Probability of Success.
1.1 Your First Program Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October.
A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
1-1 Copyright © 2015, 2010, 2007 Pearson Education, Inc. Chapter 10, Slide 1 Chapter 10 Understanding Randomness.
2.1 Functions. f x y z f (x, y, z) 3 Functions (Static Methods) Java function. n Takes zero or more input arguments. n Returns one output value. Applications.
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October 21, 2015.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Introduction to Behavioral Statistics Probability, The Binomial Distribution and the Normal Curve.
Multidimensional Arrays. 2 Two Dimensional Arrays Two dimensional arrays. n Table of grades for each student on various exams. n Table of data for each.
Copyright © 2010 Pearson Education, Inc. Chapter 14 From Randomness to Probability.
CompSci 100E 3.1 Random Walks “A drunk man wil l find his way home, but a drunk bird may get lost forever”  – Shizuo Kakutani Suppose you proceed randomly.
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · 9/10/08 9/10/08.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists.
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
EAS31116/B9036: Statistics in Earth & Atmospheric Sciences Lecture 3: Probability Distributions (cont’d) Instructor: Prof. Johnny Luo
40S Applied Math Mr. Knight – Killarney School Slide 1 Unit: Statistics Lesson: ST-5 The Binomial Distribution The Binomial Distribution Learning Outcome.
Essential Statistics Chapter 91 Introducing Probability.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 From Randomness to Probability.
CHAPTER 10 Introducing Probability BPS - 5TH ED.CHAPTER 10 1.
Chapter 10 Introducing Probability BPS - 5th Ed. Chapter 101.
Page 1 Adapted after Dr. Menezes Recurrence 1 …..
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
Overview Of Probability Distribution. Standard Distributions  Learning Objectives  Be familiar with the standard distributions (normal, binomial, and.
Random Variables Ch. 6. Flip a fair coin 4 times. List all the possible outcomes. Let X be the number of heads. A probability model describes the possible.
BPS - 3rd Ed. Chapter 91 Introducing Probability.
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
CompSci 100e2.1 Today’s Topics l Reading:  Sedgewick & Wayne: , l Topics  APTs  Basic Collections: ArrayLists: the expandable array.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CompSci 100e2.1 1 N-Body Simulation l Applications to astrophysics.  Orbits of solar system bodies.  Stellar dynamics at the galactic center.  Stellar.
1 Chapter 10 Probability. Chapter 102 Idea of Probability u Probability is the science of chance behavior u Chance behavior is unpredictable in the short.
CompSci 100E 4.1 Google’s PageRank web site xxx web site yyyy web site a b c d e f g web site pdq pdq.. web site yyyy web site a b c d e f g web site xxx.
The Practice of Statistics, 5th Edition Starnes, Tabor, Yates, Moore Bedford Freeman Worth Publishers CHAPTER 6 Random Variables 6.1: Discrete and Continuous.
Chapter 8: Probability: The Mathematics of Chance Probability Models and Rules 1 Probability Theory  The mathematical description of randomness.  Companies.
Binomial Distributions Chapter 5.3 – Probability Distributions and Predictions Mathematics of Data Management (Nelson) MDM 4U Authors: Gary Greer (with.
2.4 A Case Study: Percolation Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Discrete and Continuous Random Variables
Functions that Deal with Arrays CS 1111 Ryan Layer Feb 23, 2010
AP Statistics: Chapter 7
Exam 2 Review 1.
Basic Practice of Statistics - 3rd Edition Introducing Probability
Data types, assignment statements, and math functions allow you to compute You write a sequence of statements that tell the computer what needs to be done.
Basic Practice of Statistics - 3rd Edition Introducing Probability
AP Statistics Chapter 16 Notes.
Introduction to Computer Science I.
M248: Analyzing data Block A UNIT A3 Modeling Variation.
Essential Statistics Introducing Probability
2.4 A Case Study: Percolation
Basic Practice of Statistics - 5th Edition Introducing Probability
Stacks, Queues, ListNodes
Presentation transcript:

2.2 Libraries and Clients Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December 5, :47 tt

2 Libraries Library. A module whose methods are primarily intended for use by many other programs. Client. Program that calls a library. API. Contract between client and implementation. Implementation. Program that implements the methods in an API.

Random Numbers Jon von Neumann (left), ENIAC (right) The generation of random numbers is far too important to leave to chance. Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin. ” “

4 Standard Random Standard random. Our library to generate pseudo-random numbers.

5 public class StdRandom { // between a and b public static double uniform(double a, double b) { return a + Math.random() * (b-a); } // between 0 and N-1 public static int uniform(int N) { return (int) (Math.random() * N); } // true with probability p public static boolean bernoulli(double p) { return Math.random() < p; } // gaussian with mean = 0, stddev = 1 public static double gaussian() // recall Assignment 0 // gaussian with given mean and stddev public static double gaussian(double mean, double stddev) { return mean + (stddev * gaussian()); }... } Standard Random

6 public class StdRandom {... public static void main(String[] args) { int N = Integer.parseInt(args[0]); double[] t = {.5,.3,.1,.1 }; for (int i = 0; i < N; i++) { StdOut.printf(" %2d ", uniform(100)); StdOut.printf("%8.5f ", uniform(10.0, 99.0)); StdOut.printf("%5b ", bernoulli(.5)); StdOut.printf("%7.5f ", gaussian(9.0,.2)); StdOut.printf("%2d ", discrete(t)); StdOut.println(); } % java StdRandom true false true true false Unit Testing Unit test. Include main() to test each library.

7 Using a Library public class RandomPoints { public static void main(String args[]) { int N = Integer.parseInt(args[0]); for (int i = 0; i < N; i++) { double x = StdRandom.gaussian(0.5, 0.2); double y = StdRandom.gaussian(0.5, 0.2); StdDraw.point(x, y); } % javac RandomPoints.java % java RandomPoints use library name to invoke method

Statistics

9 Standard Statistics Ex. Library to compute statistics on an array of real numbers. meansample variance

10 public class StdStats { public static double max(double[] a) { double max = Double.NEGATIVE_INFINITY; for (int i = 0; i < a.length; i++) if (a[i] > max) max = a[i]; return max; } public static double mean(double[] a) { double sum = 0.0; for (int i = 0; i < a.length; i++) sum = sum + a[i]; return sum / a.length; } public static double stddev(double[] a) // see text } Standard Statistics Ex. Library to compute statistics on an array of real numbers.

Modular Programming

12 Modular Programming Modular programming. n Divide program into self-contained pieces. n Test each piece individually. n Combine pieces to make program. Ex. Flip N coins. How many heads? n Read arguments from user. n Flip one fair coin. n Flip N fair coins and count number of heads. n Repeat simulation, counting number of times each outcome occurs. n Plot histogram of empirical results. n Compare with theoretical predictions.

13 public class Bernoulli { public static int binomial(int N) { int heads = 0; for (int j = 0; j < N; j++) if (StdRandom.bernoulli(0.5)) heads++; return heads; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); int T = Integer.parseInt(args[1]); int[] freq = new int[N+1]; for (int i = 0; i < T; i++) freq[binomial(N)]++; double[] normalized = new double[N+1]; for (int i = 0; i <= N; i++) normalized[i] = (double) freq[i] / T; StdStats.plotBars(normalized); double mean = N / 2.0, stddev = Math.sqrt(N) / 2.0; double[] phi = new double[N+1]; for (int i = 0; i <= N; i++) phi[i] = Gaussian.phi(i, mean, stddev); StdStats.plotLines(phi); } Bernoulli Trials theoretical prediction plot histogram of number of heads perform T trials of N coin flips each flip N fair coins; return # heads

14 Dependency Graph Modular programming. Build relatively complicated program by combining several small, independent, modules.

15 Libraries Why use libraries? n Makes code easier to understand. n Makes code easier to debug. n Makes code easier to maintain and improve. n Makes code easier to reuse.

Extra Slides

17 Discrete Distribution Discrete distribution. Given an array of weights (that sum to 1), choose an index at random with probability equal to its weight. public static int discrete(double[] p) { // check that weights are nonnegative and sum to 1 double r = Math.random(); double sum = 0.0; for (int i = 0; i < p.length; i++) { sum = sum + p[i]; if (sum >= r) return i; } return -1; } something went wrong p = { 0,.1,.1,.1,.1,.1,.5 }