Statistics Function Implementation

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Rolando V. RaqueñoWednesday, June 10, 2015 Statistics Function Implementation Traditional Programming Approach.
Computer Science 1620 Loops.
Programming Cheng-Ling Kuo
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Rolando V. RaqueñoSaturday, June 27, Quiz #6 Topics cut paste sort tail head.
Quiz #6 Topics cut paste sort tail head. Debugging in IDL.
Understanding the Mainline Logical Flow Through a Program (continued)
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Python quick start guide
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Homework Reading Programming Assignments
High-Level Programming Languages: C++
1 Chapter 9 Writing, Testing, and Debugging Access Applications.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Rolando V. RaqueñoWednesday, October 21, Special Function Implementation in IDL A Case Study.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Making friends with IDL IDL is an interpreted rather than a compiled language. This means that large IDL programs can execute less rapidly than equivalent.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
Allegro CL Certification Program Lisp Programming Series Level I Session Basic Lisp Development in the IDE.
Python Functions.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Lab 3 + Using the Terminal 1. "Under Linux there are GUIs (graphical user interfaces). where you can point and click and drag, and hopefully get work.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Navigation and Ancillary Information Facility NIF Using Module Headers April 2006.
The Hashemite University Computer Engineering Department
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Functions Functions, locals, parameters, and separate compilation.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Chapter 9: Value-Returning Functions
CMPT 120 Topic: Python’s building blocks -> More Statements
Review 1.
User-Written Functions
Basic IDL Commands SIMG 726 Winter Quarter 2005 Rolando V. Raqueño.
ESC101: Introduction to Computing
Key Ideas from day 1 slides
Chapter 2 Assignment and Interactive Input
RCS Revision Control System
Chapter 4 MATLAB Programming
Basic operations in Matlab
Functions CIS 40 – Introduction to Programming in Python
Scripts & Functions Scripts and functions are contained in .m-files
Lecture 07 More Repetition Richard Gesick.
Microsoft Access Illustrated
Computer Science 101 While Statement.
Lecture 4B More Repetition Richard Gesick
What is Bash Shell Scripting?
Week 8 - Programming II Today – more features: Loop control
Python Primer 2: Functions and Control Flow
Use of Mathematics using Technology (Maltlab)
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
Loop Statements & Vectorizing Code
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Homework Reading Programming Assignments Finish K&R Chapter 1
Loop Statements & Vectorizing Code
Input and Output Python3 Beginner #3.
Perl Programming Dr Claire Lambert
Presentation transcript:

Statistics Function Implementation Traditional Programming Approach Winter Quarter 2005 Rolando V. Raqueño

minimum.pro function minimum, x n = n_elements(x) answer = x(0) for i=1L, n-1 do begin if( answer gt x(i) ) then $ answer = x(i) endfor return, answer end Winter Quarter 2005 Rolando V. Raqueño

sum.pro function sum, x n = n_elements( x ) answer = 0 for i = 0, n-1 do begin answer = answer + x(i) endfor return, answer end Winter Quarter 2005 Rolando V. Raqueño

Overflow Problems What if the array is an integer array? Sums will have a maximum limit based on the number of bits specified by the integer type. Winter Quarter 2005 Rolando V. Raqueño

Corrected sum.pro function sum, x n = n_elements( x ) answer = 0.0D for i = 0L, n-1 do begin answer = answer + x(i) endfor return, answer end Winter Quarter 2005 Rolando V. Raqueño

mean.pro function mean, x n = n_elements(x) answer = sum(x) / n return, answer end Winter Quarter 2005 Rolando V. Raqueño

Integer Problems IDL> b=9/5 IDL> print,b 1 Winter Quarter 2005 Rolando V. Raqueño

Integer Problems IDL> b=9/double(5) IDL> print,b 1.8000000 Winter Quarter 2005 Rolando V. Raqueño

Corrected mean.pro function mean, x n = n_elements(x) answer = sum(x) / double(n) return, answer end Winter Quarter 2005 Rolando V. Raqueño

sum_squares.pro function sum_squares, x squares = x^2.0 answer= sum( squares ) return, answer end Winter Quarter 2005 Rolando V. Raqueño

variance.pro function variance, x n = double(n_elements(x)) answer=(sum_squares(x)-(sum(x)^2.0/n))/ (n-1) return, answer end Winter Quarter 2005 Rolando V. Raqueño

sd.pro function sd, x answer = sqrt(variance(x)) return, answer end Winter Quarter 2005 Rolando V. Raqueño

Debugging in IDL Winter Quarter 2005 Rolando V. Raqueño

Basic IDL Debugging Commands breakpoint .step (.s) .stepover (.so) .continue .return .out Winter Quarter 2005 Rolando V. Raqueño

mean.pro 10 ;- 11 ; 12 function mean,x 13 n = n_elements(x) 10 ;- 11 ; 12 function mean,x 13 n = n_elements(x) 14 answer = sum(x)/double(n) 15 return, answer 16 end Winter Quarter 2005 Rolando V. Raqueño

Set/Clear Breakpoint Winter Quarter 2005 Rolando V. Raqueño

Enable/Disable Breakpoint Winter Quarter 2005 Rolando V. Raqueño

Edit breakpoints Winter Quarter 2005 Rolando V. Raqueño

Breakpoints IDL> breakpoint, ‘mean.pro’, 12 IDL> help,/breakpoint Breakpoints: Index Module Line File ----- ------ ---- ---- 0 MEAN 12 mean.pro IDL> breakpoint, /clear, 0 OR IDL> breakpoint, /clear, ‘mean.pro’, 12 Winter Quarter 2005 Rolando V. Raqueño

.step Winter Quarter 2005 Rolando V. Raqueño

.step Used after a breakpoint is reached Single step through commands If statement is a function or procedure Will enter into it After a breakpoint is reached, you can single step through each statement using the .step command If the next statement is a function or procedure, the .step command will enter that module and allow you to step through each statement Winter Quarter 2005 Rolando V. Raqueño

.stepover Winter Quarter 2005 Rolando V. Raqueño

.stepover Similar to .step Executes functions and procedures to completion without stopping inside the function Winter Quarter 2005 Rolando V. Raqueño

.out Winter Quarter 2005 Rolando V. Raqueño

.out Continues execution out of the current routine. Useful in leaving a routine that you stepped into, but have determined that all is working properly and want to return to the calling routine. Winter Quarter 2005 Rolando V. Raqueño

.return sqrt(variance(x)) Continues execution until a return statement is encountered. Useful in checking the return value of an inner (nested) function in a function composition e.g. sqrt(variance(x)) Winter Quarter 2005 Rolando V. Raqueño

mean.pro 10 ;- 11 ; 12 function mean,x 13 n = n_elements(x) 10 ;- 11 ; 12 function mean,x 13 n = n_elements(x) 14 answer = sum(x)/double(n) 15 return, answer 16 end Winter Quarter 2005 Rolando V. Raqueño

Navigating the stack Winter Quarter 2005 Rolando V. Raqueño

Internally Documenting Your IDL Routines Winter Quarter 2005 Rolando V. Raqueño

IDL_PATH environment variable setenv IDL_DIR /cis/common/rsi/idl_5 setenv IDL_PATH \+$IDL_DIR/lib:\+$IDL_DIR/examples: \+/dirs/common/rsi/idl_5:\+/dirs/common/lib/idl :~/lib/idl Winter Quarter 2005 Rolando V. Raqueño

Use the following template for IDL documentation /cis/common/rsi/idl_5.5/examples/template.pro ;+ ; NAME: ; PURPOSE: ; INPUTS: ; OPTIONAL INPUTS: ; KEYWORD PARAMETERS: ; OUTPUTS: ; OPTIONAL OUTPUTS: ; PROCEDURE: ; EXAMPLE: ; MODIFICATION HISTORY: ; Written by: Your name here, Date. ; July, 1994 Any additional mods get described here. ;- Winter Quarter 2005 Rolando V. Raqueño

doc_library example ;+ ; The following routine computes the mean ; of an arbitrary sized array ; ; $Header$ ; $Log$ ;- function mean,x ... Winter Quarter 2005 Rolando V. Raqueño

Executing doc_library IDL> doc_library Name of procedure or * for all: mean Enter 1 for printer, 0 for terminal: 0 ----- Documentation for /cis/staff/rvrpci/lib/idl/statistics/mean.pro ----- The following routine computes the mean of an arbitrary sized array Winter Quarter 2005 Rolando V. Raqueño

Executing doc_library $Header: /nfs/cis/staff/rvrpci/lib/idl/statistics/RCS/mean.pro,v 1.1 1998/01/13 15:44:47 rvrpci Exp $ $Log: mean.pro,v $ Revision 1.1 1998/01/13 15:44:47 rvrpci Initial revision Winter Quarter 2005 Rolando V. Raqueño

More updated help feature mk_html_help routine Comment your routines in a given directory using the template describe previously Give the mk_html_help command with agruments for directory of routines to convert to name of html file IDL> mk_html_help,’.’,’stats.html’ Winter Quarter 2005 Rolando V. Raqueño

Winter Quarter 2005 Rolando V. Raqueño

Figuring out where to optimize your code Code Profiling in IDL Figuring out where to optimize your code Winter Quarter 2005 Rolando V. Raqueño

PROFILER Command in IDL Allows you to gather statistics where your program is spending the most time Guides you to the routine that needs the most optimization Winter Quarter 2005 Rolando V. Raqueño

Let’s take the sum example ; Scalar Approach function sum, x n = n_elements(x) answer = x[0] for i=1L, n-1 do begin answer = answer + x[i] endfor return, answer end ; Vector Approach function sum, x n = n_elements(x) a = reform(x,n) answer = a ## transpose(replicate(1.0, n)) return, answer end Winter Quarter 2005 Rolando V. Raqueño

Sample test program for profile analysis pro test_profiler a = findgen(100) for i = 1, 100 do begin print, i s = sum( a ) s2 = sum_squares( a ) m = mean( a ) v = variance( a ) sd = standard_deviation( a ) endfor end Winter Quarter 2005 Rolando V. Raqueño

sum.pro function sum, x n = n_elements( x ) answer = 0.0 for i = 0L, n-1 do begin answer = answer + x(i) endfor return, answer end Winter Quarter 2005 Rolando V. Raqueño

sum.pro function sum, x n = n_elements( x ) a = reform( x, n) answer=a##transpose(replicate(1.0,n)) return, answer end Winter Quarter 2005 Rolando V. Raqueño

Using Matrix Multiplication reform ## = transpose(replicate(1.0,n)) Winter Quarter 2005 Rolando V. Raqueño

sum_squares.pro function sum_squares, x squares = x^2.0 answer= sum( squares ) return, answer end Winter Quarter 2005 Rolando V. Raqueño

sum_squares.pro function sum_squares, x n = n_elements( x ) a = reform( x, n) answer = a ## transpose(a) return, answer end Winter Quarter 2005 Rolando V. Raqueño

Using Matrix Multiplication reform ## = Winter Quarter 2005 Rolando V. Raqueño

To Profile Your Code Compile (and run) all the routines you want to analyze At the IDL prompt (or through the GUI), invoke the profiler IDL> profiler Run your code Output the report using IDL> profiler, /report Winter Quarter 2005 Rolando V. Raqueño

Interpreting the Profile report MODULE Name of routine TYPE U = user-defined function S = system-defined function COUNT Number of times a function has been called since the invocation of the “profiler” command Winter Quarter 2005 Rolando V. Raqueño

ONLY Time Statistic in Profile Report Time spent in the function ONLY Calls made to other functions from within this function are not part of this value AVERAGE Average of the time spent ONLY in this function for all the calls made so far Winter Quarter 2005 Rolando V. Raqueño

TIME Statistic in Profile Report Cumulative time spent in a given routine including subsequent calls to other functions AVERAGE Average of TIME for all the calls made so far Winter Quarter 2005 Rolando V. Raqueño

Result of initial profile IDL> profiler,/report Module Type Count Only(s) Avg.(s) Time(s) Avg.(s) MEAN (U) 100 0.003368 0.000034 0.046592 0.000466 STANDARD_DEVIATION (U) 100 0.001531 0.000015 0.100819 0.001008 SUM (U) 700 0.313726 0.000448 0.313726 0.000448 SUM_SQUARES (U) 300 0.010265 0.000034 0.141342 0.000471 TEST_PROFILER (U) 1 0.247449 0.247449 0.581684 0.581684 VARIANCE (U) 200 0.005345 0.000027 0.208235 0.001041 Winter Quarter 2005 Rolando V. Raqueño

Result of vector sum routine IDL> profiler,/report Module Type Count Only(s) Avg.(s) Time(s) Avg.(s) MEAN (U) 100 0.002323 0.000023 0.008024 0.000080 STANDARD_DEVIATION (U) 100 0.002149 0.000021 0.036169 0.000362 SUM (U) 700 0.056344 0.000080 0.056344 0.000080 SUM_SQUARES (U) 300 0.011034 0.000037 0.043131 0.000144 TEST_PROFILER (U) 1 0.036511 0.036511 0.115969 0.115969 VARIANCE (U) 200 0.007608 0.000038 0.052291 0.000261 SCALAR RESULTS SUM (U) 700 0.313726 0.000448 0.313726 0.000448 Winter Quarter 2005 Rolando V. Raqueño

Let’s take the sum_squares example ; Scalar Approach function sum_squares, x answer = sum(x^2) return, answer end ; Vector Approach function sum_squares, x n = n_elements(x) a = reform(x,n) answer = a ## transpose(a) return, answer end Winter Quarter 2005 Rolando V. Raqueño

Profile report of vectorized sum_squares function IDL> profiler,/report Module Type Count Only(s) Avg.(s) Time(s) Avg.(s) MEAN (U) 100 0.002285 0.000023 0.007718 0.000077 STANDARD_DEVIATION (U) 100 0.002084 0.000021 0.016103 0.000161 SUM (U) 400 0.024090 0.000060 0.024090 0.000060 SUM_SQUARES (U) 300 0.013434 0.000045 0.013434 0.000045 TEST_PROFILER (U) 1 0.099387 0.099387 0.149792 0.149792 VARIANCE (U) 200 0.008513 0.000043 0.028404 0.000142 SCALAR RESULTS SUM_SQUARES (U) 300 0.011034 0.000037 0.043131 0.000144 Winter Quarter 2005 Rolando V. Raqueño

Use this as an exercise to become familiar with profiling techniques What is the performance difference between the scalar and vector implementation of RECT? Use this as an exercise to become familiar with profiling techniques Winter Quarter 2005 Rolando V. Raqueño

Profile the Rect Function VECTOR FORM function rect, x answer=x ax=abs(x) gt_half=where(ax gt 0.5,n_gt) eq_half=where(ax eq 0.5,n_eq) lt_half=where(ax lt 0.5,n_lt) if (n_gt ne 0) then answer(gt_half)=0.0 if (n_eq ne 0) then answer(eq_half)=0.5 if (n_lt ne 0) then answer(lt_half)=1.0 return, answer end SCALAR FORM function rect, x, x0, b a = abs( (x-x0)/b ) if(a gt 0.5)then return, 0.0 if(a lt 0.5)then return, 1.0 if(a eq 0.5)then return, 0.5 end Winter Quarter 2005 Rolando V. Raqueño