Working with matrices is both convenient and fast. Many functions that work on a single value can also be applied to an entire matrix. In addition, functions such as V Max, V Mean, and V Quantile can be applied to a function of the matrix (columns, in these situations). This month’s challenge aims to take this to the next level. We’ll be looking to extend this functionality to functions that aren’t available in JSL, and to do it for both matrices and lists.
- Write a function named mad that takes a vector as an input and returns the median absolute deviation (the median of the absolute differences from median of the data). For example, mad([1,2,3,1000]) returns 1. If there are an even number of observations, take the average value in the middle to calculate the median.
- Write a function trimmedMean(vecIn,pctLo,pctHi) that takes a vector (vecIn) and value between 0 and 0.5 corresponding to the percent of observations below (pctLo) and above (pctHi) the mean that are removed before the mean is calculated. The percent of trimmed values should be the largest number that does not exceed the input percent.
- Write a function listContains(listIn,valueString) that takes a list of strings (listIn) and a string (valueString) and returns a list of matrix values where each element of the matrix corresponds to a starting position of valueString in the jth item of listIn (i.e., an item of listIn may contain valueString more than once). Return the missing value if the item does not contain the string.
- Write a function applyFunction(listIn,fn,argList) that takes an input list (listIn) and applies the function fn with arguments argList to it. Assume the list will only contain numbers or strings. The function should work for both built-in and user-supplied functions. It could return a single value, matrix, or list of value depending on the input function. For simplicity, assume any function supplied to applyFunction will only need to operate on a character, string, numbers, matrix, or list (restricted to character, string, number, matrix, or list items).
Scoring: Two points for items 1 and 2, six points for items 3 and 4. Four extra points if you can write item 3 or 4 without using a loop (of any sort) or data table.
Deadline for entries is 11:59 PM Eastern US Time on Monday, August 17.
11 Aug 2020: There's been a bit of confusion over a few aspects of the challenge. Let me see if I can clear things up:
- For trimmedMean, pctLo and pctHi are frational values bounded below by 0 and above by 0.5. If, for example, pctLo = 0.23, then the 23% of the values from the lower tail are removed.
- For listContains, if listIn = {"ABCD","DEFG", "GHIJ"} and valueString = "D", then [4,1,.] is returned.
- For applyFunction, both listIn and argList are arguments to fn. The main difference is that listIn will never be empty, but argList may be empty. For example, if listIn = {"ABCD","DEFG","GHIJ"} and fn = Contains, then argList should be a list that contains at least one substring to search for. If fn = Length, however, argList would be empty. In cases where fn can be applied to the individual elements, do so. For instance, if fn = Char, rather than returning the quoted list, quote the individual elements of the list.
Hope this makes things clearer. If not, please let me know.