Assignment 6
Be sure to use version control ("git"), as you develop your code. Do "git add ...., git commit
" repeatedly as you add and edit your code. You will hand in the output of "git log
" for your assignment repository as part of the
assignment.
1) Create a Python file named myFuncs.py
, which contains definitions for the following three functions.
1a) Write a Python function, called geomPosMean
, which takes an array of numeric values, calculates and returns the "geometric mean" of positive values in the array.
The geometric mean determines the typical value of a set of numbers by using the product of their values (as opposed to the arithmetic mean which uses their sum). It is defined as the nth root of the product of n
numbers numbers, i.e., for a set of numbers x1, x2, ..., xn
, the geometric mean is defined as
$$\left(\prod^n_{i=1} x_i\right)^\frac1n = \sqrt[n]{x_1 x_2 \ldots x_n}$$
Your function geomPosMean
should only work on positive numbers (not including zero). Any non-positive value in the input vector should to be ignored.
You can use the numpy
function prod()
to calculate a product. If you want to know how many elements are in an numpy
array vec
, use vec.size
. You could also use the double asterisk **
symbol to raise something to a power: 2**3 = 8
.
1b) Write a Python function, called negMean
, which calculates and returns the usual arithmetic mean of an array of numeric values, but only considers negative values (not including 0), and ignores positive values.
$$\frac1n \sum_{i=1}^n x_i = (x_1 + x_2 + \ldots + x_n)/n$$
Find ways to use the slicing techniques which will make your code cleaner, more robust and efficient. No loops should be used for either of these two functions.
1c) Write a function, called harmonicMean
, which takes an array of numeric values and calculates and returns the "harmonic mean".
The harmonic mean determines the typical value of a set of numbers by calculating the reciprocal of the average reciprocal value of the vector of values. Thus for n
numbers, i.e. for a set of numbers x1, x2, ..., xn
, the harmonic mean is defined as
$$\left(\frac1n \sum_{i=1}^n \frac1{x_i}\right)^{-1} = \frac1{\frac1n\left(\frac1{x_1} + \frac1{x_2} + \ldots + \frac1{x_n}\right)}$$
As you might expect, if any of the values in the array are zero, the harmonic mean is undefined. Therefore, your function, harmonicMean
, should handle existence of zeros in the input array. If any zero values are encountered, those values should be ignored and the harmonic mean calculated using the remaining values.
Use the techniques, such as 'slicing', which will make your code cleaner, more robust and efficient. You should not need to use loops in your function. You can use the numpy
function sum()
to calculate a sum.
2) Create a Python script, named selectStat.py
, which:
- Imports the file
myFuncs.py
; - Creates an array
mydata
containing the values-10.0, -3.0, 2.0, 0.0, 4.0, 5.0, 0.3, -0.1, 2.1, 3.2, 0.0, -12.1, -1.2
; -
Reads one argument from the Linux command line:
-
if the argument is
0
the script callsgeomPosMean(mydata)
and prints the output of the function thus: "The geometric mean of positive values in the data is ..."; -
if the argument is
1
the script callsnegMean(mydata)
and prints the output of the function thus: "The mean of negative values in the data is ..."; -
if the argument is
2
the script callsharmonicMean(mydata)
and prints the output of the function thus: "The harmonic mean of non-zero values in the data is ..."; - if neither of these arguments is given, the script should output: "Error, incorrect command line argument."
-
if the argument is
3) Write a shell (bash) script (not an Python script), called evalStats.sh
, which calls selectStat.py
three times, once with an argument of 0
, once with the argument 1
and once with the argument 2
.
Submit your evalStats.sh
, selectStat.py
, myFuncs.py
and the output of git log
from your assignment repository.
To capture the output of 'git log
' use redirection, git log > git.log
, and hand in the git.log
file.
Assignments will be graded on a 10 point basis.
Due date is March 12th (midnight), with 0.5 penalty point per day off for late submission until the cut-off date of March 19th, at 10:00am.