Statistical Computing STA 231 1 UNIVERSITY OF IBADAN LIBRARY Copyright © 2002, Revised in 2016 by Distance Leinagr nCentre, University of Ibadan, Ibadan. All rights reserved. No part of this publication ym bae reproduced, stored in a retrieval System, or transmitted in any form or by any me aenlse,ctronic, mechanical, photocopying, recording or otherwise, without threio rp permission of the copyright owner. ISBN 978-021 General Edito:r Prof. Bayo Okunade Error! Use the Home tab to apply Guide Publishing nI stitute to the text that you want to appear here. University of Ibadan, Nigeria Telex: 31128NG Tel: +234 (80775935727) E-mail: ssu@dlc.ui.edu.ng Website: www.dlc.ui.edu.ng 2 UNIVERSITY OF IBADAN LIBRARY Vice-Chancellor’s Message The Distance Learning Centre is building on a s otrlaiddition of over two decades of service in the provision of External Studies Promgmrae and now Distance Learning Education in Nigeria and beyond. The Distance Lienagr nmode to which we are committed is providing access to many deservinge Nriaigns in having access to higher education especially those who by the nature oirf tehnegagement do not have the luxury of full time education. Recently, it is contribugti nin no small measure to providing places for teeming Nigerian youths who for one reasonh oer otther could not get admission into the conventional universities. These course materials have been written by w risteprescially trained in ODL course delivery. The writers have made great efforts to vpidre up to date information, knowledge and skills in the different disciplinensd a ensure that the materials are user- friendly. In addition to provision of course materials in npt rai nd e-format, a lot of Information Technology input has also gone into the deploymoef ncto urse materials. Most of them can be downloaded from the DLC website and arel aabvlaei in audio format which you can also download into your mobile phones, IPod,3 MaPmong other devices to allow you listen to the audio study sessions. Some o fs tuhdey session materials have been scripted and are being broadcast on the univesrs iDtyia’ mond Radio FM 101.1, while others have been delivered and captured in audsiuoa-vl i format in a classroom environment for use by our students. Detailed imnfaotrion on availability and access is available on the website. We will continue in ouffro rets to provide and review course materials for our courses. However, for you to take advantage of these for,m yaotus will need to improve on your I.T. skills and develop requisite distance learn Cinuglture. It is well known that, for efficient and effective provision of Distance leianrgn education, availability of appropriate and relevant course materials sisin ea qua no.n So also, is the availability of multiple plat form for the convenience of our stnutdse. It is in fulfilment of this, that series of course materials are being written tob len oaur students study at their own pace and convenience. It is our hope that you will put these course mialtse rto the best use. Prof. Abel Idowu Olayinka Vice-Chancellor 3 UNIVERSITY OF IBADAN LIBRARY Foreword As part of its vision of providing education fo“Lr iberty and Development” for Nigerians and the International Community, the Uenrsivity of Ibadan, Distance Learning Centre has recently embarked on a vigorous repoonsinitgi agenda which aimed at embracing a holistic and all encompassing approtoa cthe delivery of its Open Distance Learning (ODL) programmes. Thus we are committe gdl otobal best practices in distance learning provision. Apart from providing an efficnite administrative and academic support for our students, we are committed to pdrionvgi educational resource materials for the use of our students. We are convinced wthiatht,out an up-to-date, learner-friendly and distance learning compliant course materihaelsr,e t cannot be any basis to lay claim to being a provider of distance learning educa tiIonnd.eed, availability of appropriate course materials in multiple formats is the hub aonfy distance learning provision worldwide. In view of the above, we are vigorously pursuin ga amsatter of priority, the provision of credible, learner-friendly and interactive coursea temrials for all our courses. We commissioned the authoring of, and review of co umrsaeterials to teams of experts and their outputs were subjected to rigorous peer rwe vtoie ensure standard. The approach not only emphasizes cognitive knowledge, but also ss kainlld humane values which are at the core of education, even in an ICT age. The development of the materials which is on-goainlsgo had input from experienced editors and illustrators who have ensured that tahreey accurate, current and learner- friendly. They are specially written with distanlceea rners in mind. This is very important because, distance learning involves non-reside snttuiadlents who can often feel isolated from the community of learners. It is important to note that, for a distance lear rtnoe excel there is the need to source and read relevant materials apart from this course rmiaal.t e Therefore, adequate supplementary reading materials as well as othfoerm ination sources are suggested in the course materials. Apart from the responsibility for you to read thciosu rse material with others, you are also advised to seek assistance from your course faatcoirlist especially academic advisors during your study even before the interactive soens swihich is by design for revision. Your academic advisors will assist you using conievnetn technology including Google Hang Out, You Tube, Talk Fusion, etc. but you htaov tea ke advantage of these. It is also going to be of immense advantage if you complesteig ansments as at when due so as to have necessary feedbacks as a guide. The implication of the above is that, a distancaer nler has a responsibility to develop requisite distance learning culture which includdeilsig ent and disciplined self-study, seeking available administrative and academic srut ppaond acquisition of basic information technology skills. This is why you aernec ouraged to develop your computer 4 UNIVERSITY OF IBADAN LIBRARY skills by availing yourself the opportunity of tnrainig that the Centre’s provide and put these into use. In conclusion, it is envisaged that the course mriaalste would also be useful for the regular students of tertiary institutions in Nigae rwi ho are faced with a dearth of high quality textbooks. We are therefore, delighted rteos pent these titles to both our distance learning students and the university’s regular esntutsd. We are confident that the materials will be an invaluable resource to all. We would like to thank all our authors, reviewernsd aproduction staff for the high quality of work. Best wishes. Professor Bayo Okunade Director 5 UNIVERSITY OF IBADAN LIBRARY Course Development Team Content Authoring Udomboso, Christopher Godwin Content Editor Prof. Remi Raji-Oyelade Production Editor Ogundele Olumuyiwa Caleb Learning Design/Assessment Authoring Folajimi Olambo Fakoya Managing Editor Ogunmefun Oladele Abiodun General Editor Prof. Bayo Okunade 6 UNIVERSITY OF IBADAN LIBRARY Table of Contents Study Session 1: General Computing ...................................................................................... 12 Introduction ..................................................................................................................... 12 Learning Outcomes from Study Session 1.................. ...................................................... 12 1.1 Brief History of the Computer .................................................................................. 12 In-Text Question .............................................................................................................. 14 In-Text Answer ........................................................................................................... 14 1.1.1 Definition of Computer ...................................................................................... 14 1.1.2 Generations of Computers ............................................................................... 15 1.2 Types of Computers ....................................................................................................... 16 In-Text Question .............................................................................................................. 18 In-Text Answer ........................................................................................................... 18 1.2.1 Components of the Computer .............................................................................. 19 In-Text Question .............................................................................................................. 20 In-Text Answer ........................................................................................................... 20 Input .................................................................................................................................... 21 1.2.2 Introduction to Operating System ........................................................................... 22 1.3 Implications of Computerization ................................................................................... 22 Summary ........................................................................................................................ 24 Self-Assessment Questions (SAQs) for study ses1s .i.o..n.. ................................................ 24 Study Session 2: Computer in Statistics Profes.s..i.o..n.. ........................................................ 26 Introduction ..................................................................................................................... 26 Learning Outcomes from Study Session 2.................. ...................................................... 26 2.1 The Growth of Statistics and Computational Dcesv i.................................................. 26 In-Text Question .............................................................................................................. 29 In-Text Answer ........................................................................................................... 29 2.2 Programing Techniques for Statistics ..................................................................... 29 Summary for 2 ..................................................................................................................... 30 Self-Assessment Questions (SAQs) for study ses2s .i.o..n.. ................................................ 31 Study Session 3: Representation of Data in the Cuotemr p........................................................ 32 Introduction ..................................................................................................................... 32 Learning Outcomes from Study Session 3.................. ...................................................... 32 7 UNIVERSITY OF IBADAN LIBRARY 3.1 Binary Representation .................................................................................................... 32 3.1.1 Converting from Decimal to Binary ................................................................... 34 3.1.2 Converting from Binary to Decimal ................................................................... 34 3.2 Simple Computer Arithmetic ......................................................................................... 35 Summary for 3 ..................................................................................................................... 40 Self-Assessment Questions (SAQs) for study ses3s .i.o..n.. ................................................ 40 Study Session 4: Introduction to Microsoft Exc.e..l. ............................................................. 42 Introduction ..................................................................................................................... 42 Learning Outcomes from Study Session 4.................. ...................................................... 42 4.1 Spreadsheet ............................................................................................................. 42 4.1.1 Simple Operations ................................................................................................... 43 In-Text Question .............................................................................................................. 43 In-Text Answer ........................................................................................................... 43 4.1.2 Selecting a Cell or Multiple Cells ........................................................................ 44 4.2 Types of Sheet in MS Excel........................................................................................... 44 In-Text Question .............................................................................................................. 45 In-Text Answer ........................................................................................................... 45 Summary for 4 ..................................................................................................................... 46 Self-Assessment Questions (SAQs) for study ses4s .i.o..n.. ................................................ 46 Study Session 5: Data Entry in Microsoft Exce.l. ............................................................... 48 Introduction ..................................................................................................................... 48 Learning Outcomes from Study Session 5.................. ...................................................... 48 5.1 Types of Data ........................................................................................................... 48 In-Text Question .............................................................................................................. 50 In-Text Answer ........................................................................................................... 50 5.2 Data Entry Techniques ........................... ................................................................... 50 5.2.1 Using Autofill ..................................................................................................... 51 In-Text Question .............................................................................................................. 52 In-Text Answer ........................................................................................................... 52 5.3 Creating Simple Trends and Forecasts .......................................................................... 53 Summary for 5 ..................................................................................................................... 54 Self-Assessment Questions (SAQs) for study ses5s .i.o..n.. ................................................ 54 Study Session 6: Data Analysis using Microsoft El .x..c..e...................................................... 56 Introduction ..................................................................................................................... 56 8 UNIVERSITY OF IBADAN LIBRARY Learning Outcomes from Study Session 6.................. ...................................................... 56 6.1 General Rule on Computation in MS Excel ........................................................... 56 In-Text Question .............................................................................................................. 58 In-Text Answer ........................................................................................................... 58 Summary for 6 ..................................................................................................................... 59 Self-Assessment Questions (SAQs) for study ses6s .i.o..n.. ................................................ 60 Study Session 7: Using the Function and Chart Wdsiz .a..r.......................................................... 62 Introduction ..................................................................................................................... 62 Learning Outcomes from Study Session 7.................. ...................................................... 62 7.1 Using Functions ....................................................................................................... 62 In-Text Question .............................................................................................................. 64 In-Text Answer ........................................................................................................... 64 7.1.1 Statistical Function................................................................................................ 67 In-Text Question .............................................................................................................. 69 In-Text Answer ........................................................................................................... 69 7.1.2 Using Charts...................................................................................................... 70 Summary for 7 ..................................................................................................................... 71 Self-Assessment Questions (SAQs) for study ses7s .i.o..n.. ................................................ 71 Study Session 8: Algorithm and Flow Chart ........................................................................... 74 Introduction ..................................................................................................................... 74 Learning Outcomes from Study Session 8.................. ...................................................... 74 8.1 Algorithm ..................................................................................................................... 75 In-Text Question .............................................................................................................. 76 In-Text Answer ........................................................................................................... 76 8.2 Flowchart ....................................................................................................................... 76 8.2.1 Flowchart Symbols ........................................................................................... 77 Summary for 8 ..................................................................................................................... 80 Self-Assessment Questions (SAQs) for study ses8s .i.o..n.. ................................................ 80 Study Session 9: Review of the BASIC Programminngg Luaage...........................................8..2 Introduction ..................................................................................................................... 82 Learning Outcomes from Study Session 9.................. ...................................................... 82 9.1 The BASIC Program ................................................................................................ 83 Summary for 9 ..................................................................................................................... 90 Self-Assessment Questions (SAQs) for study ses9s .i.o..n.. ................................................ 90 9 UNIVERSITY OF IBADAN LIBRARY Study Session 10: Descriptive Statistics .......................................................................... 92 Introduction ..................................................................................................................... 92 Learning Outcomes from Study Session 10................................................................. 92 10.1 Rounding of Numerical Data ................................................................................. 93 10.1.1 Error ...................................................................................................................... 95 In-Text Question .............................................................................................................. 95 In-Text Answer ........................................................................................................... 96 10.1.2 Ratios and Percentages ......................................................................................... 97 10.1.3 The Median ................................................................................................... 104 In-Text Question ............................................................................................................ 106 In-Text Answer ......................................................................................................... 106 Summary for 10 ........................................................................................................... 117 Self-Assessment Questions (SAQs) for study ses1s0io ..n.. ................................................ 117 Study Session 11: Probability Theory................................................................................... 120 Introduction ................................................................................................................... 120 Learning Outcomes from Study Session 11............................................................... 120 11.1 Set Theory ............................................................................................................ 121 In-Text Question ............................................................................................................ 122 In-Text Answer ......................................................................................................... 122 11.2 Mutually Exclusive Events ........................................................................................ 130 Summary for 11 ........................................................................................................... 136 Self-Assessment Questions (SAQs) for study ses1s1io ..n.. ................................................ 136 Study Session 12: Probability Distribution Funcsti o..n.............................................................. 138 Introduction ................................................................................................................... 138 Learning Outcomes from Study Session 12............................................................... 138 12.1 Probability Function (The Discreet Case)............................................................ 139 Summary for 12 ........................................................................................................... 156 Self-Assessment Questions (SAQs) for study ses1s2io ..n.. ................................................ 156 Study Session 13: Correlation and Linear Regre s..s..i.o..n.................................................... 159 Introduction ................................................................................................................... 159 Learning Outcomes from Study Session 13............................................................... 159 13.1 The Theory of Correlation ................................................................................... 160 13.2 Coefficient of Correlation ..................................................................................... 161 In-Text Question ............................................................................................................ 165 10 UNIVERSITY OF IBADAN LIBRARY In-Text Answer ......................................................................................................... 165 Summary for 13 ........................................................................................................... 169 Self-Assessment Questions (SAQs) for study ses1s3io ..n.. ................................................ 169 Study Session 14: Elementary Time Series Anal.y.s..i.s.. .................................................... 171 Introduction ................................................................................................................... 171 Learning Outcomes from Study Session 14............................................................... 171 14.1 Components of a Time Series .................................................................................... 171 In-Text Question ............................................................................................................ 172 In-Text Answer ......................................................................................................... 172 14.1.1 Models of Time Series ........................................................................................ 172 Summary of 14 ............................................................................................................. 176 Self-Assessment Questions (SAQs) for study ses1s4io ..n.. ................................................ 177 Study Session 15: Statistical Tests and ConfideInntceerv als ............................................... 180 Introduction ................................................................................................................... 180 Learning Outcomes from Study Session 15............................................................... 180 15.1 Point and Interval Estimates ............................................................................... 181 15.2 Hypothesis Testing..................................................................................................... 190 Summary of 15 ............................................................................................................. 192 Self-Assessment Questions (SAQs) for study ses1s5io ..n.. ................................................ 192 Study Session 16: Introduction to MATLAB ................................................................... 194 Introduction ................................................................................................................... 194 Learning Outcomes from Study Session 16............................................................... 194 16.1 MATLAB ............................................................................................................... 195 Summary of 16 ............................................................................................................. 217 Self-Assessment Questions (SAQs) for study ses1s6io ..n.. ................................................ 217 11 UNIVERSITY OF IBADAN LIBRARY Study Session 1: General Computing Introduction The Computer has become an indispensable paret ohfu tmh an life which has literally phased out many jobs which were hitherto done by severeanl .m The relevance of computer in almost every human activity cannot be overemphda.s i ze It is also important to know how the computer camtoe be, the generations, types, configuration, and the likes. The statistics psrosfioen has become, in the last 50 to 60 years, a major player in the computing world. Learning Outcomes from Study Session 1 At the end of this study session, you should be atob:l 1.1 Concept of a modern computer; 1.2 Types of computers; 1.3 Identify the implications of computers in everyo fperssion. 1.1 Brief History of the Computer Data and information are so important to human gbse ibnecause they are needed for decision making. The human brain is equipped to file antdri ervee data and information. Billions of such activities are carried out every day. While data are raw facts that are almost uselte shsa,s i to be processed before it can make any meaning. These processed data is called informn.a t ioThe brain performs numerous processes everyday. 12 UNIVERSITY OF IBADAN LIBRARY Due to its limitations (for example, stress dueo vtoe rload of data and information), it is not sufficient to rely only on it (the brain). Thiso burght about the art of recording. Recording is writing or drawing ‘something’ on a physical obj escot as to reduce load on the brain and enhance memory. Prehistoric cave dwellers painted pictures on w oafl lscaves, while the ancient Egyptians wrote on a crude form of paper called papyrus. STuhme erians, around 3000B.C, developed a device for representing numbers by use of stoinn eas b ox, which the Chinese, about 1000 B.C, improved upon by tying stones on strings wino ao den frame. The device was called Abacus, so named after thinee Cseh name for box, baccus. Abacus remained a powerful mathematical tool especiallry bfuosiness for several centuries. The Europeans also tried by trying to simplify comptlaesxk s into simple ones. Figure1.1: Abacus Computer. Source: http://mmebsabacus.blogspot.com.ng/2015/04/whact-tleyx-ias-abacus.htm l During the industrial revolution in 1804, a Frencahnm named Josef Marie Jacquard developed a device that could control the operas tiofn the weaving loom. He used cards with holes punched into them at appropriate locna, twiohich could program a textile machine (or weaving loom) to weave specific patterns wpithe csific texture using specific colours. His machine was a mechanical device called Jacquarvdi nwge laoom. His machine had three units. These are input purnoitc,essing unit and output unit. In 1842, an English mathematician named Charles Babbagete ds tawrork on a calculating device 13 UNIVERSITY OF IBADAN LIBRARY called Difference Engine, which he did not comp lebteefore abandoning it to work on a general – purpose digital calculating machine cda Allenalytical Engine. In-Text Question …………..remained a powerful mathematical tool esplelyc iafor business for several centuries. In-Text Answer Abacus He adapted the idea of punched cards to this mea.c h Winorking closely with him was a woman named Augusta Ada Byron, nicknamed ‘Lady Llaocve’. She wrote programs that made Babbage’s machine to work. This machine hlla tdh ea units of the Jacquard weaving loom, i.e. input unit, processing unit and the ouut tupnit. The major difference was that Babbage’s machine used electricity, thus makinagn iet lectronic device. This machine marked the beginning of modern elencictr ocomputer, and is therefore called the ‘father of modern computer’. At this point wcaen now define a computer. 1.1.1 Definition of Computer A computer is an electronic device that acceptas fdraotm the input unit, processes the data in the processing unit according to the instructiovne ng,i and produces a result in response to format specification through an output unit. 14 UNIVERSITY OF IBADAN LIBRARY Figure 1.2: Computer Source: https://ucpcentralmn.org/compute rs/ 1.1.2 Generations of Computers The term ‘generation’ was applied to different tsy poef computers in order to delineate the major technological developments in hardware anftdw saore. So far there are four distinct generations, with the fifth under construction. b Irnief, they are outlined below: 1. First generation 2. Second generation 3. Third generation 4. Fourth generation 5. Fifth generation 1. First generation (1944 – 1958) – The technology used was vacuumes t.u bThey used punched cards and magnetic tapes and wer ea nsldo wlarge, producing a tremendous amount of heat, and running one program at a tim Eex. amples are ENIAC and UNIVAC1. 15 UNIVERSITY OF IBADAN LIBRARY 2. Second generation (1959 – 1963) – The technology used was transi satonrd some other solid – state devices. These were much esrm tahlal n the vacuum tubes, and made computers to be smaller, more reliable and siganniftilcy faster. 3. Third generation (1964 – 1970) – Now the Integrated Circuit (IC) larecped the transistorized circuitry as the technology impro.v eTdhe use of magnetic discuss became widespread, and computers began to support theb icliatipeas such as multiprogramming and sharing. Operating systems and applicationw saoreft became increased and rapidly produced, and the sizes of computers became murceh rmedouced. 4. Fourth generation (1971 – Now) – Improvement in technology causede th replacement of the Integrated circuit by the LaSrgcea le Integrated Circuit (LSIC). The computers of this era had a much larger capac istyu ptoport main memory, and the use of keyboard as an input device began to be popular. 5. Fifth generation (Now and in the future) – this is still under ctornusction. However what constitutes the fifth generation computers nhoats been well defined. Nevertheless, everyone agrees that there must be a great impreonvte omver the LSIC technology. 1.2 Types of Computers Computers can be categorized according to the icra (paa)bilities and (b) primary functions. a. Types of computers according to capability i. Super computer – this is about 50,000 times faster than a micmropcuoter and can handle large amount of scientific computation.i s Imt aintained in a special room or environment. 16 UNIVERSITY OF IBADAN LIBRARY Figure 1.3: Super Computer Source: http://www.networkworld.com/article/2848788/datan-tceer/the-10-mightiest- supercomputers-on-the-planet.h t ml ii. Mainframe computer – this can support the processing requirementhsu onfd reds and often thousands of users and computer profneaslsi.o It is large and also maintained a controlled environment. iii. Minicomputer – also called midsize or low-end mainframe comp, uitse rfunction is similar to the main frame computer, and can sup 2p otort about 50 users and computer professionals. iv. Microcomputer – this is the most common computer seen in almeovestr y office, home and everywhere computer is used. It is anlsoow kn as a personal computer (P.C) and comes in a variety of forms such as d, inaorytebooks, laptop and desktop computers. 17 UNIVERSITY OF IBADAN LIBRARY Figure 1.4: Microcomputer Source: https://www.quora.com/What-is-Micro-computer-supceorm- puter- mainframe-and-Minicomput e r In-Text Question First Generation technology used was In-Text Answer Vacuum tubes. b. Types of computers according to their primary fuionnc t i. Digital computers – these operates on data of discrete forms andfo rmpesr mathematical computations on them. It is mosta sbuleit for business and statistical analyses. ii. Analog computers – these operate on data in the form of continuvoaursia ble quantities. It is most suitable for engineeringrp pouses and other physical sciences. iii. Hybrid computers – these combine the features of both the digintadl analog computers. 18 UNIVERSITY OF IBADAN LIBRARY 1.2.1 Components of the Computer The computer is made up of the hardware and soeft wcoamr ponents Computer Hardware Software Peripherals CPU System software Application software Input Output Backing ALU CU Memory media media storage Operating Translator User Application device device system application package software The computer system is made up of two parts, na mthel yhardware and the software. A. The hardware comprises of the i. Peripheral devices ii. Central Processing Unit (CPU) 1. The peripheral devices are made up of the i. Input media devices, like the keyboard, mouse,t lpigehn, scanner, joystick, etc. ii. Output media devices, like the monitor, printera,p ghr plotter, speaker, etc. iii. Backing storage which is used in storing data annfodr mi ation. iv. The monitor goes by different names such as thuea vl idsisplay unit (VDU), display, cathode ray tube (CRT), and so on. 2. The Central Processing Unit (CPU) also knownth aes C entral Processing Zone (CPZ), is comprised of the Arithmetic/Logical Unit (ALU), th Ceontrol Unit (CU) and the memory, also known as the main or primary memory. 19 UNIVERSITY OF IBADAN LIBRARY The ALU is where the computer processes arithmaentdic logical operations. The operators for these operations include: i. Arithmetic operators - + - ÷x ii. Logical operators - > ≥< ≤ The CU is the heart of the computer. It is whevrer yething that goes on in the computer is controlled. It is also called the supervisor. Tmhemory stores data and information even while the file processing is in progress. Theree tawro main parts of this memory. These include the Read Only Memory (ROM), and thaen dRom Access Memory (RAM). The ROM is a non-volatile or permanent memory, ew hthile RAM is a volatile or temporary memory. Information stored in the ROM cannot bset leoven when the power is turned off. Information in the RAM is lost when power is turn oefdf. In order not to lose information we have tos ave. In-Text Question ……………. comprised of the Arithmetic/Logical Unit (AUL) In-Text Answer Central Processing Unit (CPU) The path a data takes through the computer for t uit rtn to information is: - Input of data through the input media device - Processing of data through the CPU - Output of information through the output media dcev i This is illustrated below; The computer is made up of the hardware and soeft war 20 UNIVERSITY OF IBADAN LIBRARY ALU Inpu Output t CPU or CPZ B. The software is a set of instructions written fhoer tcomputer to carry out its task. There are two types of software, namely the system soreft,w aand the application software. 1 The system software is the set of instructionistt ewnr for the computer to manage itself. There are two of them, which are the operating esmy s(tOS), and the compiler and interpreter. a. The operating syste mis a set of system program that acts as an inatesrep hbetween the user and the machine. b. The translator commonly called comp iliesr a set of system program that translates user language to machine readable language. Ttehrep rIenter is peculiar to only the BASIC programming language. 2. The application software is a set of instruc toiorn program written purposely for the user to perform his/her task. This is also of ttwypoe s, namely the user application software and the application package. a. The User Application Softwar eT:his is the program written for private use. b. The Application Packag eT:his program is designed for public use. For epxlaem, MS Word, MS Excel, SPSS, E-views, AutoCAD, Peaceh Atreccounting, etc. Application software comes in different forms. Tseh eare i. Low Level Language e.g. Assembly Language ii. High Level Language e.g. BASIC, FORTRAN, COBOL, C, +J+AVA 21 UNIVERSITY OF IBADAN LIBRARY iii. DOS-based Applications e.g. Lotus 1-2-3, WordPet,r fDebcase 3+ iv. Windows-based Application e.g. MS Word, MS AcceCsosr,e l Draw. 1.2.2 Introduction to Operating System The operating system (OS) is system software tchtas ta as an interphase between the machine and the user. It is also called control program s yosrtem program. There are two types of OS. 1. Single – Use OS 2. Multi – User OS A single-user OS accepts commands form one usear tiamt e and perform a single task. Examples include MSDOS, IBMDOS, PCDOS, and all ivoenrs of WINDOWS. A multi-user OS accepts commands from differentr su sweorking at different local terminals simultaneously. Examples include UNIX, XENIX, LA NW,AN, MOS. 1.3 Implications of Computerization Computers have essentially revolutionized data inafnodrm ation processing. They have also changed some industries and actually created o. th Merasny people focus on the freedom from routine and boring activities that computeirvse g. The phasing out of many jobs by the introductio nt hoef computer has necessitated people rushing to train on the use of the computer. To mdaoyst employees are computer literate, at least to some extent. In fact, computer literasc yfa ist becoming a major requirement for employment no matter the certificate or degreeh oanse. Computer are used in virtually every field of hum eandeavour such as in business, government, legal profession, medicine, educatiionnd,u stry, entertainment and sports, agriculture, and the home. (Please make a reseinatroc h ow the computer fits in into these fields). 22 UNIVERSITY OF IBADAN LIBRARY The following are a few examples of jobs that comteprsu create; i. Software/Firmware Engineer ii. Decision support Analyst iii. Programmer Analyst iv. MIS Manager v. Desktop Publishing/Graphic Artist vi. Word processor vii. Application Programmer viii. Telecommunications Engineer ix. Data Processing Specialist x. Computer Training Specialist xi. Data Processing Position xii. Computer Technician/Engineer xiii. Data Entry clerk xiv. Systems Programmer xv. Computer Marketing Professional xvi. System Analyst 23 UNIVERSITY OF IBADAN LIBRARY Summary In this study session you have learnt about: • Data and information processing have been of ut mimopstortance to humans long before the advent of computers. • Through the ages, human beings have made effo rintsv etont devices that could give them ease in the processing of data, and stoorfa ginef ormation. This led to the abacus and other calculating devices. • The first computer was developed by Charles Bab bina g1e842, and Augusta Ada Byron wrote the program that this computer us ed. • A computer is composed of the input unit, procegs suinit and the output unit. • Since 1944 we have witnessed four distinct genoenrsa toi f computer. Computers are classified according to their capabilities andm parriy functions, and are made up of the hardware and software. • The use of computers in society cannot be overesmizpehda. It has virtually taken over many jobs Self-Assessment Questions (SAQs) for study sess1io n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the followinge sqtuions. Write your answers in your study Diary and discuss them with your Tutor at ntheext study Support Meeting. You can check your Define School answers with the Notesth oen S elf-Assessment questions at the end of this Module. 24 UNIVERSITY OF IBADAN LIBRARY SAQ 1. Differentiate among the generations of computers. 2. Differentiate between the operating system and ciloemr. p 3. Write on the effects of computers on the society. References Goal 2000 Computer Networks, Module U1s: ing Micro-Computers. MAGNA Computer School, Ibadan: Nigeria. Introduction to Micro computing and Operating Syms te- A handbook of Computer Appreciation. MAGNA Computer School, Ibadan: Nrigae. 25 UNIVERSITY OF IBADAN LIBRARY Study Session 2: Computer in Statistics Profession Introduction In the previous study session, you noted that cotemrsp uhave taken over most jobs. There is hardly any profession that has not applied the cuotemrp to its job. Statistics is no different. Due to the enormous data that statisticians, anjdo rimtya of other profession that uses data and information, encounter, you cannot undereseti mthaet volume of computations being performed every day. Without the use of computation devices (which indcel uthe computer) data processing can become very boring. Even with ordinary calcula toitr sis boring. Thus, the need for statisticians and users of statistical data to beell awcquainted with the use of computers cannot be overemphasized. Learning Outcomes from Study Session 2 At the end of this study session, you should be atob:l 2.1 The Growth of Computational devices in Stact isti 2.2 Identify packages that are used to solves stitcast iproblems. 2.1 The Growth of Statistics and Computational Devcies The study of statistics started with an Englishmcanlle d John Graunt in the eighteenth century. He was a commoner and a storekeeper,w wahso i nterested in reviewing a weekly 26 UNIVERSITY OF IBADAN LIBRARY church publication issued by the local parish c lethrkat listed the number of births, christenings, and deaths in each parish. These so called Bills of Mortality also listed tchaeu ses of deaths. He organized this data in the forms we call descriptive statistics, which wpausblished as NATURAL AND POLITICAL OBSERVATION MADE UPON THE BILL OF MORTALTI Y. Shortly thereafter, he was elected as a membehre o Rf ot yal Society. Statistics, which comes from the Italian word standing for state, dealsh w diat ta collected by a nation just for records purpose. The history of data collection dates btoa cbketween 3000B.C. and 4000B.C. Then there was no interest in this collection mtohraen just to be acquainted with the total population of people or things. This form of dactoall ection is very much common in the bible, especially the Old Testament. Figure 2.1: John Graunt Source: https://en.wikipedia.org/wiki/John_Graunt 27 UNIVERSITY OF IBADAN LIBRARY From the days of Graunt, interest shifted from plaotpioun to such statistics as the averages, percentages and proportions. This started thel odpemve nt of statistics as a unique area of study and professionalism. Before long it bega nfin tdo application in many fields much as agriculture and the social sciences, especiallyn oemcoics. Today statistics is a unique feature of the 20th century. In the 19th century, computation depedn odne calculating machines. The profound statistician, Karl Pearson (the first h eoaf department of statistics in the world, in the University of London), wrote in a letter in 148: 9“I want to purchase a Brunsviga calculating machine before anything else, and amki nmga inquiries about it. I think it would make moment – calculating easy”. In the early 1s9, 0ro0utine statistical analysis involved mostly tabulating data and calculating averages inadnedx numbers. As such, the machines of those times were made to add and subtract, opuritn ttotals, sub-totals, and individual items if required. The first modern and automatic electronic compu wteerrse introduced in the 1940s, precisely the ENIAC. The advantage of the electronic comrp iust ethe speed at which computations are carried out. However, statisticians were silno win tegrating the use of computers for their use due to the fact that, then, they were more ecronnecd with much smaller sets of data compared to the large data the computer can ha n dle. They felt their calculators are enough for the sl mtaaslk. Apart from that, most computer programs then were written by mathematicians anudld c noot do exactly what the statistician desired. Besides, statisticians were unwillingw rtoite programs, at least for their use. To crown their unwillingness to use computers,i ssttiacitans want to be in direct contact with the data and the processes of analysis, which m tahkeems to be very familiar with the result for proper decision making. With the computer,e cdti rinput with the data is most possible, and most statisticians do not know what the comrp duotes with their data. The arrival of microcomputers has had positive cet ffoen statistical analysis. These effects include its computing power, which is enormousley agter r than that of the desk machines, in that they can be programmed using a high-level ulagneg, and the more recent windows 28 UNIVERSITY OF IBADAN LIBRARY application programs. With the microcomputer, ocanne keep the analysis under his control, that is, keep close to the original data while yasnias lis going on. Recently, (about three decades after the introodnu cotfi electronic computers), the technology of calculators improved such that we now have pto-scikzee hand-held electronic calculators which could go a long way in assisting in statiaslt icomputations. Unlike the old electro-mechanical desk machineesy, thave a number of built-in programs, ranging from a mean-and-variance routine to (in em oprowerful versions) routines for regression and validity probability densities. In-Text Question The study of statistics started ……………..in the eeigehntth century In-Text Answer John Graunt 2.2 Programing Techniques for Statistics In the early days of computer programming, machciondee s were the only familiar software. However, development has grown from these codethse t oh igh-level languages like BASIC, FORTRAN, COBOL and PASCAL. Some of these high-l epvreograms were not so user- friendly, though more user friendly than the maceh cinodes. This led to the development of more user-friendrloy gprammes (due to the evolvement of windows) like the SPSS (Statistical Package fo rS tohceial Sciences), developed in the USA, and GENSTAT (a General Statistical Program) andM G, LbIoth developed in Britain. The most user-friendly program that could analyze sttiactai l problems is the Microsoft Excel. This is a software for most forms of computatiodnesv, eloped by the Microsoft Corporation, USA. It is easy to learn. 29 UNIVERSITY OF IBADAN LIBRARY Figure 2.2 : Microsoft Excel Other statistical packages include the E-views n(Eocmoetric Views) for econometric and time series analyses, SAS (Statistical ApplicatSionft ware), STATA, statistical software for almost all statistical theory, and a host of manthye ors. The effect of computers is much more noticeable in advanced statistics, such a Ms uthlteiple Regression. The whole field of multivariate analysis has beepne noed up through the advances in computers, but the techniques developed there aasred b on more complicated mathematics. In this book, all our computations shall be based on MS Excel and BASIC. Summary for 2 Even though statistics have been in use for a lvoenrgy time, its scope had not gone beyond knowing the population of a country. However, siitgsn ificance began to unfold in the 18th century when John Graunt began to collect demogicr adpahta recorded in the church. 30 UNIVERSITY OF IBADAN LIBRARY Early statisticians had no interest in the useh oef ctomputer, but with the development of statistical tools, interest began to shift towatrhdes device. Today most statistical analyses are computed through the use of computer devices. Self-Assessment Questions (SAQs) for study sess2io n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the followinge sqtuions. Write your answers in your study Diary and discuss them with your Tutor at ntheext study Support Meeting. You can check your Define School answers with the Notesth oen S elf-Assessment questions at the end of this Module. Reference Cooke D., Craven A. H. and Clarke G. M.: Basic iSstiactal Computing. Second Edition. Edward Arnold. A division of Hodder and Stought on. 31 UNIVERSITY OF IBADAN LIBRARY Study Session 3: Representation of Data in the Comupter Introduction Computers do not understand the human code. B perforceessing can be done there must be leverage between the user and the computer. Tsh, atth ei computer must be able to understand what you are saying. You may not be able to understand the computeru lagneg, but you speak to the computer in your own language. There is a device in the comerp tuhtat converts your language to that which the computer is able to understand. Thisic dee ivs called the COMPILER. In BASIC programming language, it is called the INTERPRET ER. Learning Outcomes from Study Session 3 At the end of this study session, you should be atob:l 1.1 Explain the Binary representation 3.2 Simple Computer Arithmetic 3.1 Binary Representation Computers use the Binary code for data or char arecpterresentation. This numbering system has only two digits, 0 and 1. Each is referreda st ob inary digit (or bit). Each bit is used to denote the presence and absence of electrical opru slsigenal in the computer circuitry. Most numbering systems are called positional, bseec athue physical location or position of digit within the number affects the value. Fort ainnsce, 64 and 46 have same digits, but their values are different because of the position o f dtihgeits. The same with 52 and 25. Let us 32 UNIVERSITY OF IBADAN LIBRARY look at the following decimal value of position wase ll as their corresponding exponential value of position. Decimal value of positio n 1000 100 10 1 -------- Exponential value of positio n103 102 101 100 Decimal value of positio n 16 8 4 2 1 -------- Exponential value of positio n24 23 22 21 20 The binary numbering system has a base of 2. Character Representation Each storage position in the main memory is redfe rtore as a byte. Since each byte contain four bits that represents the decimal number 82, , 4a, nd 1, then this makes it possible to represent the decimal digits 0 to 15. However ,t hfoer alphabets and special character an 8- bit code is usually used. This is divided into ,t wvioz, four zone bits and fourd igit bits. This is represented below: Zone bits Digit bits 8 4 2 1 8 4 2 1 One Byte The Extended Binary Coded Decimal Interchange c(oEdBeC DIC) is the most commonly used computer code for character representatiohne. foTur zone bits are used to indicate codes for letters, unsigned numbers, positive nursm, bneegative numbers, and special characters. Other computer codes for characterer srenptation include: Binary Coded Decimal (BCD) and American Standardd eC ofor Information Interchange (ASCII). There is an 8 – bit ASCII and also a 7t -AbSi CII. 33 UNIVERSITY OF IBADAN LIBRARY 3.1.1 Converting from Decimal to Binary Using the positional numbering system, you can rdmeintee what combination of these positional values equals the decimal value. Example 1 : Convert 13ten to binary. Solution: Write out the positional values and place thgeit d1i under the values that add up to 13. Put 0 elsewhere. 8 4 2 1 1 1 0 1 So that 13ten = 1101two. Using the remainder theorem, divide the decimaul ev ably 2 and write down the remainders. Then rearrange the remainders from the bottome t oto tph. 2 13 2 6 r 1 2 3 r 0 2 1 r 1 2 0 r 1 = 110tw1o. 3.1.2 Converting from Binary to Decimal It can also use the positional numbering systemde toe rmine this. 34 UNIVERSITY OF IBADAN LIBRARY Examples 2 : Convert 111t1wo to decimal. Solution: Write out the positional codes and write undheer tdigits of the binary code. Multiply each code with the corresponding value andd. 8 4 2 1 1 x 1 = 1 1 x 2 = 2 1 x 4 = 4 1 x 8 = 8 15 ∴ 1111two = 15ten. 3.2 Simple Computer Arithmetic 1. Addition: Addition of binary numbers is the same with deacli mnumbers. The highest value for any addition is 1. Anything above 1 isk etna as a ‘carry over’ to the next addition. Example 3 : 10two + 11two, and compute its equivalence in decimal. Binary Decimal 10 2 + 11 + 3 10tw1o 5ten Example 4 : 1011two + 1110two 35 UNIVERSITY OF IBADAN LIBRARY Binary Decimal 1011 11 + 1110 + 14 1100 1two 25 2. Subtraction: While binary subtraction is done in much the s awmaey as the decimal subtraction, computers perform this task uscinogm plement additio nA. complement of a number is the value which must be added to it to it sg enumber base. For example, in the decimal system, the complement of 7 is 3, and othf a6t is 4. Using this to perform a subtraction, say 7-4, obtain the complement of d4 adnd to 7. Then discard the carry, and leave the rightmost digit. That is, the compelent of 4 is 6, so that 7 + 6 = 13. We discard 1 and write 3, this gives the same ressu l7t a– 4 = 3. The same method is also applied to the binary system. The reason for this is that unique property ofa bryin numbers allows the determination of the 2’s complement to be very simple, a fact thast himportant implications for simplifying computer circuit design. The digit nbge isubtracted is called the ‘subtrahend’ while the digit from which the subtrahend is takfreonm is called the ‘minuend’. Example 5 :Perform 7 – 4 in binary with and without complemt aedndition a. Without complement addition. Decimal Binary 7 0111 - 4 - 0100 3 0011 36 UNIVERSITY OF IBADAN LIBRARY b. With complement addition Decimal Binary 7 0111 + 6 + 1011 * 3 1100 11 0011 3. Multiplication: This is done by ‘a shift-left and add’ operatsio ans it is in the case of decimal. For example, 12 multiplied by 12 in deacli mis 12 x 12 24 12 144 Example 6 :Multiply 12 x 5 in binary Decimal Binary 12 1100 x 5 x 0101 60 1100 + 0000 1100 37 UNIVERSITY OF IBADAN LIBRARY 0000 0111100 60ten = 111100two verify this. Example 7 : What is 122 in binary 1100 x 1100 0000 + 0000 1100 1100 10010000 10010000two = 144ten verify this. 4. Division: This is the opposite of multiplication. Inste oafd shifting the multiplier left and adding the intermediate result, division isf opremred by shifting the divisor right and subtracting it from the quotient initially and th fernom each intermediate remainder until zeros are obtained. The subtraction would act uablely performed using the 2’s complement addition method. Example (without complement addition0 25÷5.25ten = 11001two and 5ten = 101two 38 UNIVERSITY OF IBADAN LIBRARY 11001 - 101 10100 - 101 01111 - 101 01010 - 101 00101 - 101 00000 Count the number of subtraction and convert it etoci mdal. In this example, the number of subsequent subtraction is 5. steon 5= 101two. Therefore 2t5en ÷ 5ten ⇒ 11001two ÷ 101two = 101two Optal and Hexadecimal Other useful number systems in computer are thael oacntd hexadecimal number systems. The octal number system uses 8 symbols (0 -7)s a dsi gitits. Note that each of these octal digits can be represented by 3 bits. The hexadael cnimumber system on the other hand, uses 16 symbols (0-9 and A – F). This A- F represen0t s- 15. Each of these hexadecimal digits can be represented by 4 bits. 39 UNIVERSITY OF IBADAN LIBRARY Summary for 3 • The only way the computer can decode the data nafonrdm iation entered into it is for it to convert it (data and information) to machine co d e. • This conversion is brought about by the compileTrh. e codes used by the computer to represent data are the digits 0 and 1, which repnrt etshe presence and absence of electrical pulse or signal in the computer circyu. it r • These codes are called binary digits. Each sto praogsietion is called a byte, containing 4- bits. The alphabets and special characters are srenpted by 8-bits comprising 4-zone bits and 4-digit bits. • Apart from the binary digits, other useful numbeinr st he computer are the octal and hexadecimal. Self-Assessment Questions (SAQs) for study sess3io n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the followinge sqtuions. Write your answers in your study Diary and discuss them with your Tutor at ntheext study Support Meeting. You can check your Define School answers with the Notesth oen S elf-Assessment questions at the end of this Module. 1. Convert the following to binary: (a0). 1 (b) 12 (c) 8 3 (d) F 2. Obtain the following using binary codes: (a) LOG5 125 (b) 3 2 + 23 (c) 0.5 x 2 (d) 50 ÷10 3. Given a base of 9 and a height of 5, what gone isn othe compiler when the area of a parallelogram is computed? (Show your workings). 4. A trapezium has its height as 6, and the pal rsaildlees as 4 and 7.5. Obtain its area in binary and convert your result to decimal. 40 UNIVERSITY OF IBADAN LIBRARY 5. Convert the following to decimal: (a1)1 00110two (b) 0010010two (c) 1111111two (d) 000011two Reference Ojo S. O. (1991):I ntroduction to Computer Scien.c e Revised Edition. Department of Computer Science, University of Ibadan, Nigeria. 41 UNIVERSITY OF IBADAN LIBRARY Study Session 4: Introduction to Microsoft Excel Introduction Analysis of data on computer is fast taking then es hoi ff electronic calculators and adding machines. One of the commonest and easiest tno sleoaftrware for analysis is the Microsoft Excel. MS Excel is a powerful tool from the Micorofts Corporation. It is an interesting package for introduction to data analysis. Learning Outcomes from Study Session 4 At the end of this study session, you should be atob:l 1.1 Explain the Spreadsheet 1.2 Types of Sheet in MS Excel. 4.1 Spreadsheet Microsoft Excel is a powerful electronic spreadst hfeoer Microsoft Windows on which data are entered and calculations performed. A spreeaedt sish a sheet that has rows and columns in which data are entered into for the purpose aotfh mematical, statistical, logical, operation, and so on. An MS Excel spreadsheet is made up of grid linesrt i(cval and horizontal) that form cells. A cell is an intersection of four grid lines, two wofh ich are vertical and the other two horizontal. In other words, it is the intersection of a row aan dcolumn. There are about 256 columns and 65,536 rows. The columns are labeled A tow hivi,le the rows are labeled numerically. Every cell has a name which is always correspon dtoin gthe column and the row. For example a cell that is made up of column D and 8ro isw called cell D8. 42 UNIVERSITY OF IBADAN LIBRARY Diagram of MS Excel spreadsheet Menu bar Standard toolbar Formatting toolbar Scroll bars Worksheet or chart name 4.1.1 Simple Operations To go to any cell, simply type the cell name in Nthaeme Box . The Name box is directly under the toolbars. To execute the command to gao n tamed cell after typing the cell name (in the name box), press the ENTER key. Every tyimoue type into any cell, the content is displayed in theF ormula Bar. The Formula Bar is adjacent to the Name Box. When you want to change the content of a cell, lsyi mpove your cell pointer to that cell and begin typing the new content. The former conteinll t awutomatically be deleted. If you want to add to the content of a cell, take your celln pteori to the cell, and click your mouse on the formula Bar. In-Text Question …………………is made up of grid lines that form cells. In-Text Answer MS Excel spreadsheet 43 UNIVERSITY OF IBADAN LIBRARY Use the arrow or direction) keys to move to thec ep layou want to make an additional insertion, are start typing. Every time you enintetor a cell, press the ENTER or arrow keys. 4.1.2 Selecting a Cell or Multiple Cells 1. To select a cell, simply click on that cell or uthse arrow keys to move to the cell you want to select. 2. To selecta djacent cell,s click the first cell and drag your mouse to thaest lcell that you want to include in the selection. Using they bkoeard, take the cell pointer to the first cell in the selection, hold down the SHIFTy kaend use the arrow keys to include every cell for the selection. 3. To selectn on-adjacent cell,s hold down the control key and click the cells ywoaunt to include into the selection. 4.2 Types of Sheet in MS Excel There are several workbooks in the MS Excel, an de aicnh workbook are several sheets. Each workbook and sheet runs into thousands reivsepleyc. t You can therefore imagine how many sheets are available in the MS Excel. Chte. ck i There are different types of sheets in a workbo Tohke. se include 1. Worksheet 2. Chart sheet 3. Visual Basic module sheet 4. Dialog sheet 5. Macro sheet 6. International macro sheets 44 UNIVERSITY OF IBADAN LIBRARY However, not all of these may be available in yoMuSr Excel package. The commonest sheets of the lot are the worksheet and chart .s hTeoe tinsert a worksheet, from the INSERT menu choose worksheet. You can as well press SH+ IF1T1 on the keyboards. To insert a chart sheet, from the INSERT menu ceh ocohsart sheet, and theAns New Shee t. The chart wizard appears. Then follow the instiorunc ot n the screen. You can as well press F11 on the keyboard. Sheets can be renamed, copied or moved within othrkeb wook, or to another workbook. You can also hide sheets within the workbook. In-Text Question To select a cell, simply click on that cell or uthse arrow keys to move to the cell you want to select. True or False In-Text Answer True. The worksheet is where calculations are performwehde,r eas the chart sheet is where charts are displayed. However, charts can also be diespdl aoyn the worksheet that contains the data used to draw the chart. Any alteration on the daalstoa alters the chart. Deleting the data also deleted the chart. Sizing Cells Cells can be enlarged or reduced. Three methodusld w boe presented here. Method 1: i. Take the cell pointer to the cell you want to egnela or r reduce. ii. Click the FORMAT menu. iii. Point to COLUMN and click ‘width’ iv. Enter a size value that could accommodate the einn trhye cell. (However you must be aware of the standard sfi zae coell which is normally 8.43 pixels). 45 UNIVERSITY OF IBADAN LIBRARY v. Press ENTER or click OK. Method 2: i. Select a cell you want to resize ii. Click the FORMAT menu iii. Point to COLUMN and click ‘Auto fit selection’. Method 3: i. Go to the column label corresponding to the ceull wyoant to resize. ii. Place your cell pointer on the right grid line hoef tcolumn. iii. Hold down the mouse button and drag to the desiirzeed, then release. Note: Whatever resizing done to any cell affecvtesr ye cell in that column. Summary for 4 In this study session, • You have been introduced to MS Excel as electrosnpirce adsheet software with simple operation of how to move to a named cell, chang aed odr to the entry of the cell. • You have also learnt the different types of sheine tas single workbook, and how to select a cell or multiple cells. Also learnt is how toz es ia cell to accommodate its entry. Self-Assessment Questions (SAQs) for study sess4io n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the followinge sqtuions. Write your answers in your study Diary and discuss them with your Tutor at ntheext study Support Meeting. You can check your Define School answers with the Notesth oen S elf-Assessment questions at the end of this Modul.e 46 UNIVERSITY OF IBADAN LIBRARY 1. Define a Microsoft Excel. 2. How many cells are in a single worksheet? 3. How do you select non-adjacent cells of a works?h eet 4. What are the kinds of computations that could brefo prmeed with MS Excel? 5. Mention five types of charts that could be drawnth w Mi S Excel. Reference Goal 2000 Computer Networks, Module 4: Using Micorfot sExcel. MAGNA Computer School, Ibadan, Nigeria. 47 UNIVERSITY OF IBADAN LIBRARY Study Session 5: Data Entry in Microsoft Excel Introduction The simplest task in MS Excel is entering data tinhteo cells. This is the first step before any analysis could be carried out. There are diffe rteypnet s of data that MS Excel can accept. However, there are rules governing entry of daTthae. r e is no data that operations cannot be performed upon. Learning Outcomes from Study Session 5 At the end of this study session, you should be atob:l 5.1 Explain the types of data to be entered into an E MxcSel worksheet; 5.2 Explain the techniques of entering data into tehlels ;c 5.3 Create simple forecasts and trends. 5.1 Types of Data You can enter two types of data into a workshetehte –se include 1. Constant values 2. Formulas A constant value is data that you type into a dcierell ctly. It comes in various forms. These are numeric, alphabetic, alphanumeric, date, ticmuerr,e ncy, logical, percentages, fractions, and scientific notations. Values are constant daon dn ot change unless you select the cell and edit the value yourself. On the other hand, a formula is a sequence of acnotn svtalues, cell references, names, functions, or operators that gives rise to a newlu ev afrom existing values. In MS Excel, formulas always begin with an equal sign, ‘=’. cIans es where cell references are used to 48 UNIVERSITY OF IBADAN LIBRARY create a formula, a value that is produced ase tshuel tr of the formula can change when other values in the worksheet change. Numbers The combination of numeric characters, 0 throug ahn 9d, any special character such as + - ( ), / & % make up Numbers. When entering numbers, i. You can include commas, for example 1,000,000 ii. A single period is treated as a decimal point. iii. Plus signs entered before numbers are ignored. iv. Precede negative numbers with a minus sign or seen ctlhoem within parentheses (or brackets). MS Excel uses General number format as defaulte, petx octherwise stated. When it can, it automatically assigns the correct number formayto tuor entry. For instance, when you enter a dollar sign before a number, MS Excel automalyti ccaolnverts your entry into a currency format. When entered, numbers in the cell alig tnh eto right. Dates and Times If you want to display the time using the 12-houlor cck, type am or pm. For example 5:00p.m, 1.00a.m. You can type ‘a’ or ‘p’ insteoafd a m or pm. However, you must include a space between the time and the letter. Unleus sty ypoe am or pm, MS Excel automatically displays time using the 24-hour clock. For exam: p2l0e:00, 23.00, and 13.00. You can type a date and time in the same cell. r eT hmeust be a space between them. In entering date, you can use either a slash (/) hoyr pah en (-). There are several standard formats for displaying date and time; however, MxSc eEl stores all dates as serial numbers and all times as decimal fractions. 49 UNIVERSITY OF IBADAN LIBRARY In-Text Question What is constant value? In-Text Answer A constant value is data that you type into a dcierell ctly. MS Excel sees dates and times as numbers. Thise sm iat kpossible to perform different arithmetic operations on them. For example, thffe rdeince between 5th of December, 2005 and 3rd of May 2005 is written. = “5/12/05” - “3/5/05” and the result that would be displayed is 175. 5.2 Data Entry Techniques To enter data into a cell i. Select the cell you want to enter data. ii. Type the data. iii. Press ENTER. To type same entry into several cells at once i. Select the range of cells you want to enter daTthae. selected cells can be adjacent or non-adjacent. ii. Type the data iii. Press CTRL + ENTER To enter numbers with fixed decimal places i. From the tools menu, choose options 50 UNIVERSITY OF IBADAN LIBRARY ii. Select the Edit tab iii. Select the fixed decimal check box, and then s ethleec tnumber of decimal places in the places box. iv. Choose the OK button v. Begin entering numbers into cells without typinge tpheriod for the decimal places. 5.2.1 Using Autofill The AutoFill feature is used to create a serieisn corfe mental or fixed values on a worksheet by dragging the fill handle with the mouse. Foar mexple, you can copy the value from one cell into five cells below it. In this instance,u tAo fill works in the same way as the Fill commands on the Edit menu. You can also drag the fill handle to increment raie se or you can use the series command (Edit menu, Fill submenu). For instance, if you type January and Februaryo inns eccutive columns, and then drag the fill handle to the right, MS Excel fills March, April, aMy and so on into the selected cells. Series are useful when creating table row or co luhmeandings on a worksheet, or anytime you need to enter a series of incremental numbdeartse,s , or time periods. Series can be created in any direction. AutoFill can only fil lr ange of adjacent cells. To copy by dragging the fill handle. i. Select the cell containing the data you want toy c. op ii. Drag the fill handle across the cells you want itllo afnd then release the mouse button. Any existing values or formulas in the cell youl wfilill be replaced. To create a series increment: 51 UNIVERSITY OF IBADAN LIBRARY i. Type into the first 2 or 3 cells of the data in eorr.d ii. Select the two or three cells that contain the dyoauta have typed and which you want to create a series. iii. Drag the fill handle across the cells you want itllo afnd then release the mouse button. In-Text Question What is AutoFill? In-Text Answer The AutoFill feature is used to create a serieisn corfe mental or fixed values on a worksheet by dragging the fill handle with the mouse. To copy using the Fill Right and Fill Down comma nds i. Select the cell or cells you want to copy and tdhjea caent cells you want to fill. ii. To copy the selection’s first column into the adejnatc cells to the right, choose Fill from the Edit menu, and then choose Right. Keyboard command = CTRL + R To copy the selection’s first row into the adjac ecnetlls below, choose Fill from the Edit menu, and then choose down. Keyboard command = CTRL + D To copy the selection’s last column into the adnjat cceells to the left, hold down SHIFT, choose FILL from the Edit menu, and then chooset. L ef Keyboard command = SHFT + FILL Right button (Ecdait egory) To copy the selections last row into the adjacenllst cabove, hold down SHIFT, choose FILL from the Edit, and then choose up. Keyboard command = SHIFT + Fill Down button (Ecdaitt egory) 52 UNIVERSITY OF IBADAN LIBRARY 5.3 Creating Simple Trends and Forecasts Trend means behaviour. You can observe the beuhra ovfio your data, e.g. sales. The pattern of sales of a company or business enterprise cboeu lsdt udied. Also a future value could also be predicted for any data. In this section, we gaorieng to see how to create simple trends and forecasts given a data. To create a linear or Growth Trend series using A tuhteo Fill shortcut menu: i. Select the cell range containing the values on hw yhoicu want to base you trend. ii. Hold down the right mouse button and drag theh failnl dle the direction you want to fill. (The AutoFill shortcut menu is displayed) iii. Choose linear trend or Growth Trend. To create Linear or Growth Trend series using ethreie s command i. Select the cell range containing the values on hw yhoicu want to base your trend. ii. From the Edit menu, choose Fill, and then choorsie ss.e iii. Under series in, select the Rows option button hoer Ct olumn option button, depending on your selection. iv. Under type, select the linear option button to purcoed a linear growth trend, or select the Growth option button to create an exponentrioawl gth trend. v. Select the trend check box. vi. Choose the OK button. 53 UNIVERSITY OF IBADAN LIBRARY Summary for 5 Entering data into MS Excel is a simple process. • Two types of data can be typed into a worksheel.t cTehl ese are constant values and formulas. Any data typed directly into a cell aisl lecd a constant value, and they come in different forms. • On the other hand, a sequence of constant valueell sr,e cferences and so on is a formula. Data can be entered into a cell or range of cet llas taime. Also you can use AutoFill command to copy and create series. • With this command also, trends could be observned , fuature values could be forecast given existing values. Self-Assessment Questions (SAQs) for study sess5io n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the followinge sqtuions. Write your answers in your study Diary and discuss them with your Tutor at ntheext study Support Meeting. You can check your Define School answers with the Notesth oen S elf-Assessment questions at the end of this Module. 1. Enter the following data into an MS Excel workesth e Income Expenditure N N 25,000 25,000 15,000 13,000 30,000 25,000 45,000 44,000 54 UNIVERSITY OF IBADAN LIBRARY 35,000 36,000 20,000 17,000 2. What is the & value of the following i. = “13/1/02” - “29/9/01” ii. = “4:40pm” - “3:15am” 3. The following are sales data from ESCRAVOS LfToDr t he first quarter of the year 1999. MONTH SALE (N) January 251,050 February 270,500 March 269,150 Forecast the sales of the company by the tenth hm, oantd obtain the predicted total sales. Reference Goal 2000 Computer Networks, Module U4:s ing Microsoft Exce. l MAGNA Computer School, Ibadan, Nigeria. 55 UNIVERSITY OF IBADAN LIBRARY Study Session 6: Data Analysis using Microsoft Excl e Introduction No data is useful when it is not processed. Thoec epsrsing of data is also known as analysis. Analysis of data provides information for the pusrep oof making decisions. The Microsoft Excel is mainly for data analysis and there areio vuasr analyses that could be performed. Learning Outcomes from Study Session 6 At the end of this study session, you should be atob:l 6.1 Explain the general rule on Computation in MxSc eEl 6.1 General Rule on Computation in MS Excel The development of the Microsoft Excel is similoa rt ht at of LOTUS 123. Being a software for data analysis, the rules of mathematics mus tf oblleowed. MS Excel makes use of arithmetic and logical operators in returning thuetp out involving two or more figures. The operator are +, -, x an÷d (for arithmetic), and <, >≤, ≥ (for logical). The rule, BODMAS, is also important in the order MS Excel performs aitslc uclations. However, to return the output of any formula or rexspsion, while Lotus 123 uses ‘@’ before writing the expression, MS Excel uses ‘=’. Any reexspsion that is not preceded by the “=” sign will be regarded as a cell entry. Expresswioitnh the ‘=’ sign is called a formula. To correct a formula entry or expression click thel ctheal t contains the formula or expression, and go to the Formula bar and correct. To exeac ufoterm ula, press the ENTER key. 56 UNIVERSITY OF IBADAN LIBRARY Simple Computations using Raw Figures In this section you shall learn how to calculatme pslie mathematical expressions. We shall start with simple arithmetic operations. 1. Addition of 2 and 2 is written =2+2 2. Subtraction of 6 from 10 is written =10–6 or =(-6)+10 3. Multiplication of 3 by 4 is written =3*4 or =4*3 4. Division of 25 by 5 is written =25/5 or =25*(1/5) The above are the basic usage of the arithmetirca otopres. Now we shall go ahead to look at a situation where we have more than one expresins iao nfo rmula. 1. 13 of 9 is written = (1/3)*9 2 4 2. 3 + 5 is written = (2/3)*(4/5) 3. 2 - 12 + 1.5 is written =2–(1/2)+1.5 Just as in normal mathematics, MS Excel will fol lothwe rule of BODMAS, and solve the part in bracket, followed by the addition before tshubtraction. 4. 3¼ ÷ 9/11 is written = (3 * ((1/4)) (9/11) 57 UNIVERSITY OF IBADAN LIBRARY The easiest way to know how to write an MS Excepl resxsion for any mathematical expression is to follow the logical procedures sfolrv ing the problem (assuming there is no computer). In-Text Question Using computation operation, Multiplication of 3 4b yis written In-Text Answer =3*4 or =4*3 Simple Computations Using Cell References A reference identifies a cell or group of cells ao nw orksheet. References tell MS Excel which cells to look into to find the values you wt aton use in a formula. With references, you can use data contained in different parts of a wshoerket in one formula and use the value of one cell in several formulas. You can also refer to cells in other sheets in ark wb ook, to other workbooks, and to data in other applications. References to cells in otheor kwbooks are callede xternal reference. s References to data in other application are carellemdo te reference. s We are going to use the examples in the previocutsio sne to work in this section, using only cell references. 1. Assume the first digit 2 is stored in A3, and theec osnd digit 2 is stored in cell A5, then the addition is written =A3+A5 2. Assume the digit 6 is stored in cell A2, and theg itd i10 is in cell B5, then the subtractions is =B5–A2 Assume a negative digit 6 is in cell A2, then theec osnd method of the subtraction (i.e. addition) is =A2+B5. 3. In the example on multiplication, put 3 in AJ20,d a 4n in K65, then we have 58 UNIVERSITY OF IBADAN LIBRARY =AJ20*K65 or =K65*AJ20 In the fourth example, suppose 25 is stored in B cKe ll2 and 5 in CF 25, then we write =BK2/CF25 Using the second example, assume 25 remained raesd ,s atond the digit 1 stored in B25, and 5 in J49, then the same result is obtained from =BK2*(B25/J49) We go ahead to look at the other examples havinrge mthoan one expression, 1. Put 1 in A23, 3 in C5, and 9 in A12, then the exspsiroen is written =(A23/C5)*A12 2. Let 2 be in B3, 3 in B4, 4 in A13 and 5 in J1, t heexnample 2 is written =(B3/B4)*(A13/J1) 3. Store 2 in cell F14, ½ written as 0.5 in cell Dn7d, a1.5 in cell A1 then examples 3 is written =F4-D7+A1 4. The last example could have 3 in cell A3, 1 in cBe1ll, 4 in cell D8, 9 in cell F29 and 11 in cell M16, and then we write. = (A3* (BI / D8))/ (F29/ M16) Note: It is preferred that cell references be usedw rtiote formulas. The reason is because whenever any change is made in any part of thein oarli gdata or some results that leads to the final result, then subsequent results changes aautitcoamlly, also affecting the final result. Summary for 6 • Problem solving with MS Excel is not different fro tmhe usual mathematics you are familiar with. • MS Excel performs calculations by use of arithm eatnicd logical operation, and also follows the rule of BODMAS. • To execute an expression or formula, an equal ‘s=i’g nm ust proceed the expression or formula. 59 UNIVERSITY OF IBADAN LIBRARY • It is best to use cell references for expressiorn fso romulas whenever the references or formulas are with respect to a data already stionr ethde workbook. Self-Assessment Questions (SAQs) for study sess6io n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the followinge sqtuions. Write your answers in your study Diary and discuss them with your Tutor at ntheext study Support Meeting. You can check your Define School answers with the Notesth oen S elf-Assessment questions at the end of this Module. Post -Test 1. Write an MS Excel expression for calculating i. 12bh , where b = 5, h = 9 −b± b2 − 4ac ii. , where a =1, b = −3, c = 2 2a iii 23 x 32 2. Assume the following data 2, 1, 5, 2, 7, 6, 03,, 91, 3. Let these data be stored in cells B2 to B11 respectively. Write an MS Excel expression for obtaining i. the sum (using the arithmetic operation ‘+’) anodr es tcell D2. ii. the average and store in cell D3. iii. the subtractions of (ii) from each of the datan ptso iin B2 to B11 and store in cells C2 to C11. iv. what have you just calculated? v. what do you expect the sum of (iii) above to be? 3. Assume the data in (2) above is stored in cBe2ll sto K2 respectively. Write an MS Excel expression for obtaining i. the sum as 2(i) above and store in cell E3. ii. the average and store in cell E4. 60 UNIVERSITY OF IBADAN LIBRARY iii. the subtractions of (iii) from each of the datan ptso iin B2 to K2, and store in cells C5 to L5. Reference Goal 2000 Computer Networks, Module U4:s ing Microsoft Exce. l MAGNA Computer School, Ibadan Nigeria. 61 UNIVERSITY OF IBADAN LIBRARY Study Session 7: Using the Function and Chart Wizadrs Introduction The ultimate in the use of the Microsoft Excel nisa laysis of data. In this study session, you are going to discover the different types of caalctiuolns that MS Excel can perform. MS Excel can perform a wide range of analysis, whicahk ems it a ready tool for almost all kinds of professions. There are also wide ranges oft sc hthaart can be drawn. Learning Outcomes from Study Session 7 At the end of this study session, you should be atob:l 7.1 Discuss the use of Functions Pre -Test 1. Mention two functions an MS Excel can perform. 2. What is the action of each of the following sub-cftuionns and under which function can each be found? i. ABS ii. KURT iii. AVERAGE iv. SINH v. IF vi. MOD vii. STDEV viii. VAR ix. EXP x. AND 3. Mention three charts you know. 4. Differentiate between a bar chart and pie chart. 7.1 Using Functions You use the MS Excel built-in functions to perforsmta ndard worksheet and macro sheet calculations. The values that you give a functtion p erform operations on are called arguments. The values that the functions retuern carlled results. You use functions by entering them into formulas on your worksheet. 62 UNIVERSITY OF IBADAN LIBRARY The sequence of characters you use in a funct iocanl liesd the syntax. All functions have the same basic syntax. If you do not follow this syxn, tMaS Excel displays a message indicating that there is an error in the formula. Brackets tell MS Excel where the arguments begidn eand. Remember to include both brackets, with no spaces preceding or following.o u Yspecify arguments within these brackets. Arguments can be numbers, text, logviaclaule s, arrays, error values, or references. The argument you designate must produce a valuide v faolr that argument. Arguments can also be constants or formulas. Tohrme uflas themselves can contain other functions. When an argument to a function is fi tase flunction, it is said to be nested. In MS Excel, you can nest up to seven levels of func tino nas formula. To insert a built-in function 1. Select the cell where you want to enter the form ula 2. From the insert menu, choose function. 3. Select the function category 4. Select the function name 5. Choose the next button 6. Enter values for the arguments 7. Choose the finish button We are going to consider some categories undeer tfhurnections, namely 1. Mathematical and Trigonometrical 2. Statistical 3. Logical (Note: the student is expected to obtain, study u anndderstand all categories) 63 UNIVERSITY OF IBADAN LIBRARY In-Text Question To insert a built-in Function, which one of thel ofowl ing is not correct. A. Select the function category B. Select the function name C. Choose the next button D. None of the above. In-Text Answer D. Mathematical and Trigonometrical Function There are fifty categories under this function. sWheall consider just ten. i. ABS – returns the absolute value of a number. The syntax is =ABS(number). Example: =ABS(5), =ABS(-3), =ABS7()A ii. SIN – returns the sine of a number. The syntax is =SIN(number). Example: =SIN(30), =SIN(-60), =SJIN25( ) iii. COMBIN - returns the combination of a number givae cnh osen number The syntax is = COMBIN(number, number_chosen) Example: =COMBIN(5,2), =COMBIN(B10, A1) iv. LOG – returns the logarithm of a number given ae b as 64 UNIVERSITY OF IBADAN LIBRARY The syntax is =LOG(number, base) Example: =LOG(25, 2), =LOG(36,6) v. LOGIO – returning the logarithm of a number giveans eb 10 The syntax is =LOG10(number) Example: =LOGIO(100), =LOG10(1) vi. LN – returns the national logarithm of a number The syntax is =LN(number) Example: =LN(2.513), =LN(1.045) vii. FACT – returns the factorial of a number The syntax is =FACT(number) Example: =FACT(5), =FACT(3) viii. MMULT – returns the matrix multiplication of two mtraices or arrays. The syntax is =MMULT(array1, array2) Example: MMULT(A2: B3, C4:D5) ix. SUM – returns the sum of several values The syntax is 65 UNIVERSITY OF IBADAN LIBRARY =SUM (number 1, number 2,…) Example: SUM (A2:A10), =SUM(B1: D1) =SUM(1, 2, 1, 3) x. MOD – returns the modulo of a number given a doivnis i The syntax is =MOD (number, divisor) Example: = MOD (5,2), =MOD(23,3) 66 UNIVERSITY OF IBADAN LIBRARY 7.1.1 Statistical Function There are eighty categories under this functione. sWhall consider just ten i. AVERAGE – returns the arithmetic mean of severalul evsa The syntax is =AVERAGE (number 1, number 2, …) Example: =AVERAGE(1,2,1,3), =AVERAGE(B3:B7) ii. GEO MEAN – returns the geometric mean of severlaule vsa. The syntax is = GEOMEAN(number 1, number 2,…) Example: =GEOMEAN(1, 2, 1, 3), =GEOMEAN(B2: B7) iii. HARMEAN - returns the harmonic mean of several evas.lu The syntax is =HARMEAN(number 1, number 2,…) Example: =HARMEAN(1, 2, 1, 3), =HARMAEN(B2: B7) iv. CORREL – returns the correlation between twrroa yas The syntax is =CORREL(array 1, array 2) Example: =CORREL(A1: A5, B1 : B5) iv. VAR – returning the variance of several values The syntax is 67 UNIVERSITY OF IBADAN LIBRARY =VAR(number 1, number 2,…) Example: =VAR(1, 2, 1, 3), =VAR(B2: B7) vi. PERCENTILE – returns the k percentile in ae gniv array The syntax is =PERCENTILE(array, K) Example: =PERCENTILE(l15 : l25, 25) Example: =PERCENTILE(M7 : K17, 90) vii. COUNT – returns the total number of entrya in array The syntax is =COUNT(value 1, value 2, …) Example: =COUNT(1, 2, 1, 3), =HARMAEN(B2: B7) viii. TTEST – returns the value of student –e ts,t tof 2 arrays. The syntax is =TTEST(array 1, array 2, tails, type) Example: =TTEST(A2 : A5, B2 : B5, 1, 2) ix. CONFIDENCE – returns the confidence intervfa al dodition. The syntax is =CONFIDENCE(alpha, standa—rdev, size) Example: =CONFIDENCE(0.05, 1.05, 5) 68 UNIVERSITY OF IBADAN LIBRARY x. BINOMDIST – returns the probability of binoml idaistribution The syntax is =BINOMDIST(number_s, trials, probability_s, cumlmatuive) Example: =BINOMDIST(5,3, 0.5, 1) In-Text Question List four statistical function in MS Excel. In-Text Answer • GEOMEAN • HARMEAN • COUNT • PERCENTILE Logical Function There are six categories under this function. Whaell sconsider all of them. i. AND – returns true if all arguments are fine, owthiesre false if any is false. The syntax is =AND(logical 1, logical 2, …) Example: =AND(B2 >1, B5 < 1) ii. IF – returns true or false given splid condition. The syntax is =IF(logical-test, value-if-true, value-if-false) Example: =IF(B2 <1, B5 >1) 69 UNIVERSITY OF IBADAN LIBRARY iii. NOT - returns true if any is true and false if aarlel false. The syntax is =NOT(logical) Example: =NOT(TRUE), =NOT(FALSE) iv. OR – returns true if any is true and false if arell faalse. The syntax is =OR(logical 1, logical 2) Example: =OR(B2 < 1, B5 >1) v. FALSE – returns the logical value False. The syntax is =FALSE( ) vi. TRUE - returns the logical value TRUE The syntax is =TRUE( ) 7.1.2 Using Charts Charts are pictorial representation of a given .d aTthaere are different types of charts such as bar chart, pie chart, line graph, column chart, asnod on. Charts can be inserted in the worksheet that contain the data being used forl adyisinpg the chart, or on a new sheet called the chart sheet. To display a chart, i. Select the range cells containing the data to beed ufosr the chart. ii. On the Insert menu, click chart 70 UNIVERSITY OF IBADAN LIBRARY iii. Select the type of chart you want to use iv. Follow the instruction on the screen Instead of first selecting the range of cells, ycouu ld go straight to the chart wizard and select the chart you want. And on the space ocre ssp parovided for the cells range, enter or drag the array into it. Then follow the instrucnt ion the screen. Summary for 7 You can use the built in formula in MS Excel to fpoermr calculation. • However, you provide MS Excel with the argumentlsa,c epd inside the brackets. When the syntax is not correct, MS Excel flashes anr emrreossage. • There are different functions in MS Excel, eachi nhga vcategories. • The most common and most used are the mathemtartigicoanl/ometrical functions, as well as the statistical functions Self-Assessment Questions (SAQs) for study sess7io n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the followinge sqtuions. Write your answers in your study Diary and discuss them with your Tutor at ntheext study Support Meeting. You can check your Define School answers with the Notesth oen S elf-Assessment questions at the end of this Module. Post -Test 1. Write an MS Excel formula to obtain i. the arrangement of 5 items taken 2 at a time ii. the selection of 5 items taken 2 at a time 71 UNIVERSITY OF IBADAN LIBRARY iii. the medium of 1, 2, 1, 1, 3, 1, 5, 2 iv. the modulo of 13 divided 3 v. the standard deviation of (iii) above vi. the correlation of (hypothetical) data in cells tBo1 B 5 and D1 to D5. vii. empty cells in cells A15 to K 15 viii. the truth value of cells B3 less than 3 and C4 tgere tahan 2. ix. the slope of a regression line x. the kurtosis of a group of data 2. A researcher wishes to know whether a stud e1nQt’s depends on his age, and thus obtained information on five randomly selected estnutds from five departments. Age (Y) Y1 Y2 Y3 Y4 Y5 Performance (X ) X1 X2 X3 X4 X5 Assuming he uses the Microsoft Excel package, atonrde ss Age variables in cells A2 to A6, and performance variables in cells B2 to B6, whthile headings, Age and Performance occupy cells A1 and B1 respectively. Help him irnit iwng expressions to i. Store the squares of performance variables in Cce2l lsto C6. ii. Store the product of Age and Performance variaibnl ecse lls D2 to D6. iii. Store the sum and mean of Age variables in cell sa nAd8 A9 respectively. iv. Store the sum and mean of performance variablceesl lisn B8 and B9 respectively. v. Store the sum of the squares of Performance vaersi ainb lcell C8. vi. Store the sum of the products of Age and Performe avnacriables in cell D8. vii. Store the number of data entered, of either vaer,ia inb lcell F9. 72 UNIVERSITY OF IBADAN LIBRARY 3. Display with MS Excel a compound bar chart thfoer following data INCOME EXPENDITURE 25,000 25,500 15,000 13,000 30,000 25,000 45,000 44,000 35,000 36,000 20,000 17,000 Reference The Microsoft Excel SoftwareE: xcel 2003 73 UNIVERSITY OF IBADAN LIBRARY Study Session 8: Algorithm and Flow Chart Introduction Everything has a way of approach. The tendencyo isfi ntd an easy means of solving a problem. Most problems are solved starting with aalgnorithm, and then a flowchart. A problem solved with algorithm and flowchart is alsmt osolved. Learning Outcomes from Study Session 8 At the end of this study session, you should be atob:l 3.1 Write an algorithm to solve problems 3.2 Algorithm with a flowchart. Pre-Test 1. Write an algorithm for the preparation oEf b“a” 2. Write an algorithm to sum five digits 3. Draw a flowchart for the preparation oEf b“a” 4. Draw a flowchart to sum five digits 74 UNIVERSITY OF IBADAN LIBRARY 8.1 Algorithm An algorithm is a step-by-step method of solvinpgr oab lem. It does not use any special code other than the languages we speak. Algorithm idse sac ription on how to approach a situation. For example, I could offer help to somn e going to UI from Lagos by way of describing how to get to UI. In doing this, whaot Id tell him? 1. Get to the motor garrage and board a vehicle gtoi nIwg o Road in Ibadan. 2. From Iwo Road, board a vehicle going to Agbowo –o oO rjoute. 3. Drop at Agbowo junction on the express, and ennteorth aer vehicle going to UI. 4. Drop at the gate and enter the campus to the torartn usnpit, and enter a vehicle going to your destination on campus. There is no general rule in writing an algorithmo.m Setimes an algorithm can be written in form of a real program. As long as it provides ans ye way to (draw flowchart and) writing the actual program, an algorithm is accepted. Example 1: Write an algorithm to compute n! i. Write down the digit n ii. Subtract 1 from n and multiply the result with n iii. Subtract 1 from the result of the subtraction i)n a(bi ove and multiply the value with the multiplication result in (ii) iv. Continue the process until the result of the sucbtitorna becomes 1. The above algorithm tells you thna!t is nx ( n−1) x( n− 2) x...x( n− n+ 1) 75 UNIVERSITY OF IBADAN LIBRARY Assumen = 5, then 5! = 5 x 4 x 3 x 2 x 1 Example 2 : Write an algorithm to calculate the area of ian tgrle given the base and the height. Solution 1. Multiply the base with the height 2. Divide the result by 2 In-Text Question What is Algorithm? In-Text Answer An algorithm is a step-by-step method of solvinpgr oab lem 8.2 Flowchart Just as the algorithm, flowcharts are graphic mdest hoof solving a problem, by a sequence of operations. A graphic is a two-dimensional picatlo rfiormat. They serve as a means of communication, telling how an operation should beerf oprmed. The name flowchart comes about from the use of charts to display the ord pearlsysing of control from one operation to the next in an explicit sequence. Prior to the advent of computers the name ‘flowtc’h waras used by systems analyst to designate a means of describing the flow of docutsm ecnarrying data in an organization. Nowadays we use it in describing the operationsth oe f computer. Flowchart comes in a variety of names such as logic chart, run diagrparmoc, ess chart, flow diagram, procedure chart, block diagram, system chart, and logic daimag, ramidst many others. Flowchart is adaptable to any kind of program oer roaption. Like in algorithm, the normal language (e.g. English Language) that you speauks eisd to describe the processing of an 76 UNIVERSITY OF IBADAN LIBRARY operation of a flowchart. It is a foundation fori twing programs, and is most useful in a team work as all members of the team understand its ulagneg regardless of their training in programming language. 8.2.1 Flowchart Symbols The basic flowchart symbols include the following i. Start/Stop (A flattened ellipse) This is used to indicate the beginning and endoifn ag process. ii. Input/Output (A parallelogram) This is used for input and output processes. Ynopu ti data, and output results. iii. Processing (A rectangle or square) This is used in arithmetic and/or logical process isne an operation. iv. Connector (A small circle) This is used in connecting two sequences of flowrtcsh tahat are broken due to insufficient space. v. Decision (A kite) This is used in setting a logical rule. To make this charts meaningful, arrows, are use jdo into one chart with the other. This is where the flow comes from, and hence, the namew c“fhloart”. Example 3 : We shall draw a flowchart for the calculation thoef area of a triangle in example 3. Start Start 77 UNIVERSITY OF IBADAN LIBRARY Input base Input b, h Let K = b x h Input height Multiply Let A = K/2 Divide your Print A Stop Print Area Stop Example 4 : Draw a flowchart for example 1. Star Input Is Yes M N > 1? 78 LET N = M-1 UNIVERSITY OF IBADAN LIBRARY No Example 5 : Draw a flowchart to calculate the mean of ao sf e5t digits. Start Start LET S = O LET M = S/5 Read FOR I = I TO 5 Let S = A + B + C + D + E Input K LET S = S + K (I) Let M = S/5 Stop PRINT M IS YES Stop NO PRINT Mean = M PRINT MEAN = M 79 UNIVERSITY OF IBADAN LIBRARY Summary for 8 • In solving a problem, the easiest way of approasc hb yi the use of algorithm and flowchart. • An algorithm is a step-by-step easy method of dibeisncgr the solution to an event or process. It has no code other than the languaug es pyeoak. • On the other hand, a flowchart is a graphic metohfo do peration of a process. It uses charts (a two-dimensional pictorial diagram) in cdreibsing an operation. • The language used in a flowchart is not differeronmt f that used in the algorithm. • Flowchart is easy to be understood especially iass v iet ry useful in team work, where all members of the team need not be programming ex. perts Self-Assessment Questions (SAQs) for study sess8io n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the following quoensst.i Write your answers in your study Diary and discuss them with your Tutor at the nsetuxtd y Support Meeting. You can check your Define School answers with the Notes on the Seslfe-Assment questions at the end of this Module. Post-Test 1. Write an algorithm and draw a flowchart to solve ftohllowing: i. the area of a circle ii. the area of a triangle given the base, height ann adn agle iii. the quadratic equation using its formula iv. the standard deviation of a set of 5 data v. the geometric and harmonic mean of 5 digits vi. the union of two sets given that the events arep inedndent vii. the sum of two probabilities given that the eveanrtes not independent. 80 UNIVERSITY OF IBADAN LIBRARY 2. Differentiate between an algorithm and flowchardt awnrite down their uses. References Cooke D., Craven A. H. and Clarke G.. :M Basic Statistical Computing. Second Edition. Edward Arnold. A division of Hodder and Stough.t on Ojo S. O. (1991):I ntroduction to Computer Scien.c e Revised Edition. Department of Computer Science, University of Ibadan, Nigeria. 81 UNIVERSITY OF IBADAN LIBRARY Study Session 9: Review of the BASIC Programming Language Introduction The BASIC programming language is an acronym for GBINENERS ALL- PURPOSE SYMBOLIC INSTRUCTIONAL CODE. It is called the staert’s code because it is a rudimentary lesson for the learning of programmlainngg uage. It is easy to learn. Instead of being phased out, as most early langsu, aitg reather undergoes modification. That is the reason for the different versions we have t oldikaey the QBASIC, GWBASIC and visual BASIC. The version we shall be reviewing here is the QBCA, SwIhich shall, alongside the MS Excel, be the main software for our analyses here (espe caiasl lwy e get to the aspect of statistical analyses from the next chapter). Learning Outcomes from Study Session 9 At the end of this study session, you should be atob:l 9.1 identify a BASIC program; Pre-Test 1. What are the type of instructions a BASIC promg rcaan accept? 2. What is a variable? 3. Return the output of the following program: 10 Read A, B, C, D 20 M = A + B + C + D 82 UNIVERSITY OF IBADAN LIBRARY 30 PRINT M 40 DATA 3.1, 2.7, 4.0, 1.5 50 END 9.1 The BASIC Program The BASIC program is essentially made up of a sf eitn sotructions logically sequenced together. These instructions are common to all ro tphreogramming languages, and they include; i. Input – this instruction reads data into the cpoumter, which is considered variable because its constants are not known during progwrraitmin g. ii. Processing – this instruction carries outh amriet tic and logical operations on data. iii. Output – this instruction produces informat ifornom the data that has been processed. To run a BASIC program, a BASIC interpreter is neede.d Fundamental Rules for Basic Programs i. Each instruction is written as a separate steantet.m ii. Every statement must appear on a separate line. iii. A statement cannot exceed one line in length. iv. Each statement must begin with a positive ginetr v. Successive statement must have increasing setante nmumbers. vi. Each statement number must be followed by a IBCA kSeyword, indicating the type of instruction to be carried out. 83 UNIVERSITY OF IBADAN LIBRARY vii. Blank spaces may be inserted as desired toro ivmep readability. viii. Statements are executed in statement numebqeur esnce unless a deliberate “jump” is indicated. ix. On most computers, programs must end with anD EsNtatement. x. Numeric variable names can be represented ebtyt ear lor a letter followed by a digit. xi. Arithmetic operation are coded with the follonwgi symbols in a LET Statement: + (Addition), - (subtraction), *(multiplication), /(iDvision), ** or ^ (Exponentiation). xii. Numeric constant can be used in arithmeticte smtaents xiii. To branch to a different place in a prograwme, use GOTO statement. Fundamental Concepts of the BASIC Language 1. Character set- these are alphabet (A to Z), nriucm ( e0 to 9) are special characters like ! @, $, #, %, &, +, >, ?, {, and as on. 2. Constants - these are exact data elements esdu ptpol iBASIC which could be a string or numeric constant. String constants are enc loins eqduotation marks. A numeric constant can be integer, fixed points, and/or ifnloga pt oint. Integer constants are whole numbers with or wit hao uptrefix + or – sign. Fixed point constants are positive or negative real (decimuaml) bners. Floating point constants are positives or negantiuvme ber represented in exponential form. Fixed point and floating point numbers can be er itshiemple or double precision numbers. Single precision constants has seven of fewer wdiigthit an exponentiation form using E, or a trailing exclamation mark (!), while a double prseiocin constant has eight or more digits with an experimental form using D, or a trailing numsb seirgn (#). Examples : 84 UNIVERSITY OF IBADAN LIBRARY Single Precision Double Precision 4815.0 2033411.2013 1.52! 2208.0# 45.7 3105112590 -2.11E-05 -1.23056D-07 3. Variables: A variable is a name that represean ntsu mber or string. It can be called a storage compartment in a program where a valuela cise dp. A variable can be a numeric variable or string variable depending on the typf ev aoriable stored. Ideally, a numeric variable is a letter, or letter followed by an igneter, while a string variable is a letter followed by a dollar sign. Writing Simple Programs The reader is expected to consult relevant BASIoCg rparmming books as this text is just a review. You are not expected to learn BASIC heruet, ibts applications. However, listed below are BASIC statements: REM INPUT READ-DATA PRINT LET GOTO IF-THEN FOR-NEXT ON-GO-TO STOP DIM GOSUB- END RANDOMIZE RESTORE RETURN STEP TO TROFF TRON DOWHILE CLS ELSE ENDIF CALL CLOSE DEF DO- FUNCTION- ONxGOSUB OPEN file FOR 85 UNIVERSITY OF IBADAN LIBRARY LOOPUNTIL ENDFUNCTION OUTPUT PRINT USING SELECT CASE SUB-ENDING WRITE AS The arithmetic operators used by BASIC have beepnla einxed earlier. These are Addition; + Subtraction: _ Multiplication; * Division: / Exponentiation: ** or ^ While the relational operators include Equal to: = Less than: < Greater than: > Less than or equal to: <= Greater than or equal to : >= Not equal to: < > Relational operators are used in logical or coonndaitil statements. Look at these simple programs: Example 1: Write a program that computes the area of a recleta ng 86 UNIVERSITY OF IBADAN LIBRARY 10 REM This program computes the area of a recleta ng 20 INPUT L, B 30 LET A = L * B 40 PRINT “Area is “; A 50 END Example 2 : Assuming data is given for the length and breasdathy, 25 and 16 respectively. Write another simple program that can solve this problem. 10 REM this program computes the area of a recleta ng 20 READ A, B 25 LET A = L * B 30 PRINT “Area is”; A 35 DATA 25, 16 40 END The difference between these two programs is thailte wthe first can solve any given data, the second is restricted to only two given daeta. ,2 i.5 and 16. The INPUT statement is shown here to be more flexible than the READ –DATA staetenmt. Example 3: The graduating diploma students of the Univerosift yIb adan are to be determined whether they are eligible to apply for direct entry. Givethna t the pass mark is 60, write a simple BASIC program that determines this. 87 UNIVERSITY OF IBADAN LIBRARY 10 REM this program prints names of graduatinglo dmipa students and 15 REM determines whether they are eligible tol ya pfopr direct entry 20 INPUT N$, S 30 IF S> = 60.0 then 40 35 IF S< 60.0 then 50 40 Print N$, “Eligible to apply for direct entry” 50 Print N$, “Not Eligible to apply for direct erny”t 55 GO TO 20 60 END Example 4: Write a program to compute the factorial of a nurm Nb.e 10 REM Program computes the factorial of a numNb er 20 LET F = 1 30 INPUT N 40 FOR I = 1 TO N 50 LET F = F * X(I) 60 NEXT I 70 PRINT “N! is”; F 80 END Example 5: 88 UNIVERSITY OF IBADAN LIBRARY The next program prints the sine and cosine ofn agnle a and calculates its tangent. 5 REM program prints the sine and cosine of anle a, nagnd 10 REM Calculates the tangent 20 INPUT K 30 LET A = SIN (K) 35 LET B = COS (K) 40 LET C = A / B 50 PRINT K 55 PRINT” Sine of K is “; A; “Cosine of K is “; B 60 PRINT “Tangent of K is”; C 70 END Example 6: Program that computes and prints group average s cor 5 REM program computers are prints group ave sracgoere 10 FOR I = 1 To 10 15 LET F = 0 20 FOR J = 1 TO 30 25 INPUT K 30 LET F = F + K 35 NEXT J 40 LET A = F / 30 89 UNIVERSITY OF IBADAN LIBRARY 45 PRINT “Group”; I; “Average is”; A 50 NEXT I 60 END Summary for 9 • BASIC programming language is a starter’s code minega BnEGINNERS ALL-PURPOSE SYMBOLIC INSTRUCTIONAL CODE. • BASIC is easy to learn and is not phased out, abtuhte r undergoes modifications. • It is made up of a set of instructions (just likteh eor programming languages) logically sequenced together. • These are the input, processing and output insiotrnusc.t • BASIC has fundamental rules that are needed ftoor wit ork (a good grasp of these rules is needed). Self-Assessment Questions (SAQs) for study sess9io n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the following quoensst.i Write your answers in your study Diary and discuss them with your Tutor at the nsetuxtd y Support Meeting. You can check your Define School answers with the Notes on the Seslfe-Assment questions at the end of this Module. Post Test 1. Write a program that computes the standard dtioenvi aof a named student. 2. Write a BASIC program that computes the simnptle riest of a named client. 90 UNIVERSITY OF IBADAN LIBRARY 3. Differentiate between the floating and fixedn pt ocionstants. 4. Differentiate between the single and double ipsiroenc constants. 5. Write a BASIC program that uses (a) INPUT (b)A RDE-DATA statements to solve the same problem. 6. Write out a i. symbol you may use for a sgt rvinariable. ii. symbol you may use for a numeric variable References Cooke D., Craven A. H. and Clarke G. M. BOa: sic Statistical Computin. gSecond Edition. Edward Arnold. A division of Hodder and Stough.t on Ojo S. O. (1991):I ntroduction to Computer Scien.c e Revised Edition. Department of Computer Science, University of Ibadan, Nigeria. 91 UNIVERSITY OF IBADAN LIBRARY Study Session 10: Descriptive Statistics Introduction In the early days of statistics, not much was dwointhe gathered data other than using the raw data itself to describe certain phenomenon in othrme fof charts and simple calculation like the averages, ratios, proportions, percentagesin adnedx numbers. Because these simple result and charts describ seusm omr arizes the data and the object it is representing, they are rightly called descriptivtaet istics. However, over the years it has undergone some expansions to include frequencryib duitsiot n, measures of central tendencies (location, petition and dispersion, and measursek oefw ness and kurtosis. Learning Outcomes from Study Session 10 At the end of this study session, you should be atob:l 10.1 Explain the Rounding of Numerical Data Pre-Test 1. Mention three charts you are familiar with ins cdreiptive statistics, and sketch them. 2. Differentiate between the mean, median and m ode. 3. Distinguish between grouped and ungrouped data. 4. What is a deviation? 5. List the deviations you are familiar with. 92 UNIVERSITY OF IBADAN LIBRARY 10.1 Rounding of Numerical Data Statistical data comes from counting or measure.m Menetasurement does not always give accurate figures, so an approximate or round fosr mof tien appreciated. Hence, rounding is simply approximating numbers in such a way as ptola rcee the affected digits by zero. This makes the number clearer and understandaobrle e. xFample, in giving the number of accident casualties, one may not be exact witha cthtuea l figure. Let us say, in actual fact 23 people really died in an accident, a round figufr e2 0o (say; not fewer than 20’) may be given. Look at these two results: 1. 4,034,713 candidates registered for Universnityra ence examination in 1974. 2. 4,000,000 candidates registered for Universnityra ence examination in 1974. We could round to either of the following ways. i. Specific units, e.g. nearest 100, or 1000. ii. Specific significant figures, e.g. 3 significta fnigures. iii. Specific decimal places, e.g. 2 decimal pla ces Example 1 :a. Round 213, 530 to the nearest i. 100 ii. 1000 iii. 4 significant figures iv. 3 significantg fui res b. Round 213.3780 to the nearest 2 decimal places. c. Round 115.3051 to the nearest 3 decimal places. d. Round 115.3051 to the nearest 5 significantr efisg.u Solution a i. 213,500, a ii. 214,000, a iii. 2130,5, 0a iv. 214,000 93 UNIVERSITY OF IBADAN LIBRARY b. 213.38 c. 115.305 d. 115.31 Using the MS Excel, to round up a digit, the syn ista Rx ound Use this command to perform the exercises in exea m1.p l 94 UNIVERSITY OF IBADAN LIBRARY 10.1.1 Error In rounding of numerical data, we are bound to coitm emrror. This is because the rounded figure is an approximate. Error is defined as tihffee rdence between the actual figure and the rounded figure. That is, Error = Actual figure – rounded figure. However, since it is possible to obtain a negavtiavleu e which we are not interested in, we thus seek to obtain the absolute value, which iegsn otrhe negative sign. So we talk of absolute error instead of just ‘error’. Therefore, when we tablko aut error, you should have it in mind that we are referring to absolute error. Assume we define the actual figure as A, roundgeudr efi as B, and absolute error as E, then. 1. Absolute error,E = A− B 2. Relative error,R = E/ B 100E 3. Percentage erroPr, = E / B x 100 or B Using the MS Excel, we define the above (note othnalyt the syntax is presented): 1. Absolute error E, =ABS(A-B) 2. Relative error R, =E/B 3. Percentage error P =(E/B)*100 or =(100*X)/B orP E=RECENT(E/B) or =PERCENT(R) In-Text Question What is Error? 95 UNIVERSITY OF IBADAN LIBRARY In-Text Answer Error is defined as the difference between thea al cfitguure and the rounded figure. The Basic program for the errors defined aboveg aivren below: 1. Absolute error 10 REM Program calculates (Absolute) Error 15 INPUT A, B 20 LET E = A - B 25 PRINT ‘Error is “; E 2. Relative Error 10 REM program calculates Relatives Error 20 INPUT A, B 30 LET E = A - B 35 LET R = E / B 40 PRINT “Relative Error is “; R 3. Percentage Error 10 REM Program calculates percentage Error 15 INPUT A, B 25 LET E = A - B 35 LET R= E / B 96 UNIVERSITY OF IBADAN LIBRARY 40 LET P = R * 100 50 PRINT “Percentage Error is”; P 60 END Note: Write a single program that computes and prihnets et three errors. 10.1.2 Ratios and Percentages Ratios are fractions or decimals that express tvioanrisa in data, irrespective of actual or absolute sizes of the data. On the other hand,e npteargces are ratios per 100. If in a class of 50 students, 32 are male, the ratio and percenotfa gf em ale students in the population is 18 = 9 18 or 0.36 and x 100 or 0.36 x 100= 36%re spectively. 50 25 50 Using MS Excel, since ratio is a fraction, we uhse styntax for calculating a fraction (as in the case of reaction error). Example 2 : Consider the given data. Item Expenditure (N) Food 9, 000 Clothing 2, 000 Housing 3, 500 Recreation 1, 500 Savings 5, 000 Others 4, 000 97 UNIVERSITY OF IBADAN LIBRARY i. Use MS Excel to obtain the ratio and percent aogf e ach item ii. Write a BASIC program to obtain the ratio anedr cpentage of each item. iii. For (i) above, what would your expressions klo lioke if the data are stored in cells A1 to A7 and B1 to B7 for each heading respectiveleyt. tLhe ratios occupy cells C1 to C7 and the percentage cells D1 to D7. Solution i. Item Expenditure Ration Percentage Food 9,000 =9000/sum( ) =(9000/sum( ))*10 0 Clothing 2,000 =2000/sum( ) =(2000/sum( ))*10 0 Housing 3,500 =3500/sum( ) =(3500/sum( ))*10 0 Recreation 1,500 =1500/sum( ) =(1500/sum( ))*10 0 Saving 5,000 =5000/sum( ) =(5000/sum( ))*10 0 Others 41,000 =4000/sum( ) =(4000/sum( ))*10 0 =sum(9000+ … +4,000) ii. 10 REM Program computes and prints Ratios ancde Pnetarges of 15 REM The Expenditures of six items 20 READ A, B, C, D, E, F 30 LET S= A + B + C + D + E + F 40 FOR I = 1 TO 6 45 INPUT N$, X 50 LET R = X / S 55 LET P= R * 100 98 UNIVERSITY OF IBADAN LIBRARY 60 PRINT “ ”; N$ 65 PRINT “Ratio is”; R; “Percentage is”; P 70 NEXT I 80 END 90 DATA 900, 3500, 1500, 5000, 4000 Another method is given below 10 REM Program computer and prints Ratios aenrdc ePntages of 20 REM The Expenditures of six items 30 LET S =0 40 FOR 1 = 1 TO 6 45 INPUT X 50 LET S = S + X 60 NEXT I 70 FOR J= 1 TO 6 75 INPUT N$, X 80 LET R = X / S 85 LET P = R * 100 90 PRINT “ ”; N$; “Ratio is”; R; “Percentagse” ;i P 100 NEXT J 110 END 99 UNIVERSITY OF IBADAN LIBRARY This second program is more flexible than the fiinrs tthat it can accept any data, unlike the first program that is restricted to the given d ata. iii. Item Expenditure (N) Ratio Percentage Food 900 =B2/B8 =C2*100 or =PERCENT(C2) Clothing 2000 =B3/B8 =C3*100 or =PERCENT(C3) Housing 3500 =B4/B4 =C4*100 or =PERCENT(C4) Recreation 1500 =B5/B8 =C5*100 or =PERCENT(C5) Saving 5000 =B6/B8 =C6*100 or =PERCENT(C6) Others 400 =B7/B8 =C7*100 or =PERCENT(C7) =SUM(B2:B7) Measures of Location The Arithmetic mean (generally called the meanth) eis a verage of all members of a data. x + x + ...+ x ∑ x x = 1 2 n = n n In Excel, the syntax is = AVERAGE(X1, X2, ...., Xn) or = (X1+X2+...+Xn)/2 or = (X1+X2+...+Xn)*0.5 or = (Sum(X1, X2, ..., Xn))/2 or = (Sum(X1, X2, ..., Xn))*0.5 Using cell references, we write 100 UNIVERSITY OF IBADAN LIBRARY = AVERAGE (cell 1: cell N) or = (Sum(cell 1:cell N))/2 or = (Sum(cell 1:cell N ))*0.5 Programming in BASIC, we write 10 REM mean of a set of data 20 INPUT N 30 S = 0 40 FOR I= 1 TO N 50 INPUT X 60 S = S + X 70 NEXT I 80 M = S/N 90 PRINT “Mean =”; M 100 END A second method is using the READ-DATA statemenet nw hlarge data is to be computed. This is to avoid mistakes when typing in the dahtae nw prompted. 5 REM computation of mean six observnast io 10 S = 0 15 FOR I = 1 TO 6 20 READ X 101 UNIVERSITY OF IBADAN LIBRARY 25 S = S + X 30 NEXT I 35 M = S / 6 40 PRINT “Mean=”; M 45 END 50 DATA 3.63, 5.71, 8.02, 6.62, 4.14, 1.91 Yet another way of writing the above is 10 REM program computes mean 20 READ N 30 S = 0 40 FOR I = 1 TO N 50 READ X 60 S = S + X 70 NEXT I 80 M = S / N 90 PRINT “Number of observation =”; N 100 PRINT “Mean =”; M 110 END 120 DATA 6 130 DATA 3.63, 5.71, 8.02, 6.62, 4.14, 1.91 102 UNIVERSITY OF IBADAN LIBRARY We could also write MS Excel expression as wellB aAsS IC program for geometric and harmonic mean. The geometric mean is given as n 1 n n ∏ Xi or ∑ ln X i i=1 n i =1 MS Excel returns the geometric mean for its sy;n tax = GEOMEAN(cell 1:cell N) or = GEOMEAN(X1, X2, ...., XN) Also, we can first find the product: = PRODUCT(X1, X2, ...., XN) or = (X1* X 2*...* X N) Then we obtain then th root = (PRODUCT(X1, X2, ....., XN))**(1/N) or = PRODUCT(X1, X2,... XN)^(1/N) or = (X1*X 2* ...* X N)**(1/N) or = (X1*X 2*....*X N)^(1/N) Using the last BASIC programme for the arithmetieca mn, we can write for geometric mean by modification. 10 REM Geometric mean 103 UNIVERSITY OF IBADAN LIBRARY 20 READ N 30 S = 1 40 FOR I= 1 TO N 50 READ X 60 S = S * X 70 NEXT I 80 K = 1 / N 90 M = S * K 100 PRINT “Number of observation =”; N 110 PRINT “Geometric mean=”; M 120 END 130 DATA 6 140 DATA 3-63, 5.71, 8.02, 6.62, 4.14, 1.91 10.1.3 The Median This is the middle number in a given set of num boerr sof a frequency distribution when arranged in order of magnitude. For an ungrouped data, When odd, medianx%, = xm where xm is the middle number. x + x When even, medianx%, = m n , if m≠ n and xm, xn are the middle numbers. 2 For a grouped data, 104 UNIVERSITY OF IBADAN LIBRARY  N  −∑ f  i  Median, x% = L1 + 2 C  fmed    Where the symbols are as defined in the approp croiauterse. The expression for median in MS excel is given as = MEDIAN(Cell 1:Cell N) or = MEDIAN(X1, X2, ..., Xn) The Mode This is the number that occurs most in a distroibnu.t i For an ungrouped data The mode is obtained by counting the value of tuhme bners separately for a grouped data  D  Mode, xˆ = L + 11  C  D1 + D2  Where the symbols are as defined in the approp croiauterse. The MS Excel command is = MODE(Cell 1: Cell N) or = MODE(X1, X2, ..., Xn) Measures of Partition 105 UNIVERSITY OF IBADAN LIBRARY The other name for the measures of partition ios ‘aQlsuantiles’, which are a useful class of descriptive statistics. They are observations dthivaitd e, in specified proportions, the total frequency of a set of observations. The commonfe qsut aontities is the ‘median’. The various measures of partition used are: 1. The quartiles which divide the total frequency into quarters f(oourr parts) 2. Thed eciles which divide the total frequency into ten parts. 3. Thep ercentile which divide the total frequency into one hundpreadrt s. In-Text Question What is Mode? In-Text Answer This is the number that occurs most in a distroibnu.t i For the MS Excel, we write only for quartiles anedr cpentiles. The Quartile = QUARTILE (array, quart). That is = QUARTILE(Cell 1:Cell N, quart) = QUARTILE (X1, X2, ...., Xn, quart) The percentile = PERCENTILE(array, K). That is = PERCENTILE(X1, X2, ..., Xn, K) 106 UNIVERSITY OF IBADAN LIBRARY The general BASIC Program for any quartile is g ivbeenlow. Note that Q stands for the quantile, and P for the part of quantile. E.g. ,P Q=1=2 result in the median, P= 25, Q= 100 results in the 2t5h percentile P=7, Q=10 results in thteh d7ecile, and P= 1, Q=4 results in the 1st quartile. 5 REM Quartiles of a set of data 15 REM INPUT: Data A (.), number of data N, 30 REM P, Q for P-th Q-tile (P= N GO TO 70 60 NEXT I 70 PRINT “Number of elements in the set =”; I 80 END 90 DATA X(1), X(2), …, X(N) With respect to example 1, we want to write a seim pBlASIC program to compute a probability. 10 REM Program calculates probability 20 REM INPUT: Date X(.) and Y(.), number of dat,a M N 30 REM OUTPUT: Probability 40 REM variables – Integer I, J – Real P 45 INPUT N, M 50 FOR I = 1 TO N 60 READ X(I) 70 X(I) = I 126 UNIVERSITY OF IBADAN LIBRARY 80 NEXT I 90 FOR J = 1 TO M 100 READ Y(J) 110 Y(J) = J 120 NEXT J 130 P = I / J 140 PRINT “Probability of …. =”; P 150 END 160 DATA X(1), X(2),…, X(N) 170 DATA Y(1), Y(2),…, Y(M) A simple approach is given below: 10 REM Program calculates probability 20 INPUT N, M 30 P = N / M 40 PRINT “Probability of …=”; P 50 END The difference between these last two programsh aist tthe first counts the number of elements in the two sets before computing the pbriolitbieas. But the second inputs directly the number of elements in the two sets and com pthuete psrobability. Addition Law of Probability This is given by P( A∪ B) = P( A) + P( B) − P( A∩ B ) The simple process in any application we wish teo ius sto 127 UNIVERSITY OF IBADAN LIBRARY 1. Calculate the probability of seAt 2. Calculate the probability of seBt 3. Calculate the probability of the intersection s oetfs A and B 4. Calculate the probability of the union of seAts a nd B by obtaining the result of the linear combination on the RHS. In MS Excel, the procedure for computinPg( A) , P( B) and P( A∩ B) is preceded by writing the command to count the elements in thiev eurnsal sets,S , and the elements in sets A and B respectively, so that P( A) =COUNT(A)/COUNT(S) P B ( ) =COUNT(B)/COUNT(S) and P( A∩ B) =COUNT(A∩ B)/COUNT(S) Therefore, P( A∩ B) =(COUNT(A)/COUNT(S)+(COUNT(B)/COUNT(S))- (COUNT(A∩ B)/COUNT(S)) For instance, consider the spreadsheet below B C D E 128 UNIVERSITY OF IBADAN LIBRARY S A B A∩ B 1 11 12 11 13 2 12 13 13 15 3 13 15 15 4 14 16 5 15 6 16 7 8 9 n( S) n( A) n( B) n( A∩ B ) 10 P( A) P( B) P( A∩ B ) 11 P( A∪ B) You are required to store the results of the comatpiounts in the cells indicated. Cell B9 is for n( S) , ∴ B9 =COUNT(S) Cell C9 is for n( A) , ∴ C9 =COUNT(A) Cell D9 is for n( B) , ∴ D9 =COUNT(B) 129 UNIVERSITY OF IBADAN LIBRARY Cell E9 is for n( A∩ B) , ∴ E9 =COUNT(A∩ B) Cell C10 is for P( A) , ∴ C10 =C9/B9 Cell D10 is for P( B) , ∴ D10 =D9/B9 Cell E10 is for P( A∩ B) , ∴ E10 =E9/B9 Finally, Cell B11 is for P( A∪ B) , ∴ B11 =C10+D10–E10 11.2 Mutually Exclusive Events It is possible that there is no intersection betnw eAe and B , that is the two events cannot occur simultaneously: In such caseP ( A∩ B) = 0. This results in the addition law of probabilbitey coming P( A∪ B) = P( A) + P( B) Independent Events Here, the eventsA and B are independent of each other. That is the tweon tesv can exist simultaneously. The occurrence of one cannot pnrte tvhe occurrence of the other. Therefore P( A∩ B) = P( A) P( B), also called multiplication law of probability alpicpable to independent events. Thus the addition law of pbroilibtya becomes P( A∪ B) = P( A) + P( B)− P( A) P( B) Conditional Probability 130 UNIVERSITY OF IBADAN LIBRARY Two events cannot be both independent and mutueaxcllylu sive. It has to be one of the two. But it is not a general rule. One event occurrcinagn be based on the occurrence of the other. An example is that a student is given a registnra tfiorm on the condition that he has registered. Let the event of a student registebrien gR , and the event that he is given a registration form beF , so the conditional probability that a studengt iivse n a registration form given that he has registered is writtPen( F / R) . When this happens, the multiplication and addition laws are respectively written as P( A∩ B) = P( A) P( B/ A) P( A∪ B) = P( A) + P( B) - P( A) P( B/ A) The Factorial Notation The factorial of any numbenr is the product ofn and all its immediate lower digits until 1. I t is written as n! = n(n-1)(n- 2)(n-3)...4.3.2.1 In MS Excel, the factorial is calculated using dtheefi ned function FACT. The syntax is =FACT(number) Example 4 :the factorial of 5 is calculated thus, =FACT(5)i cwhh returns 120. The alternative is to multiply the digits 5,4,3,n2d a 1. The BASIC program is 131 UNIVERSITY OF IBADAN LIBRARY 10 REM Factorial of N 20 INPUT N, M 30 F = 1 40 FOR I = 1 TO N 50 READ X(I) 60 F = F * X(I) 70 NEXT I 80 PRINT “N factorial =”; F 90 END 100 DATA X(I), X(2),… X(N) Permutation This is an arrangement of a number of objects dine fain ite order. The factorial oNf is an example of the arrangement Nof objects in N ways if all the objects are to be taken at one time. If R of the N distinct objects are to be taken at a time, thheen ntumber of arrangement is known as permutation, denoted by N N !PR = ( N − R)! So the permutation of 5 objects taken 3 at a tism geiv ien as 5 5!P3 = = 5! = 60 (5− 3) ! 2! In MS Excel, the permutation oNf objects takenR at a time is written as =PERMUT(N, R) 132 UNIVERSITY OF IBADAN LIBRARY 5 E.g. P3 =PERMUT(5, 3) Its BASIC programming can be given as 10 REM permutation ofN objects takenR at a time 20 INPUT N 25 INPUT R 30 F = 1 40 K = I 50 FOR I = 1 TO N 60 READ X(I) 70 F = F * X(I) 80 NEXT I 90 FOR J = 1 TO (N-M) 100 READ Y(J) 110 K = K * Y(J) 120 NEXT J 130 P = F / K 140 PRINT “N Permutation R =”; P 150 END 160 DATA 1, 2, 3, …, N 170 DATA 1, 2 …, (N-R) 10 REM Combination of N objects taking R 20 INPUT N 30 INPUT R 40 F = 1 50 K = 1 60 S = 1 70 M = N-R 133 UNIVERSITY OF IBADAN LIBRARY 80 FOR I = 1 TO N 90 READ A(I) 100 F = F*A(I) 110 NEXT I 120 FOR J = 1 TO R 130 READ B(J) 140 K = K*B(J) 150 NEXT J 160 FOR L = 1 TO M 170 READ C (L) 180 S = S*C(L) 190 NEXT L 200 P1 = K*S 210 P = F/P1 220 PRINT “N Combination R = ;”P 230 END 240 DATA A(1), A(2), …, (A(N) 250 DATA B(1), B(2),…, B(M) 260 DATA C(1), C(2),…, C(M) Combination This is the selection oNf objects takenR at a time. In this case order is not of any ienstet.r This is written as N N = PR = N !CR R! R! ( N− R)! 5C2 = 5C3 = 5! =10 Example: 2!3! 134 UNIVERSITY OF IBADAN LIBRARY In MS Excel, it is written as = COMBIN(N, R) E.g. 5C2 = COMBIN(5,2), and 5 C3 = COMBINE(5,3). Both returns 10. The BASIC format may be written as 10 REM Combination 20 INPUT N, M 30 F = 1 40 R = I 50 S = I 60 FOR I = I TO N 70 READ A(I) 80 F = F * A(I) 90 NEXT I 100 FOR J = I TO M 110 READ B(J) 120 R = R * B(J) 130 NEXT J 140 FOR K = I TO N – M 150 READ C(K) 160 S = S * C(K) 170 NEXT K 180 P1 = F 190 P2 = R * S 200 P3 = P1 / P2 135 UNIVERSITY OF IBADAN LIBRARY 210 PRINT “F Combination R =”; P3 220 END 230 DATA A(1), A(2), …, (A(N) 240 DATA B(1), B(2),…, B(M) 250 DATA C(1), C(2),…, C(M) Summary for 11 Probability is a game of chance. Probability oyf aenvent lies between 0 and 1. In computing the probability of any event, the procedure for cougn ttihne number of elements in the sets is very important. Permutation and combination are intle gprarts in the computation of many probabilities. While permutation deals with arreamngent with regard to order, combination deals with selection with no regard to order. Self-Assessment Questions (SAQs) for study sess1io1n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the following quoensst.i Write your answers in your study Diary and discuss them with your Tutor at the nsetuxtd y Support Meeting. You can check your Define School answers with the Notes on the Seslfe-Assment questions at the end of this Module. Post-Test 1. Write a BASIC program that computes a combinica.t or 136 UNIVERSITY OF IBADAN LIBRARY 2. Given that A and B are two independent events with respective prolibtiaebs i 0.3 and 0.45. Write a BASIC program to compute (Pa() A∩ B) (b) P( A∪ B) (c) P( A/ B) . 3. Consider the universal seSt,= (1, 2, 3, 4, 5, 6, 7, 8, 9, 1)0. ) Two events,A and B , are drawn from S . A = (2, 4, 5, 6, 7), B = (6, 1, 9). Use the MS Excel to compute (a)P ( A) (b) P( B) (c) P( A∪ B) (State clearly the steps needed to arrive at the results) References Adamu S. O. and Johnson T. L. (198S5t)a: tistics for Beginners, Book. S1econd Edition. Lagos: KOLA Publishers Limited, Nigeria. Cooke D., Craven A. H. and Clarke G. MB.a: sic Statistical Computin. gSecond Edition. Edward Arnold. A division of Hodder and Stoughton. Omotosho Y. (1990):C ollege and University Text Statist.i cs Ibadan: NPS Educational Publishers Limited, Nigeria. Ross S. (1994): A First Course in Probability. rFtho uEdition. Prentice Hall, Englewood Cliffs, NJ 07632. 137 UNIVERSITY OF IBADAN LIBRARY Study Session 12: Probability Distribution Functsio n Introduction The sum of probabilities of different events whoeslem ents are members of a particular universal set always equals to one. The functhioant gt ives rise to the probability of each event is known as the ‘probability distribution cfutinon’. In the case of the discreet random variables, tshseo caiated function is known as the probability mass function (p.m.f)’, while that ohfe t continuous random variables is known as the ‘probability density function (p.d.f)’. Thesiru m (in case of p.m.f) and integral (in case of p.d.f) is called the ‘cumulative distribution funiocnt (c.d.f)’. Learning Outcomes from Study Session 12 At the end of this study session, you should be atob:l 12.1 Define a probability function; 1. compute the probabilities of a given function; 2. write a program for probability functions; and 3. draw a graph of probability functions and their. fc. . d Pre-Test 1. Define a probability distribution. 2. Write out the formulas of three p.m.f and two p .tdh.aft you know. 138 UNIVERSITY OF IBADAN LIBRARY 12.1 Probability Function (The Discreet Case) A probability function of X (a discreet random variablep),( x) , is a mathematical function which assigns positive values to thXe' s in such a way that their sum is equal to 1. This makes a probability function look like a cumulat ifvrequency distribution. The difference is that while probability functiosn ai rational number, in most cases, values that make a cumulative frequency distribution aerael rdigits. Consider the tossing of a fair coin once. The apbroilbities are given as Outcome Probability ( X ) p( x) 1 1 6 1 2 6 1 3 6 1 4 6 15 6 6 1 6 Total 1 1 The function that gives rise to 6 in each case is known as a probability functiohni,l ew the total is known as the cumulative distribution function. Therefore, let us denote the probability functiosn fa, and the cumulative distribution function as F . Then for the discreet case, the probability tXha takes the particular valuxe is given as Pr( X = x) = f ( x) 139 UNIVERSITY OF IBADAN LIBRARY And the probability thatX is less than or equal to some particular valuye ,b s,a is given as F (b) = Pr( X ≤ b) For example b F (b) = ∑ f ( x) ( b≤ n) x=0 x = 0,1, 2, ...,n The second line is for the particular case whxe rnu ns from 0 to n . Also for the continuous cas,e the probability thatX takes a value in the interva(al , b) is given as b Pr(a ≤ X ≤ b) = ∫ f ( x) dx a And the probability thatX is less than or equal to some particular valuye ,b s,a is given as b F (b) = Pr( X ≤ b) = ∫ f ( x) dx −∞ Mean and Variance of a Probability Function Having known the values oXf and their probabilities, we now set forth to onb ttahieir mean and variances. Consider the table below: X p( x) xp( x) x1 p( x1) x1 p( x1) x2 p( x2 ) x2 p( x2) 140 UNIVERSITY OF IBADAN LIBRARY x3 p( x3 ) x3 p( x3) M M M xb p( xb ) xb p( xb ) Total 1 For the discreet case The mean of a probability function is given as µ = x1 p( x1) + x2 p( x2) + x3 p( x3) + ...+ bx p( bx) b = ∑ xi p( xi ) x=1 and the variance given as σ 2 = ( 2 2 2 2x1 − µ ) p( x1) + ( x2 − µ ) p( x2) + ( x3− µ ) p( 3x) + ...+ ( bx− µ ) p( bx) b = ∑( 2xi − µ ) p( xi ) x=1 For the continuous case The mean is given as b µ = ∫ xi f ( xi ) dx −∞ and the variance is given as 141 UNIVERSITY OF IBADAN LIBRARY b σ 2 = 2∫ ( xi − µ ) f ( xi ) dx −∞ Example 1 Use the MS Excel to compute the mean and variaonfc ethse outcomes of tossing two unbiased coins. 142 UNIVERSITY OF IBADAN LIBRARY Solution The possible outcome will include T H x x HH 2H OT 0 2 HT IH 1T 1 OR 1 TH IH 1T TT OH 2T 2 0 We can use either outcomes (H or T) for our comtpiount.a Store the results of outcome in cells, say D4 to, aDn6d their probabilities in cells, say E4 to E6. D4 = 0 E4 = 0.25 D5 = 1 E5 = 0.5 D6 = 2 E6 = 0.25 Observe that the total of their probabilities is 1 Multiply the adjacent cells D_*E_ and store theu rlte isn F4 to F6 F4 = D4*E4 F5 = D5*E5 F6 = D6*E6 Sum up the cells F4 to F6 and store the result7 in F F7 = Sum(F4:F6) F7 is the mean of the outcomes of the throwingt wthoe u nbiased coins. 143 UNIVERSITY OF IBADAN LIBRARY For the variance, obtain the deviations of eachc oomute and store in cells say G4 to G6 G4 = D4–F7 G5 = D5–F7 G6 = D6–F7 Square these new results and store in say H4 t o H6. H4 = G4^2 H5 = G5^2 H6 = G6^2 Multiply each of the above with the probabilitiensd a store in cells, say I4 to I6 I4 =H4*E4 I5 =H5*E5 I6 =H6*E6 Sum up these products and store in cell, say I7 I7 =Sum(I4:I6) I7 now is the variance of the outcomes of the thinrogw of two unbiased coins. Discreet Random Variables The Bernoulli and Binomial distributions We are going to consider these two distributionsc abuese they are related, in that, the Bernoulli is the probability of x successes in a single trial, whereas the binoims iathl e probability of x successes in trials (in both cases the probability of success sp i, while that of failure isq =1− p. 144 UNIVERSITY OF IBADAN LIBRARY The Bernoulli function is given as f ( x) = ( 1x ) px q1− x, x= 0,1 = pxq1− x, since ( 1 10 ) = ( 1) =1 while that of the Binomial function is given as f ( x) = ( n x n− xx ) p q , x= 0,1, 2, ...,n In MS excel, to compute a binomial probability, tshyentax is given as =BINOMDIST(number_s, trials, probability_s, cummatuivl e) This is a direct computation. However, we can au sete p wise computation (having known our p ) by 1. computing the combinatory part 2. computing the px 3. computing theq n− x 4. computing the product of the three results above. For the Bernoulli distribution, the BASIC programe lobw can be used compute the probabilities. 10 REM Bernoulli probability 20 INPUT X, P 30 READ A 40 Q = 1 – P 50 P1 = P ^ X 60 W = A – X 145 UN VERSITY OF IBADAN LIBRARY 70 Q1 = Q ^ W 80 B = P1 * Q1 90 PRINT “Bernoulli Probability =”; B 100 END 110 DATA 1 The Binomial distribution BASIC program is similtaor the Bernoulli. The only difference is that the combinatory part is also computed. 10 REM This program computes Binomial probability 20 REM combination 30 INPUT N, P, M 40 F = 1 50 R = 1 60 S = 1 70 FOR I = 1 TO N 80 READ A (I) 90 F = F * A(I) 100 NEXT I 110 FOR J = 1 TO M 120 READ B(J) 130 R = R * B(J) 140 NEXT J 150 FOR K = 1 TO N-M 146 UNIVERSITY OF IBADAN LIBRARY 160 READ C(K) 170 S = S * C(K) 180 NEXT K 190 P1 = F 200 P2 = R * S 210 P3 = P1 / P2 220 REM Binomial probability 230 Q = 1 – P 240 X = M 250 P4 = P ^ X 260 W = N – X 270 Q1 = Q^W 280 B1 = P3 * P4 * Q1 290 PRINT “Binomial Probability =” ; B1 300 END 310 DATA A(1), A(2), …, A(N) 320 DATA B(1), B(2),…, B(M) 330 DATA C(1), C(2),… C(M) 147 UNIVERSITY OF IBADAN LIBRARY The Poisson distribution The p.m.f is given as x f ( x) = λ e−λ , x= 0,1, 2, ... x! The MS Excel syntax is given as =POISSON(n, mean, cumul) A do it by yourself method is to obtain the valuf e o 1. λ x (having being givenλ ) 2. The exponent of negativλe 3. The product of (1) and (2) above 4. The factorial of x 5. The quotient of the value in (3) and (4) The BASIC program is given below: 10 REM Poisson Probability 20 REM Factorial of X 30 INPUT N, L 40 F = 1 50 FOR I = 1 TO N 148 UNIVERSITY OF IBADAN LIBRARY 60 READ A(I) 70 F = F * A(I) 80 NEXT I 90 X = N 100 B = L * X 110 C = EXP (L) 120 D = F * C 130 E = B / D 140 PRINT “Poisson Probability =”; E 150 END 160 DATA A(1), A(2), …, A(N) The Geometric distribution The print is given as f ( x) = p( − )1−x1 p = pq1−x x= 1, 2, ... The simple BASIC program for this distribution is 10 REM Geometric distribution 20 INPUT X, P 30 Q = 1 - P 40 W = 1 - x 149 UNIVERSITY OF IBADAN LIBRARY 50 Q1 = Q ^ W 60 G = P * Q1 70 PRINT “Geometric Probability =”; G 80 END Continuous Random Variables Most of the common continuous random variables hparvoebability density functions that look complicated. However, their evaluation isa isgthr t forward. One line of program will serve to calculate each of these functions forr taic pualar value of x (or b ). The Exponential distribution The p.d.f is given as f ( x) = λe−λx, x≥ 0 and c.d.f F ( x) = 1− e−λx Look at this 10 REM Exponential distribution 20 INPUT X, L 30 DEF FNP = L * EXP (-L * X) 40 DEF FNC = 1 – EXP (-L * X) 50 PRINT “Probability density function =” ; FNP 60 PRINT “Cumulative distribution function =” ; FCN 150 UNIVERSITY OF IBADAN LIBRARY 70 END The Normal distribution The Normal distribution has p.d.f given as 2 − 1 x−µ  f ( x) = 1 e 2 σ   − ∞ < x < ∞ 2πσ The MS Excel format for calculating the probabi liisty given as =NORMDIST(x, mean, std_dev, cum) The syntax assumes that the mean and standardti odne visia known. You can also use a stepwise procedure. The BASIC program for this function is given asl ofowls: 10 REM The Normal Probability 20 INPUT X, U, R 30 READ P 40 D = 2 * P * R 50 D1 = SQR (D) 60 F = 1 / D1 70 Z = (X – U) / R 80 Z = (Z ^ 2) * 0.5 90 N = G * EXP (-Z1) 100 PRINT “Normal probability =”; N 151 UNIVERSITY OF IBADAN LIBRARY 110 END 120 DATA 3.1428571 Cumulative Function of Continuous Random Variables Evaluating the cumulative distribution function m baey odd and cumbersome since there is not always an equivalent explicit expression sucsh eaxists for the probability density function. So we resort to the computation of inratelsg. And the most appropriate approach is to use numerical integration of the probability sdietyn functions. We shall use the Simpson’s Rule. b Simpson’s rule for approximating the integr∫alf ( x) dx is a h ( f0 + 4 f1 + 2 f 2 + 4f 3 + 2f 4+ ...+ 4fn− 3+ 2f3 n− 2+ 4f n− 1+ f n) which may be written as h (S(1) + 2S( 2) + 4 S( 4) ) 3 where S(i ) , with i =1,2 or 4, is the sum of the function values which have coefficient i. S(1) = f0 + fn which means,S (2) = f2 + f4 + ...+ fn− 4 + fn− 2 S(4) = f1 + f3 + ...+ fn−3 + fn−1 The BASIC programme for numerical integration bmy Spsi on’s Rule is given below. 152 UNIVERSITY OF IBADAN LIBRARY 610 REM Simpson’s Rule 620 INPUT U, L 630 A = 1.0 E-6 640 N = 2: H = 0.5 * (U –L) 650 S1 = FNF (L) + FNF (u) 660 S2 = 0 670 S4 = FNF (0.5 * (L + U) 680 S = H * (S1 + 4 * S4) 690 W = S: N = N + N : H = H / 2 700 S2 = S2 + S4 710 S4 = 0 : I = 1 720 S4 = S4 + FNF (L + I * H) 730 I = I + 2 740 IF I <= N THEN 120 750 S = H * (S1 + 2 * S2 + 4 * S4) 760 IF ABS (S - W) > A THEN 90 770 S = S / 3 The Normal distribution (cumulative function) We shall apply the Simpson’s Rule in evaluating ctuhme ulative distribution function of the normal distribution. The c.d.f is given as 153 UNIVERSITY OF IBADAN LIBRARY z 2 F (z) = 1  x ∫ exp−  dx −∞ 2π  2  The above function assumes standard normal distitornib uwith mean 0, and variance 1. To use Simpson’s Rule we rewrite the integral siny ma metrical form about zero. That is ( )  0 z  1  x2  F z =  ∫ +∫  exp−   dx  −∞ 0  2π  2  z  = 1 x 2  0.5+ ∫ exp−π  dx 0 2  2   10 REM Normal probability (integration) 20 DEF FNF (x) = 0.39894228 * EXP (-0.5 * X ^ 2) 30 L = 0.0: u = z 40 GOSUB 610: REM Integration, Simpson’s Rule 50 P = S + 0.5 60 RETURN This program takes into account that the SimpsoRnu’lse has been included in the program somewhere along the line. So the GOSUB – RETURaNte mstent recalls it. Chart or Graph of Random Variables In plotting the graph of random variables, we muaske of the probability of each event. We can also draw the graph of their cumulative disuttrioibn function. Example 2 154 UNIVERSITY OF IBADAN LIBRARY The problem that deals with the throwing of two good and unbia csoeidns once could be used to illustrate this purpose. We found out that the graph of the probability rdibisuttion is as below : P( x) ¾ 2/4 ¼ 0 1 2 While that of the cumulative functions is as bel ow: F ( x) 1 ¾ 2 /4 ¼ 0 1 2 The broken lines show that it is a discreet funnc.t i oAn example of a cumulative function curve of a continuous random variable is that oef nthormal which is as given belo w: 155 UNIVERSITY OF IBADAN LIBRARY Therefore, in plotting a graph of a probability ms afusnction or probability density function, evaluate the various probability and plot agaihnest vtalues (ofx ) given. And in plotting the graph of cumulative distribution function, sum uhpe tvarious probabilities and plot their points as the summation progresses. The MS Excel chart wizard is a good tool in plogtt itnhese graphs. Summary for 12 • Probabilities are events under uncertainty. • The sum of all probabilities of events from the sea umniversal set equals 1. • The function giving rise to the various probabeilsit iis known as the probabilities mass function (p.m.f) for the discreet case, and theb parboility density function (p.d.f) for the continuous case. • Their sum or integral is known as the cumulativset rdibiution function. In evaluating the cumulative distribution function of the continuoruasn dom variables (using BASIC), the numerical integration approach by Simpson’s Ru lvee irsy useful. • To draw the graph of a p.m.f or p.d.f and c.d.fa, lueavte the various probabilities. Self-Assessment Questions (SAQs) for study sess1io2n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the following quoensst.i Write your answers in your study Diary and discuss them with your Tutor at the nsetuxtd y Support Meeting. You can check your Define School answers with the Notes on the Seslfe-Assment questions at the end of this Module. Post-Test 1 i. Define a probability mass function (p.m.f) ii. Define a probability density function (p.d .f) 156 UNIVERSITY OF IBADAN LIBRARY iii. Define a cumulative distribution function (c.f.)d 2. If 40% of cocoa seeds brought by a produce buye r daerfective, write a BASIC program to compute the probability that out of 5c ocao seeds selected at random, (a) exactly 3 are defective (b) at most 2 are defe.c tive 5% of the inhabitants of a village who were attadc kbey cholera died. Write a BASIC program to compute the probability that out of am pslae of 60 cholera patients selected at random in the village (i) exactly 2 (ii) at let a2s (iii) less than 2 will die. The mean life span of a number of cancer patierenatst etd by one specialist is 72 years. If the life span of the patients are normally distributweidth standard deviation 3.5, use the MS Excel to compute the probability that any cancetire pnat treated by the specialist will live for less than 69 years live for more than 76 years live for between the ages of 74 and 80 years (Write out the steps you need to take) Given that n is large, and p is the probability of success, write a BASIC praomgr that computes the standardizeZd – value of the binomial distribution. Write (i) an MS Excel expression (ii) a BASIC proagmr to evaluate (3) . 157 UNIVERSITY OF IBADAN LIBRARY References Adamu S. O. and Johnson T. L. (1985): Statisticr sB feoginners, Book 1. Second Edition. Lagos: KOLA Publishers Limited, Nigeria. Cooke D., Craven A. H. and Clarke G. M.: Basic iSstiactal Computing. Second Edition. Edward Arnold. A division of Hodder and Stoughton. Omotosho Y. (1990): College and University Text tiSstiacs. Ibadan: NPS Educational Publishers Limited, Nigeria. 158 UNIVERSITY OF IBADAN LIBRARY Study Session 13: Correlation and Linear Regres sion Introduction You often find variables that are related, esplleyc iina economics. For example, it is known that income and expenditure have a relationship. It may not be good enough to examine how some bvlaersia affect others that they are related with, but we can examine the degree of their asastiocni. The degree of association of related variablesa lilse dc Correlation, while that of effect of one variable upon the other(s) is called Regression. sWhaell approach this chapter, and subsequent ones using the Microsoft Excel. Learning Outcomes from Study Session 13 At the end of this study session, you should be atob:l 13.1 Explain the Theory of Correlation Pre Test 1. Mention five pairs of variables that are redla wteith each other. 2. Enumerate the types of correlation you know 3. List the types of correlation coefficients yaorue familiar with. 4. What do you understand by the term ‘Regres?si on’ 159 UNIVERSITY OF IBADAN LIBRARY 13.1 The Theory of Correlation Correlation is simply the degree of association wbeetn two variables. To obtain a meaningful correlation, the variables must be eredl.a Tt here are different types of correlation which are described below. 1. Perfect and positive correlation - This is tchaes e where all points of the scatter diagram fall on a straight line which slopes upwsa frrdom left to right, or downwards from right to left. 2. Perfect and negative correlation - This is cthaes e where all points of the scatter diagram fall on a straight-line which slopes upwsa frrdom right to left, or downwards from left to right. 3. Positive or direct correlation - This is thes ec awhere one variable increases as the other increases in a left-right direction (i.e. ltihne slopes upwards from left to right). 4. Negative or increase correlation - This is ctahsee where one variable increases as the other decreases, or one variable decreases ast htehre inocreases. The line slopes downward in a left- right direction, or upwardsa i nri ght-left direction. 5. Null correlation-This is the case where thenr en oi definite pattern in the direction of the two variables. The different correlation could be illustrated w ait hdiagram 160 UNIVERSITY OF IBADAN LIBRARY 1. 2. * * * * * * * * * * * Perfect and Negative Perfect and positive Caotriroenl Correlation 3. 4. * * * * * * * * * * * * Positive or direct Perfect or Inverse Correlation Correlant io 5. * * * * * * * * * * * * * * Null correlation 13.2 Coefficient of Correlation There are different types of correlation coeffict,i eonf which two are very paramount. These two are: 1. The spearman’s rank correlation coefficient. 2. The Karl Pearson’s Product Moment Correlatione fCficient. 161 UNIVERSITY OF IBADAN LIBRARY The most popular, and often used, of the two i sP trhoeduct Moment Correlation Coefficient. The spearman’s rank correlation coefficient is ng ivaes n 6∑d2i ρ =1− i=1( n n2 −1) The Karl Pearson’s Product moment correlation cicoiefnft is given as cov ρ = ( X ,Y) ∑ ( X − X )( Y− Y)= var( X ) var(Y) ( )2 2∑ X − X ∑ ( Y− Y) XY− X Y = ∑ ∑ ∑  2 ( 2∑ X − ∑ X )   Y2 − ( 2 Y)    ∑ ∑  ρ is the population correlation coefficient. Mosfte on we compute the sample correlation coefficient because the population variance isn o nftoet known. We rather use the estimated value given asρ ̂ , or simply r . Our interest here is not to show how these coefnfitcsi of correlation are derived, but its application in MS Excel. The syntax for this expsrieosn is given as: =Correl(array 1, array 2) Assume your arrays are in cell B9 to K9, and B1 0K 1to0, then we can obtain the correlation coefficient thus: = CORREL (B9: K9, B10: K10) 162 UNIVERSITY OF IBADAN LIBRARY The result that will be obtained will lie betwee1n a-nd + 1 inclusive. + 1 shows perfect and positive relationship, while -1 shows perfect anedg antive relationship. O shows null relationship. It should be noted here that MS Excel uses the PKeaarlrson’s Products moment correlation coefficient to compute the relationship. You canso a wl ork it out yourself by using the formula. Example 1 Determine the correlation coefficient between tihve ng data X X1 X2 X3 X4 X5 Y Y1 Y2 Y3 Y4 Y5 Solution: i. Enter the variables in an MS Excel spreadsheet D E F G h 1 X Y X 2 Y2 XY 2 X1 Y X 2 2 1 1 Y1 X1Y1 3 X2 Y2 X 2 Y22 2 X2Y2 4 X3 Y3 X 2 3 Y 2 3 X3Y3 163 UNIVERSITY OF IBADAN LIBRARY 5 X4 Y 2 2 4 X4 Y4 X4Y4 6 X5 Y 2 2 5 X5 Y5 X5Y5 7 5 5 5 5 5 ∑ X i ∑Yi ∑ X 2i ∑Y2i ∑ XiYi i =1 i =1 i =1 i =1 i =1 8 X Y ii. Using the defined function, we write =CORREL(D2:D6, E2:E6) However, if you want to use the formula, you can aghoead to obtain the other results in columns F, G and H, as well as the results in oD H7 7t, as well as D8 and E8, so that we could use either of the two formula. ∑ XY = H7, ∑ X = D7, ∑ Y= E7, ∑ X2 = F7, ∑ Y2 = G7 Therefore, the coefficient of correlation is wrnit taes =(H7-(D7* E7))/(((F7-(D7^2))*(G7-(E7^2)))^0.5) This looks a little bit clumsy. You can simplify bify solving the arguments one by one, storing them in different cells, and further ustinhge new results in the new cells. Another way of approaching this problem is to uhse dt efined function for variance and covariance. That is, for the numerator (store ilnl, scaey F9); F9 =COVAR(D2:D6, E2:E6) And for the denominator, F or the X variables (es tionr cell, say G9); G9 =VAR(D2:D6) And for the Y variables (store in cell say, H9); 164 UNIVERSITY OF IBADAN LIBRARY H9 =VAR(E2:E6) Find the product of the resulting variances andre s itno all cell, F10 =G9*H9. And obtain the square not of F10, store in cell ,G s1a0y, G10 =SQR(F10) This finally gives the correlation as =F9/G10 Another easier way, after obtaining the covaria nincset,ead of the variances, we look for the standard deviations of the variables. That is, =GST9DEV(D2:D6) and H9 =STDEV(E2:E6) so that the correlation becomes = F9/(G9*H9). In-Text Question List the different type of coefficient of correloanti. In-Text Answer Spearman's Rank correlation coefficient Karl Pearman's Product Moment Correlation coefnfitc i e The Theory of Linear Regression 165 UNIVERSITY OF IBADAN LIBRARY Let us start this section by mentioning the equna otifo a straight line given b y y = mx+ c, where y = dependent variable m= slope (gradient) of the line x = independent variable c = intercept on they − axis This equation shows the relationship between threia bvlaes x and y . According to the definition, we see that the result oyf depends on whaxt is with the fact that there is a gradient between then, and an interruption. Therere cases where the intercept does not exist. That is, the line of relationship passeso uthgrh the origin. In this casec,= 0 so that y = mx. But in regression theory, the intercept is commoenc abuse it is uncommon to have a relationship that will pass through the origin. T ehqeuation y = mx+ c is called the regression line, though often written in the formy = a+ bx, where a is the interrupt,b the slope, andx and y are as defined. The regression line is the best line that couldd rbaew n to fit into a scatter diagram. This is because not all the points of a scatter diagrama boifv arate data( x, y) very often lie on a straight line. Listed are some of the methods utos efidt a regression line. 1. The free hand method 2. The grand mean method 3. The semi-average method 4. The least squares method 166 UNIVERSITY OF IBADAN LIBRARY The one we shall be interested in for the purpof steh iso course is the least squares method. (You should familiarize yourself with the other mhoedt s.) The regression line obtained here is unique. The name ‘least squares’ is so called because wteer mdiene the slope and intercept by minimizing the error sum of squares. The actuarl ersesgion line isy = a+ bx+ e, where e is the error or variation between the variabyle a nd its estimated valuŷe . That is e= y− ˆy, so that e2 = ( y− ˆ 2y) , and 2 ∑e2 =∑( y− ˆy) The last expression is called the error sum of rseqsu. a On minimizing the variation, we obtain the normqaul aetions ∑ y = b∑ x + na - - - (i) ∑ xy = b∑ x2 + a∑ x - - - (ii) On solving the two simultaneous equation we obtahien estimated values faor and b (properly written asa ̂ and b̂ ) ˆ n∑ xy− (∑ x)(∑ y) cov= = ( x,y)b 2 n∑ x2 − (∑ x) var( x) = 1 â (∑ y − b∑ x) = y− bx n The estimated regression line becomes yˆ = aˆ + bˆ x In example 1, using the formula to finâd and b̂ , we proceed as follows 167 UNIVERSITY OF IBADAN LIBRARY For b̂ =((5*H7)-(D7* E7))/(5*F7)-(D7^2)) However, if the number of observations in varia bilse sstored in a cell, say, C8, we can replace 5 by the cell address thus. =((C8* H7)-(D7*E7))/((C8*F7)-(D7^2)) We can store this result fob̂r in a cell, say D9, so that for â =E8-(D9*D8) Whatever value we obtain will be substituted in ethsetimated regression line. However, in recent MS Excel packages, we can o bat ariengression equation directly from the Tools menu in Data Analysis. Familiarize youlf rwseith this new innovation in MS Excel and observe the results displayed. You can also use defined functions for covarianncde vaariance to computb̂e (just like we did in the case of correlation). Coefficient of Determination (R2 ) This is the amount of variation in the independveanrti able that explains the variation in the dependant variable, and is mathematically given as 2 = explained variation squared R total variation squared ( ŷ − y)2 = ∑ ( − )2 ∑ y y You can go further to obtain this result using MthSe Excel by applying the formula. This result is as well given in the Regression analiyns tish e Data Analysis under the Tools menu. 168 UNIVERSITY OF IBADAN LIBRARY Summary for 13 • Related variables often have effect on each obthuet rn, ormally are associated. But the degree of association is what is to be determined. • Correlation is the degree of association; while ethffe ct one variable has on other(s) is known as regression. • You also learnt that to determine the extent anl aeinxepd variable has on the unexplained, you determine the (the determination coeffici.e nt) Self-Assessment Questions (SAQs) for study sess1io3n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the following quoensst.i Write your answers in your study Diary and discuss them with your Tutor at the nsetuxtd y Support Meeting. You can check your Define School answers with the Notes on the Seslfe-Assment questions at the end of this Module. Post -Test Use MS Excel (stating your procedure) to obtain 1. The Product moment correlation coefficient beetnw e a. Botany ( X ) 3 6 4 6 4 7 5 5 4 7 Zoology (Y) 4 6 5 7 4 7 6 6 5 8 b. Reading( X ) 4 6 5 5 4 6 6 6 Arithmetic (Y) 3 3 3 3 2 3 2 2 c. Agriculture (Y) 4.4 4.3 12.4 13.4 24.6 38 .188 . 5 42.0 20.5 169 UNIVERSITY OF IBADAN LIBRARY Health ( X ) 12.0 14.1 19.9 18.3 29.1 62.4 .5 83 85.0 95.5 2. The spearman’s Rank correlation on coefficief ntht eo data in 1a - 1c. 3. The Regression equation Yof on X , and X on Y if the data in 1a - 1c, and also obtain their coefficients of determinationR, 2 . References Adamu S. O. and Johnson T. L. (198S5t)a: tistics for Beginners, Book. S1econd Edition. Lagos: KOLA Publishers Limited, Nigeria. Cooke D., Craven A. H. and Clarke G. M. OBa: sic Statistical Computin. gSecond Edition. Edward Arnold. A division of Hodder and Stough.t on Omotosho Y. (1990):C ollege and University Text Statist.i cs Ibadan: NPS Educational Publishers Limited, Nigeria. 170 UNIVERSITY OF IBADAN LIBRARY Study Session 14: Elementary Time Series Analysis Introduction A time series could be defined as data obtainedrt iomve, that is, at equal intervals of time. This could be weekly, fortnightly, monthly, quarlyte ror yearly. However, a fortnight interval is not common since some months break fiinvteo weeks as against the traditional four weeks. Examples of time series are annualn fcinial reports, monthly sales of particular good, quarterly products of export/import of protdsu, cor weekly collection of rainfall in a particular month. Learning Outcomes from Study Session 14 At the end of this study session, you should be atob:l 14.1 Components of a Time Series 1. observe the trend of a time series data; and 2. make simple forecast. Pre-Test 1. What do you understand by variation in values? 2. What are the causes of variation in values? 3. How many models has a time series? Name them 4. What do you understand by trend? 5. How do you estimate trend? 14.1 Components of a Time Series A time series data is never smooth. It is subtjoe cvt ariation which could be attributed to climatic, social, economic or accidental factorTsh. e se factors make up features of the four components of time series. 171 UNIVERSITY OF IBADAN LIBRARY They are: i. Trend – This is the path a time series follow oav elro ng period of time. It is best illustrated by the graph of the time series data. ii. Seasonal variation – This is as a result of clicm antid social factors. It is usually (but not perfectly) regular, especially regular with ssoenas and climatic changes. The graph is up and down in movement. iii. Cyclical variation - This is caused by economic tofar.c It is usual to experience periods of booms, recesses and recovery. The gisr aopshcillatory in nature. iv. Irregular variation- This variation is accidentna l niature. This is due to factors that do not occur on a regular basis, or may not be cetexpde to occur, such as war, flood, drought, industrial actions, fire disasters, eolencsti, special occasions, and so on. The graph is erratic in nature. In-Text Question The four component of Time Series are; In-Text Answer • Trend • Seasonal variation • Cyclical variation • Irregular variation 14.1.1 Models of Time Series There are two types of models that are most apipartoep fror associating the components of time series. These are i. Additive model, Yt = Tt + St + Ct + tIt ii. Multiplicative model, Yt = Tt St Ct tI t where in both cases, 172 UNIVERSITY OF IBADAN LIBRARY Yt = Observed data Tt = Trend values St = Seasonal variation Ct = Cyclical variation I t = Irregular variation Estimating Trend The methods that may be applied are: i. The free-hand method ii. The semi-average method iii. The least squares method iv. The moving average method We shall consider the ‘popular’ least squares mde,t haond the moving average method. The least squares method is as described in thveio purse lesson under the linear regression theory. However, instead of thye in the estimated regression line, we may wTrit,e w hich stands for the Trend. That is, in the normal rsesgiroen equation, we write y = a+ bx+ e which can be written as T = a+ bx+ e and the estimated line yˆ = aˆ + bˆ x becomes 173 UNIVERSITY OF IBADAN LIBRARY T = aˆ + bˆ x so that every calculated value ŷof now becomes the trend. It should be noted thailet wt he original y values may not be smoothingly increa,s itnhge trend of necessity is. This is because the trend is a linear combination of xth ev alues, with the slope and intercept. However, the trend of a time series may not bef faic iseunt tool for prediction. A typical time series data look like what we haveelo bw: Year Q X Y 1974 1 1 5.8 2 2 2.1 3 3 6.8 4 4 51.1 1980 1 5 7.5 2 6 1.2 3 7 2.1 4 8 34.8 174 UNIVERSITY OF IBADAN LIBRARY 1981 1 9 14.6 2 10 1.5 3 11 1.0 4 12 40.6 1982 1 13 18.7 2 14 21.5 3 15 11.2 4 16 35.1 The linear regression equation of this time seisri egsiv en as yt = a+ bxt + et and the estimated equation is T = yˆt = aˆ + bˆ xt If you solve this, it results in T = 8.02+ 0.94x Confirm that the trend is as given below: 9.0, 9.9, 10.8, 11.8, 12.7, 13.7, 14.6, 15.5, 1 167.5.4,, 18.4, 19.3, 20.2, 21.2, 22.1, 23.1 Compare these results with thye v alues and comment. The computer procedure is as described for thel es imlinpear regression in the last session 175 UNIVERSITY OF IBADAN LIBRARY The Moving Average Method The procedure for computing the moving average nddesp eon whether the desired moving average is to be odd or even. A good example oof dadn moving average is the 3-point moving average, which is preceded by computingp ao i3n-t moving totals. The 3- point moving average is the desired trensdin. gU the data given earlier, we find out that the trend is given as 4.9, 20.0, 21.8, 19.9, 3.6, 12.7, 17.2, 17.0, 154.7.4, , 20.1, 26.9, 17.7, 22.6 An example of an even moving average is the 4-p moionvting average, which is preceded by the computation of the 4-point moving totals aned 2th-4 point moving totals. The resulting results of the 4- point moving average is knownt haes trend. Confirm the results given below: 16.7, 16.8, 16.1, 13.4, 12.3, 13.2, 13.1, 13.79, ,1 148..0, 21.7, 22.3, Compare these three trends and comment. The procedure for computer analysis is very simapsl eit is all of addition and division. You are expected to carry out the computer analysaisti,n sgt the steps you have taken. Built-In Trend Command There is a built-in trend command as explainede cintu lre five. This is carried out in the Edit menu. Carry out this built-in solution and comp yaoreur results as well. Summary of 14 • Time series data is that collected at equal intl eorfv taime. • It is affected by certain factors which are summzeadr i into four features, viz; trend, seasonal variation, cyclical variation, and irreagr uvlariation. 176 UNIVERSITY OF IBADAN LIBRARY • The model of a time series could be additive or timpluiclative. In estimating a trend, most used methods are the least squares and thineg m aovverage. Self-Assessment Questions (SAQs) for study sess1io4n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the followinge sqtuions. Write your answers in your study Diary and discuss them with your Tutor at ntheext study Support Meeting. You can check your Define School answers with the Notesth oen S elf-Assessment questions at the end of this Module. Post-Test 1. State the essential components of a time serieas adnadt give a mathematical relationship between each data unit and the va rcioumsponents. The data below shows the total domestic production of cements iigne rNia during the period 1974 – 1977. Figures are in thousand metric tons. Usee ctohmputer to perform the computation of the trend. Year Quarter I II III IV 1974 279 308 328 311 1975 471 381 225 311 1976 312 294 339 329 177 UNIVERSITY OF IBADAN LIBRARY 1977 305 309 315 332 Source: National Bureau of Statistics 2. Consider the time series data below Year Output ‘000 Year Output ‘000 1950 30 1963 62 1951 29 1964 69 1952 31 1965 76 1953 32 1966 71 1954 33 1967 74 1955 37 1968 81 1956 41 1969 82 1957 43 1970 85 1958 41 1971 91 1959 49 1972 93 1960 55 1973 96 1961 58 1974 103 1962 60 1975 108 Use the computer to i. Plot the time series data 178 UNIVERSITY OF IBADAN LIBRARY ii. Calculate a five-year moving average iii. Plot the moving average graph on the same grap(ih). as Reference Omotosho Y. (1990):C ollege and University Text Statist.i c sIbadan: NPS Educational Publishers Limited, Nigeria. 179 UNIVERSITY OF IBADAN LIBRARY Study Session 15: Statistical Tests and ConfidencInet ervals Introduction Making inference about a population is what is tomf oust concern to the statistician. If a conclusion cannot be drawn over a given data, nthoe wn ork is done. In making an inference about a population, you need to set up a hypot,h aelswisays known as the null hypothesis, and then you make suitable assumptions about tmhep les adata. Next is to obtain a suitable statistic, sayt − statistic or F − statistic This statistic must justify our assumptions, as l waesl having knowledge of the sampling distribution. You then calculated the value of tshtea tistic of interest from our data, and compare the result with the tabulated value oft ethset statistic. In finding a confidence interval for a populatioanr apmeter, for example the mean, you need an estimate of the parameter, and its samplingr ibduistiton. In most cases the assumption holding for the sampling distribution is the norm oar lt distribution. Learning Outcomes from Study Session 15 At the end of this study session, you should be atob:l 15.1 Point and Interval Estimates 15.2 Explain the Hypothesis Testing. Pre- Test 1. What in a hypothesis? 2. Define a Standard Normal Variate. 180 UNIVERSITY OF IBADAN LIBRARY 3. Define at − test statistic. 15.1 Point and Interval Estimates In study session ten, you were able to obtain eastetism such as the mean. A population parameter estimated as a single number is knowan p aosin t estimate. On the other land, if it lies between two numbers it is known as intervatilm easte. For exampleµ, = 3.5 is a point estimate, and2 .5≤ µ ≤ 4.5 is an interval estimated of the population mean. Confidence Interval and Limits for the Population Mean (µ ) 1. Large sample case- The definition of the confidence interval for thmee an in large samples is approached using the normal distrib.u tWioen know that µ and σ are respectively the population mean and standard tdioenv.ia The critical value is defined as ±Zα , where Z is the standard normal variate. The acceptancieon r efogr µ must 2 be within the bounds of the critical values inscivlue. This is illustrated in the graph below. 181 UNIVERSITY OF IBADAN LIBRARY −Zα 2 Zα 2 The shaded region is the acceptance region. S eutpti nag confidence interval foµr , from the figure above, mathematicallµy must be such that − x − µZα ≤ ≤ Zα 2 σ n 2 which results x − Zα σ n ≤ µ ≤ x+ Zα σ n 2 2 This is the confidence interv al From here we obtain the confidence limits as x ± Zα σ n 2 A test could be 1-tailed or- t2ailed. 182 UNIVERSITY OF IBADAN LIBRARY For 1-tailed, the 95% confidence interval and lsim airte given as µ ≤ x +1.65σ n and x +1.65σ n respectively for upper tails, and µ ≥ x −1.65σ n and x −1.65σ n respectively for lower tail. For 2-tailed, the 95% confidence interval and lsim airte given as: x −1.96σ n ≤ µ ≤ x+ 1.96σ n and x ±1.96σ n respectively. The standard normal variate in defined as x − µ Z = σ n In MS Excel, the syntax foZr is given as =STANDARDIZE (X, mean, std_dev) The form of Z used here is x − µ Z = σ (Note, however , thaσt n is the standard error) You could obtain the saemseu ltr in both cases using a stepwise format. That is, for Z = x − µ , having given the population and sample mean, ealsl aws the standard σ n deviation and total sample number, we could wr igte naeral syntax as Z =(X-µ)/(σ /SQR(n)) 183 UNIVERSITY OF IBADAN LIBRARY Note that SQR(n) must have been solved earlier. OR Z =(X-µ)/(σ /(n^0.5)) Since this is a syntax, it would be best to repnrte tshee parameters and statistics above by real letters. That is, let X = X, U = µ and R= σ , so that Z=(X-U)/(R/(N^0.5)) x − µ The second form oZf , that is Z = , could also be written as σ Z =(X-U)/R x − µ Here is a BASIC program foZr = σ n 10 REM Standard Normal Variate 20 INPUT N, X, U, R 30 Z = (X - U) / (R / SQR(N)) 35 PRINT “Standard normal Test” 40 PRINT “Mean of null hypothesis=”; U 50 PRINT “sample mean=”; X 60 PRINT “Standard error of the mean=”; R/SQR(N) 70 Print “Z- statistic =”; Z The standard error given as R/SQR(N), where R tdeesn tohe standard deviation could also be written as SQR(V/N), where V denotes the vacreia. n 184 UNIVERSITY OF IBADAN LIBRARY The confidence interval is not strenuous as weFllo. r 2-tailed, the simple program is as follows; 10 REM Confidence interval for 2-tailed 20 INPUT N, X, R 30 READ Z 40 N1 = SQR(N) 50 I1 = X - (Z * (R / N1)) 60 I2 = X + (Z * (R / N1)) 70 PRINT “Upper Confidence Limit=”; I2 80 PRINT “Lower Confidence Limit=”; I1 90 PRINT “95% Confidence Interval for mean is”; “I<1= U <=”; I2 100 END 110 DATA 1.96 And for a 1-tailed tests, 10 REM Confidence Interval for 1-tailed 20 INPUT N, X, V 30 READ Z 40 S = SQR (V / N) 50 I1 = X + (Z * S) 60 I2 = X – Z * S 70 PRINT “Upper Tail Confidence Limit =”; I1 185 UNIVERSITY OF IBADAN LIBRARY 80 PRINT “Lower Tail Confidence Limit=”; I2 90 PRINT “95% Upper Tail Confidence Interval is < U=”; I1 100 PRINT “95% Lower Tail Confidence Interval is > U=”; I2 110 END 120 DATA 1.65 While it is assured that there is not much diffecree nbetween the two programs, the only difference that may be noticed is that ‘R’ is exncghead for ‘V’ in the second program. R stands for the ‘standard deviation’, and V stanodr s‘v fariance’. 2. Small Sample Case- The appropriate distribution used here is tshteu dent− t distribution, which become an alternative for th.e Z is useful only when the population standard deviation is known. The paraemr, emt entioned is largely unknown. We rather make do with the estimate. Trehsisu lts in the new variat(et ) , given below as : = x − µ t s n−1 Where s is the sample standard deviation, andn − 1 is the degree of freedomn, being the sample size. The MS Excel format for thet − test is = TTEST(arrary1, array 2, tails, type) The t- distribution has the syntax =TDIST(x, deg_freedom, tails) For the population meaµn , to all fall within the acceptance region, it mt bues such that x − µ tα ≤ 2 s n−1 186 UNIVERSITY OF IBADAN LIBRARY This means that x − µ −tα ≤ ≤ tα 2 s n−1 2 Therefore, for a 2-tailed, the confidence interivsa l x − tα s n−1 ≤ µ ≤ x+ αt s n−1 2 2 having confidence limitsx ± tα s n−1 2 And for a 1-tailed, the upper and lower confide innctervals are µ ≤ x + tα s n−1 and µ ≥ x − tα s n−1 2 2 respectively, having confidence limits x + tα s n−1 for the upper tail, andx − tα s n−1 for the lower tail. 2 2 The value tα and −tα could be read form the table to-fd istribution undern −1 degrees of 2 2 freedom. In MS Excel, confidence interval could be calcudla dteirectly using the syntax. =CONFIDENCE(alpha, standard_dev, size). However, you are encouraged to apply a do-it-yolfu rmsethod as well so that you could compare results with the built- in facilities ofe t hMS Excel. Unpaired Sample- Difference of means Assume we have two samplxe and y with sizes n1 and n2 respectively. Thet − test for these two samples from normal population is givse n a 187 UNIVERSITY OF IBADAN LIBRARY t = x − y 2  1 1 S  +   n1 n2  where x and y are means of each sample, with pooled variaSn2c e (n −1) s22 1 1 + ( n2 −1) s2S = 2 n1 + n2 − 2 The degrees of freedom of tht ed istribution defined above ins1 + n2 − 2 . The BASIC program for thist - statistic involving two sample is 10 REM t- statistic for unpaired samples 20 INPUT X, Y, N1 N2, V1, V2 30 K = N1 + N2 - 2 40 S1= (N1 - 1) * V1 + (N2 - 1) * V2 50 S2 = S1 / K 60 S1 = SQR (S2 / N1 + S2 / N2) 70 T = (X - Y) / S1 80 PRINT “ t-statistic for unpaired samples =”; T 90 END Example The mean weight and standard deviation of 150o tfi nvse getable cooking oil produced by an oil producing factory showed 3.8 litres and 0.2tr5e sli respectively. Write a BASIC program 188 UNIVERSITY OF IBADAN LIBRARY to compute (a) 2- tailed (b) 1- tailed (upper) 1(c- )t ailed (lower) 95% confidence limits and intervals for the mean. Solution: Since n is large (i.e. 150), normal distribution is appalbicle. a. 10 REM Confidence limits and Intervals for 2- etadi l 20 READ A, B, C, D 30 N1 = SQR ( A) 40 I1 = B - (D * (C / N1)) 50 I2 = B + (D * (C / N1)) 60 PRINT “Upper Confidence Limit =”; I2 70 PRINT “Lower Confidence Limit =”; I1 80 PRINT “95% Confidence Interval for Mean =”; I“1<;= U <=”; I2 90 END 100 DATA 150, 3.8, 0.25, 1.96 b. 10 REM Confidence Limit and Interval for 1- tai le(udpper) 20 READ A, B, C, D 30 N = SQR (A) 40 I = B + (D * (C / N) 50 PRINT “Upper tail confidence limit =”; I 60 PRINT “95% upper tail confidence IntervaUl i s< =”; I 189 UNIVERSITY OF IBADAN LIBRARY 70 END 80 DATA 150, 3.8, 0.25, 1.65 10 REM Confidence Limit and Interval for 1- tai le(ldower) 20 READ A, B,C, D 30 N = SQR (A) 40 I = B - (D * (C / N)) 50 PRINT “Lower tail Confidence Limit =”; I 60 PRINT “95% Lower tail Confidence Interval is> U= ”; I 70 END 80 DATA 150, 3.8, 0.25, 1.65 You are encouraged to write a single program fo),r ((ba) and (c), as well as use the MS Excel to compute these results. 15.2 Hypothesis Testing A statistical hypothesis is an assumption made opno paulation in the process of taking certain decision(s) on the population. The assuomnp mti ade with the aim of rendering a statistical hypothesis insignificant is called ‘ln huylpothesis’. It assumes no difference in certain condition (oar apmeter of interest). Symbolically, it denoted by H0 . Of necessity, another assumption is made to tceor uthne null hypothesis, which is called the alternative hypothesis, den obtye dH1 or HA . For example H0 : the 2 students are equally brilliant H1: the 2 students are not equally brilliant. 190 UNIVERSITY OF IBADAN LIBRARY In statistics, hypotheses are better stated useianlg v ralues. For example: H0 : µ = 2 H1: µ > 2 In stating a hypothesis, errors might be commit(tdeude to our ‘imperfections’). There are two types of error. These are 1. Type 1 error, which is committed if a hypothetical value is etank to fall within the rejection region when it is supposed to fall wit hthine acceptance region. 2. Type 2 error, which is committed if a hypothetical value is etank to fall within the acceptance region when it ought to talk within rtehje ction region. In general, the following is the procedure for cyainrgr out a hypothesis testing (our test- statistic of interest are the normal and t); 1. Make statement about your hypothesHe0s ,a nd H1. 2. Obtain the value for mean and standard devni aotifo the given population, or their estimates. 3. Compute the test statistiZc, or t , of the hypothetical value. 4. Determine the critical value(s) that correspso ntod the given significance level and hence the acceptance region. 5. Compare your results in (3) and (4) and eitahcecre pt or reject yourH 0 as the case may be. If the computed test statistic is lessn tthae critical value (i.e. table value), accept H0 . Otherwise reject it. 191 UNIVERSITY OF IBADAN LIBRARY Summary of 15 • No statistical analysis is computed if an infere ncacennot be drawn on the entire population given the sample data. • In any sample or population, it is always impor taton tknow the mean and the standard deviation, in order to be able to carry out te sts. • However, the population standard deviation is llayr guenknown, so we limit ourselves to the estimate, that is, sample standard deviation. • To carry out a test, a hypothesis, known as thel hnyuplothesis must first be conjectured, which serves as springboard for the test. Acce pret joerct if your computed test- statistic falls within or outside the region of acceptance, resivpelcyt Self-Assessment Questions (SAQs) for study sess1io5n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the following quoensst.i Write your answers in your study Diary and discuss them with your Tutor at the nsetuxtd y Support Meeting. You can check your Define School answers with the Notes on the Seslfe-Assment questions at the end of this Module. 192 UNIVERSITY OF IBADAN LIBRARY Post -Test 1. Write a BASIC program to compute the 95% connfcidee limits and confidence intervals of both 2-tailed and 1- tailed (upper and lower). 2 a. Write stepwise the MS Excel format. b. wrait eB ASIC program to carry out a test at 5% level to find whether or not a claim of an avger areturn of N5550 by a sales representative could be regarded as the mean lf omr oanlths if in 5 years he made an average monthly returns of5 N550 with standard deviation of2 N50. 3. Rewrite all the BASIC programs in this lectunred aassess yourself. References Adamu S. O. and Johnson T. L. (19):8 S5tatistics for Beginners, Book S1e. cond Edition. Lagos: KOLA Publishers Limited, Nigeria. Cooke D., Craven A. H. and Clarke G. MB.a: sic Statistical Computin. gSecond Edition. Edward Arnold. A division of Hodder and Stoughton. Omotosho Y. (1990):C ollege and University Text Statist.i cs Ibadan: NPS Educational Publishers Limited, Nigeria. 193 UNIVERSITY OF IBADAN LIBRARY Study Session 16: Introduction to MATLAB Introduction The name MATLAB stands for MATrix Laboratory. Ist ai high-performance language for technical computing. It integratesc omputation, visualization, and programming environment. It is a modern programming language environmenht wsoitphisticated ata structure,s built- in editing andd ebugging tool,s and supportso bject-oriented programmin. gThese factors make MATLAB an excellent tool for teaching and raersceh. MATLAB has many advantages compared to convent iocnoaml puter languages (e.g., C, FORTRAN) for solving technical problems. It is ainnte ractive system whose basic data element is ana rray that does not require dimensioning. Specific acpaptiloi ns are collected in packages referred to taoso lbox. There are toolboxes for signal processing, sym bcoolimcputation, control theory, simulation, optimization, and several other fields of appliecide nsce and engineering. Learning Outcomes from Study Session 16 At the end of this study session, you should be atob:l 1. Recognize MATLAB as scientific analytic softwea. r 2. Understand the basic features of MATLAB. 3. Use MATLAB as a calculator. Pre-Test 1. What is the meaning of MATLAB? 2. Differentiate betweewn ho andw hos commands. 3. What are the hierarchies of arithmetic openrast iaos used in MATLAB? 194 UNIVERSITY OF IBADAN LIBRARY 16.1 MATLAB After logging into your account, you can enter MAATBL by double-clicking on the MATLAB shortcut icon on your Windows desktop. When you start MATLAB, pae csial window called the MATLAB desktop appears. The doeps kits a window that containost her windows. The major tools within or accessible frtohme desktop are: The Command Window The Command History The Workspace The Current Directory The Help Browser You can customize the arrangement of tools and mdoecnuts to suit your needs. Excellent graphics facilities are available, and the pict ucreasn be inserted into LATEX and Word documents. MATLAB can be used in a number of different ways m oordes; as an advanced calculator in the calculator mode, in a high level programminngg luaage mode and as a subroutine called from a C-program. To execute a command in MATLAB, type on the comm apnrodmpt sign, >“ >” when it is activated in the command window. Command Line Help Help is available from the command line prompt. >> help command To obtain help one lementary math functio,n fsor instance, type 195 UNIVERSITY OF IBADAN LIBRARY >> help elfun This gives rather a lot of information so, in or dtoe rsee the information one screenful at a time, first issue the commanmdo re on, i.e., >> more on >> help elfun Hit any key to progress to the next page of infotriomna. Another way to get help is to use tlhoeo kfor command. Thelo okfor command differs from the help command. The help command searches feoxr aacnt function name match, while the lookfor command searches the quick summary informatioena cinh function for a match. For example >> lookfor inverse >> help sqrt The doc function opens the on-line version of tehlep hmanual. This is very helpful for more complex commands. >> doc plot Use lookfor to find functions by keywords. The general form is >> lookfor functionnam e Managing the Workspace 196 UNIVERSITY OF IBADAN LIBRARY The contents of the workspace persist between xtheec uetions of separate commands. Therefore, it is possible for the results of onoeb plerm to have an effect on the next one. To avoid this possibility, it is a good idea to issau ec lear command at the start of each new independent calculation. >> clear The commandc lear or clear all removes all variables from the workspace. Thise sf reup system memory. In order to display a list of theri avbales currently in the memory, type >> who while whos will give more details which include size, spaclleo caation, and class of the variables. To keep a record, issuing the command >> diary mysession This will cause all subsequent text that appear st hoen screen to be saved to the file mysessio nlocated in the directory in which MATLAB was inveodk. You may use any legal filename except the names on and off. The recoryd bmea terminated by >> diary off The file mysessio nmay be edited with your favourite editor (the MAATBL editor, emacs, or even MS Word) to remove any mistakes. If you wish to quit MATLAB midway through a calcutiloan so as to continue at a later stage type >> save thissession 197 UNIVERSITY OF IBADAN LIBRARY This will save the current values of all variabtleos a file calledt hissession.ma. tThis file cannot be edited. When you next startup MATLAB,e t yp >> load thissession and the computation can be resumed where you flfe. ft Ao list of variables used in the current session may be seen with wthheo s command, for example >> whos Output: Name Size Elements Bytes Density Complex ans 1 by 1 1 8 Full No v 1 by 3 3 24 Full No v1 1 by 2 2 16 Full No v2 1 by 2 2 16 Full No v3 1 by 3 3 24 Full No v4 1 by 3 3 24 Full No x 1 by 1 1 8 Full No y 1 by 1 1 8 Full No Grand total is 16 elements using 128 bytes Copying to and from Word and other applications There are many situations where one wants to choep yo ut tput resulting from a MATLAB command (or commands) into a Windows applicatiocnh sausM S Word or into a Unix file editor such asE macs. Copying material is made possible on the Windowesr aotping system by using the Windows clipboard. Also, pictures can be exported to filne sa number of alternative formats such as 198 UNIVERSITY OF IBADAN LIBRARY encapsulated postscript format or in jpeg formaAt. TMLAB is so frequently used as an analysis tool that many manufacturers of measuret mseynstems and software find it convenient to provide interfaces to MATLAB which kmea it possible, for instance, to import measured data directly into a *.mat MATLAB file. Quitting MATLAB To end your MATLAB session, typqeu it in the command window, or seleFctil e - Exit MATLAB in the desktop main menu. Creating MATLAB Variables MATLAB variables are created with an assignmentt esmtaent. The syntax of variable assignment is variable name = a value (or an expression) For example, >> x = expression where expressio nis a combination of numerical values, mathema toicpaelrators, variables, and function calls. On other wordesx,p ressio ncan involve: manual entry built-in functions user-defined functions Variables >> 3-2^4 ans = -13 199 UNIVERSITY OF IBADAN LIBRARY >> ans*5 ans = -65 The result of the first calculation is labeleadn s by MATLAB and is used in the second calculation where its value is changed. We can use our own names to store numbers: >> x = 3-2^4 x = -13 >> y = x*5 y = -65 so that x has the value 13 and y = 65. These can be used in subsequent calculations. These are examples of assignment statements: vaarleu easssigned to variables. Each variable must be assigned a value before it may be usehde o rnig tht of an assignment statement. Variable Names Legal names consist of any combination of lettenrds daigits, starting with a letter. These are allowable: X, Y, NetCost, Left2Pay, x3, Xz25, c5, and so on These are not allowable: Net-Cost, 2pay, %x, @sign It is important to use names that reflect the vsa ltuhey represent. Special names: you should avoid using eps = 2.2204e-16 =2  (The largest number such that 1 + eps is indiustisinhgable from 1), and pi = 3.14159... = 200 UNIVERSITY OF IBADAN LIBRARY If you wish to do arithmetic with complex numbebrso,t h i and j have the valu√e 1 unless you change them >> i,j, i=3 ans = 0 + 1.0000i ans = 0 + 1.0000i i = 3 Overwriting Variable Once a variable has been created, it can be renaesds. igIn addition, if you do not wish to see the intermediate results, you can suppress the rnicuaml eoutput by putting a semicolon (;) at the end of the line. Then the sequence of commlaonodks like this: >> t = 5; >> t = t+1 t = 6 Entering Multiple Variables per Line It is possible to enter multiple variables per .l inUese commas (,) or semicolons (;) to enter more than one statement at once. Commas (,) allouwlti pmle statements per line without suppressing output. >> a=7; b=cos(a), c=cosh(a) b = 0.6570 c = 548.3170 Error Messages If we enter an expression incorrectly, MATLAB wrilel turn an error message. For example, in the following, we left out the multiplicationg sni, *, in the following expression >> x = 10; 201 UNIVERSITY OF IBADAN LIBRARY >> 5x ??? 5x || Error: Unexpected MATLAB expression. Basic Arithmetic Operators Symbol Operation Example + Addition 2 + 3 - Subtraction 2- 3 * Multiplication 2 * 3 / Division 2/3 MATLAB as a Calculator The basic arithmetic operators are + - * / ^ aneds eth are used in conjunction with brackets: ( ). The symbol ^ is used to get exponents (powe2r^s4):= 16. You should type in commands shown following the prompt: >>. >> 2 + 3/4*5 ans = 5.7500 >> Is this calculation 2 + 3/(4*5) or 2 + (3/4)*5? MATLAB works according to the priorities: 1. quantities in brackets, 2. powers 2 + 3^2 )2 + 9 = 11, 3. * /, working left to right (3*4/5=12/5), 4. + -, working left to right (3+4-5=7-5), Thus, the earlier calculation was for 2 + (3/4)*y5 p briority 3. 202 UNIVERSITY OF IBADAN LIBRARY Hierarchy of arithmetic operations Precedence Mathematical operations First The contents of all parentheses are evaluatsetd, sfitrarting from the innermost parentheses and working outward. Second All exponentials are evaluated, working from lteof tright Third All multiplications and divisions are evaluatewdo, rking from left to right Fourth All additions and subtractions are evaluatedr,t isntga from left to right Now, consider another example: 1 4 6 × 2 3 5 7 In MATLAB, it becomes >> 1/(2+3^2)+4/5*6/7 ans = 0.7766 or, if parentheses are missing, >> 1/2+3^2+4/5*6/7 ans = 10.1857 So here what we get: two different results. Therree, fowe want to emphasize the importance of precedence rule in order to avoid ambiguity. Controlling the Hierarchy of Operations or Precedence Let's consider the previous arithmetic operatiount, nbow we will includep arenthese.s For example, 1 + 2 x3 will become (1 + 2) x3 203 UNIVERSITY OF IBADAN LIBRARY >> (1+2)*3 ans = 9 and, from previous example >> 1+2*3 ans = 7 MATLAB arithmetic operators obey the sampree cedenc erules as those in most computer programs. For operators eoqf ual precedence, evaluation is frolemft to right. Mathematical Functions Some commonly used functions, where variables x ya ncdan be numbers, vectors, or matrices. Elementary Functions cos(x) Cosine abs(x) Absolute value sin(x) Sine sign(x) Signum function tan(x) Tangent max(x) Maximum value acos(x) Arc cosine min(x) Minimum value asin(x) Arc sine ceil(x) Round toward∞s + atan(x) Arc tangent floor(x) Round toward∞s - exp(x) Exponential round(x) Round to nearest integer sqrt(x) Square root rem(x) Remainder after division log(x) Natural logarithm angle(x) Phase angle 204 UNIVERSITY OF IBADAN LIBRARY log10(x) Common logarithm conj(x) Complex conajuteg Predefined constant values pi The  number, = 3:14159 … i,j The imaginary unit , √1 Inf The infinity, ∞ NaN Not a number Examples We illustrate here some typical examples which teredl ato the elementary functions previously defined. As a first example, the value of the expression  sin 10, for   5,   2, and   8 is computed by >> a = 5; x = 2; y = 8; >> y = exp(-a)*sin(x)+10*sqrt(y) y = 28.2904 The subsequent examples are >> log(142) ans = 4.9558 >> log10(142) ans = 2.1523 205 UNIVERSITY OF IBADAN LIBRARY Note the difference between the natural logaritohgm( xl) and the decimal logarithm (base10) log10(x). To calculates in ⁄4 ande 10, we enter the following commands in MATLAB, >> sin(pi/4) ans = 0.7071 >> exp(10) ans = 2.2026e+004 Suppressing Output One often does not want to see the result of inetedriamte calculations. We can terminate the assignment statement or expression with semi-colon >> x=-13; y = 5*x, z = x^2+y y = -65 z = 104 >> the value of x is hidden. Note also we can placve rsael statements on one line, separated by commas or semi-colons. Arrays 206 UNIVERSITY OF IBADAN LIBRARY An array is an ordered collection of numbers. Enaucmh ber can be accessed directly using an index (much like the subscript notation used inh meamtatics to refer to an element of a vector or a matrix). An array is entered into MATLAB asli sat of numbers e.g. x = [1,3,5,7,9] or instead of using commas we can use blanks x = [1 3 5 7 9] This creates a variable called x which has elem xe(n1t)s, x(2), ..., X(5). Each such element is a real number and can be used directly. To acches tsh itrd element we type: z = x(3) This code would result in the variable z assumi nvga laue of 5. Arrays become useful when one wants to performs tahme e action on every element of an array. For example, supposed we have an array ibdiensgc rthe flows of a set of four components in a mixture. We can find out the toflotawl by simply adding up the individual flows. One way would obviously be to write a staetenmt of the form Totalflow = x(1) + x(2) + x(3) + x(4); Though, luckily, MATLAB has a built in functions um which can be used in this instance. We write Totalflow = sum(x); 207 UNIVERSITY OF IBADAN LIBRARY Dot Division of Arrays (./) There is no mathematical definition for the divnis iof one vector by another. However, in MATLAB, the operator ./ is defined to give elemebnyt element division – it is therefore only defined for vectors of the same size and type. >> a = 1:5, b = 6:10, a./b a = 1 2 3 4 5 b = 6 7 8 9 10 ans = 0.1667 0.2857 0.3750 0.4444 0.5000 >> a./a ans = 1 1 1 1 1 >> c = -2:2, a./c c = -2 -1 0 1 2 Warning: Divide by zero ans = -0.5000 -2.0000 Inf 4.0000 2.5000 The previous calculation required division by 0 o-t icne the Inf, denoting infinity, in the answer. >> a.*b -24, ans./c ans = -18 -10 0 12 26 Warning: Divide by zero 208 UNIVERSITY OF IBADAN LIBRARY ans = 9 10 NaN 12 13 Here we are warned about 0/0 - giving a NaN (NNotu am ber). Example Estimate the limit '(  lim %&∞  )*+ ,% The idea is to observe the behaviour of the ratio for a sequence of values of x that % approach zero. Suppose that we choose the seqduefninced by the column vector >> x = [0.1; 0.01; 0.001; 0.0001] then >> sin(pi*x)./x ans = 3.0902 3.1411 3.1416 3.1416 which suggests that the values approa.c hT o get a better impression, we subtract the v alue of  from each entry in the output and, to display m doerceimal places, we change the format >> format long >> ans -pi ans = -0.05142270984032 -0.00051674577696 209 UNIVERSITY OF IBADAN LIBRARY -0.00000516771023 -0.00000005167713 Can you explain the pattern revealed in these nursm?b e We also need to use ./ to compute a scalar di vbidye ad vector: >> 1/x ??? Error using ==> / Matrix dimensions must agree. >> 1./x ans = 10 100 1000 10000 so 1./x works, but 1/x does not. Dot Power of Arrays (.^) To square each of the elements of a vector we ,c ofourl dexample, do u.*u. However, a neater way is to use the .^ operator: >> u.^2 ans = 100 121 144 >> u.*u ans = 100 121 144 >> u.^4 210 UNIVERSITY OF IBADAN LIBRARY ans = 10000 14641 20736 >> v.^2 ans = 400 441 484 >> u.*w.^(-2) ans = 2.5000 -11.0000 1.3333 Recall that powers (.^ in this case) are done, fbiresftore any other arithmetic operation. Other Array arithmetic Addition and subtraction Matrix addition and subtraction are in fact car roieudt on an element-by-element basis and so there is no need for separate array addition abntdr ascution operations. Multiplication How can we square all the elements in matrix Aw? eIf write a program that calculates A^2 (this is equivalent to carrying out the matrix oapteiorn A*A) then we might write: A = [1 2; 3 4]; B = A^2; % or B = A * A however, this gives the output 211 UNIVERSITY OF IBADAN LIBRARY B = 7 10 15 22 So we need to specify that we want to carry ou t otpheration on an element-by-element basis, hence: A = [1 2; 3 4]; C = A.^2; % or C = A .* A C = 1 4 9 16 In general, the operatio-n , ..0 1, . will result in a matrix with elements* 23*2. Division In a similar way to multiplication if we want to rcrya out division on an element-by-element basis then we used the array operations. Thuso, ptehrea tion- , ../1, . will result in a matrix with elements *2/3*2. Hence, A = [1 2; 3 4]; C = [1 4; 9 16]; D = C./A D = 1 2 3 4 Note: For both array multiplication and division, unsle esither variable is a scalar, then both matrices being operated on must be the same sf iyzoeu. Idon't understand the rules of matrix 212 UNIVERSITY OF IBADAN LIBRARY and array arithmetic then it may help to refer otou ry maths notes or a standard maths text book. Matrices Thus far we have only introduced one dimensionraal yasr, however many engineering and mathematical calculations require the use of meastr, icwhich are two dimensional arrays. These are entered in a similar way >> A = [1 2;3 4] A = 1 2 3 4 We can determine the size of any matrix, usings tizhef unction >> size(A) where the output is ans = 2 2 This tells us thaAt is a matrix with two rows and two columns. Actua, lalyll variable types in MATLAB (scalar variables, and row and column arr)a aysre in fact matrices of different sizes. So if we determine the size of our row vre, cxt o >> size(x) we are told ans = 1 5 213 UNIVERSITY OF IBADAN LIBRARY So x is a matrix 1 row by 5 columns. Matrix arithmetic operations in MATLAB When earlier we considered the multiplication andd itaion of simple (scalar) variables, we used normal mathematical operators. However, flocru claations with matrix variables we will see that there are two types of arithmetic openras:t io Matrix arithmetic: which is based on the rules of standard linear baralg.e The standard operators are used (+ , -, *, /,\, ^). Array arithmetic: which is carried out element-by-element. To disutiinsgh from matrix arithmetic the standard operators are preceded fbuyll -satop (.* ,./ ,.^). The results of the operations are generally quite different, and tfhoerere, it is essential to determine which option you require before writing your program. Addition and subtraction Under the standard rules of matrix arithmetic, naolr amddition and subtraction are carried out on an element-by-element basis. For example >> A = [1 2;3 4] ; >> B = [2 4;3 5]; >> C = A + B >> D = A - B gives the output C = 3 6 6 9 D = -1 -4 214 UNIVERSITY OF IBADAN LIBRARY 0 -1 and of course the operation C - A gives B. Multiplication Matrix multiplication is illustrated by the followngi program: A = [1 2; 3 4]; B = A * A C = B *2 The output of the program is B = 7 10 15 22 C = 14 20 30 44 Notice that the multiplications are carried out eurn dthe standard rules of matrix arithmetic and thus the result of the first operation is nimotp sly the square of all the elements but for this example is calculated as below: B = (1*1+2*3) (1*2+2*4) (1*3+4*3) (3*2+4*4) Division In MATLAB both left and right division are possib. leSo if we wish to divide two scalars then two results are possible: >>2\3 ans = 215 UNIVERSITY OF IBADAN LIBRARY 1.5 >>2/3 ans = 0.6667 Of course we can also use matrix division to reev ear smultiplication e.g. A = [1 2; 3 4]; C = [14 20; 30 40]; D = C/2 E = D/A this program gives D = 7 10 15 22 E = 1 2 3 4 In general if A is a square matrix then we can tshayt A\B = inv(A)*B A/B = B*inv(A) Solving linear equations Perhaps the most useful application of matrix dioivni sis in the solution of linear equations. In fact in MATLAB a system of n linear equations n in unknowns can be solved easily using left division (\). (The maths for over or under csipfied systems of equations is much more complicated and thus we won't consider it.). 216 UNIVERSITY OF IBADAN LIBRARY Consider a system of two simultaneous linear eoqnusa tci ontaining two unknowns: 25 5  3 45 5  11 This can be written in matrix form A*X =B where A = 2 5 X = 5 B = 3 4 5  11 Summary of 16 • MATLAB is an acronym for MATrix LABoratory. • It is a high-performance language for technical pcuotming as well as a modern programming language environment with sophisticatdeadt a structure,s built-in editing andd ebugging tools, and supportos bject-oriented programmin. g • These factors make it an excellent tool for teagc haind research. It has many advantages compared to conventional computer languages (Ce.,g .F, ORTRAN) for solving technical problems. The basic features are interactive. • It can also be used as a calculator. It has foruenledv ant usage in all areas of science and technology. Self-Assessment Questions (SAQs) for study sess1io6n Now that you have completed this study session ,c yaonu assess how well you have achieved its Learning outcomes by answering the following quoensst.i Write your answers in your study 217 UNIVERSITY OF IBADAN LIBRARY Diary and discuss them with your Tutor at the nsetuxtd y Support Meeting. You can check your Define School answers with the Notes on the Seslfe-Assment questions at the end of this Module. Post – Test 1. In each case find the value of the expression AinT MLAB and explain precisely the order in which the calculation was performed. i) -2^3+9 ii) 2/3*3 iii) 3*2/3 iv) 3*4-5^2*2-3 v) (2/3^2*5)*(3-4^3)^2 vi) 3*(3*4-2*5^2-3) 2. Solve for 5 and  in the following simultaneous equation: 25 5  3 45 5  11 References 1. Griffiths D. F. (2005): An Introduction to MATALB. A Publication of the Department of Mathematics, University of Dundee, Scotlanrdd ,E 3d. 2. Houcque D. (2005): Introduction to MATLAB forn Egineering StudentsA. Publication of Northwestern University, Illinois, US. A 218 UNIVERSITY OF IBADAN LIBRARY