Lisp Functions - University of Dayton



Users Guide

Software Functions

Loading the Software

Ensure that the files Genie.exe, msvcr70.dll, msvcr70.dll, and license.dll are in the same directory.

Left double click on genie.exe and you should find yourself in the application at the

Genie> prompt, ready to give commands

To change screen settings, right click in the area of the top margin of the window running the

application, select properties to get to the menu, select layout to change the screen area, colors to change the colors, options for quick edit, and font to change the font. Quick edit allows you to highlight a section of your screen by depressing and moving the mouse. Press ENTER to put the selection on the clipboard.

To load the software, double left click on a SOFTWARE icon on your desktop leading to the executable file "Genie.exe." The Genie is interactive - issue a correct command, receive a result. Commands may also be put into a command file with a .txt extension, loaded and executed. Enter :Q to quit.

To compile a text file of commands that you have written, enter (compile-file "filename.lisp")

Software expressions are shown bold and enclosed in parentheses. Some basic calculations follow for entering at the Genie prompt. Notice that commands are enclosed in parentheses and that the symbol ( is a substitute for returns. Genie returns the last calculation performed unless it prints a last calculation (side effect), which causes nil to be returned.

(+ 1 2 3 4 5) ( 15 (- 1 2 3 4 5) ( -13 (* 1 2 3 4 5) ( 120 (/ 16 8 4) ( 0.5

(expt 2 3) ( 8 (+ (expt 2 3) (expt 3 2)) ( 17 (+ (* 2 3 4) (/ 16 8)) ( 26

(list 'a 'b 'c) ( (A B C) (cons 'a '(b c)) ( (A B C) (append '(A B) '(C D)) ( (A B C D)

(random 1000) returns a random integer from 0 to 999. (random 1000.0) returns a random number between 0 and 1000. (random -1000.0) returns a random number between 0 and -1000.

The commands always return something. The software uses * for the last value returned and ** for the next to last value returned and *** for the third from last value returned. Thus after entering 2 3 4 in turn, the command (* * ** ***) returns 24. The first * denotes multiplication, the second * denotes 4, ** denotes 3 and *** denotes 2. The substitution of the *'s can save time.

The semicolon is used to indicate comments for code documentation.

The assignment operator is setf; (setf symbol object), e.g.,

(setf x '(a b c d e)) assigns the list (a b c d e) to the symbol x.

The command repeat #' repeats the operation on a list.

(repeat #' square '(1 2 3 4 5)) returns (1 4 9 16 25) by squaring each number in the list.

(list-of n item) returns a list of n items. (list-of 5 7) ( (7 7 7 7 7).

(args 'function-name) returns the arguments for the function. For example,

(args 'square) returns number, implying the square function takes a numerical argument.

To create a function, the commands are combined inside a defun (define function) structure. For example, to create a function that simulates the tossing of a fair die n times, one must create a name for the function, die-toss, determine its inputs, n, a number, and write the details.

(defun die-toss (n) ; takes an integer argument n

(let ((die-list nil)) ; die-list is local variable for holding output

(dotimes (i n die-list) ; toss the die n times and return die-list when done

(push (+ 1 (random 6)) die-list)))) ; (random 6) returns an integer 0 to 5, + 1 => 1 to 6

Then the command (die-toss 10) may return (2 1 6 3 4 3 2 6 5 2).

An error causes the following message:

;;; Use :C followed by an option to exit. Type :HELP for help.

;;; Restart options:

;;; 1 Abort to top level.

Enter :c 1 to return to the top level.

For re-editing the commands, the up arrow ( key brings up prior commands with each pressing. The down arrow key ( proceeds in the opposite direction.

(cs) returns a cleared buffer screen showing NIL and the Genie> prompt. Similar commands are (clear), (cls), and (clear-screen).

Templates commands are in bold and the arguments are in italics. For example,

(square n) is a template whereas (square 5) is a command returning 25. Note that if n is assigned to 5, then (square n) is a command returning 25.

Pressing the keys F3, F7 and F9 can help in editing and executing previous commands.

Key Function

F3 …………………………………………………………….. Brings up last command for execution

F7 ……………………………………………………………… Displays prior commands by number

As shown. Highlight a command and press Enter.

25: (anova cables)

26: (combination '(A B C D E) 3)

27: (pick-until 4 10)

28: (pi1000)

30: (firstn 100 (pi1000))

31: (sim-coins-1-1 100 1/2 10)

32: (sim-coins 1000 1/2 10)

F9 ……………………………………………………………….. Enter command number: 28

Brings up command 28 (pi1000) for execution. Can be used with F7

The main commands to help you understand the functions used in the text are:

Setf (setf x '(1 2 3 4 5)) ; assigns x to the list of integers 1 to 5. When x ; is entered, (1 2 3 4 5) is returned.

Mu (mu x) ; mu returns the mean of x, 3;

; also known as (aka) (mean x)

Repeat (repeat #' square x) ; returns '(1 4 9 16 25) squaring each in x; aka mapcar

List-of (list-of 4 x) ; returns ((1 2 3 4 5) (1 2 3 4 5) (1 2 3 4 5) (1 2 3 4 5))

Random (random 10) ; returns a random integer between 0 and 9.

(random -10.0) ; returns a random number between 0 and -9.

Sum (sum x) ; returns 15

Count (count 1 '(1 1 3 1 4)) ; returns 3.

Nth (nth 2 x) ; returns 3 as nth is 0-based

First (first x) ; 1

Fifth (fifth x) ; 5

Firstn (firstn 3 x) ; (1 2 3)

Flatten (flatten '((1 2)(3 4))) ; returns (1 2 3 4)

Member (member 3 x) ; ( 3 4 5), indicating true

(member 7 x) ; nil, indicating false.

Upto (upto 10) ; returns (1 2 3 4 5 6 7 8 9 10)

Pi1000 (pi1000) ; returns the list of the first 1000 decimal digits of (.

Pi-2500 pi-2500 ; is assigned to the first 2500 digits of (.

From-a-to-b (from-a-to-b 3 5 2) ; returns (3 5 7 9) in increments of 2 for a = 3,

; b =2. Default is 1 if 3rd argument 2 is omitted.

Args (args 'square) ; returns entering arguments of function square,

Print-Length (print-length n) ; enables the first n items of output list.

(print-length nil) ; enables the entire output list.

; for example, x ( (1 2 3 4 5)

; (print-length 3) x ( (1 2 3 …)

SWR (swr 10 (upto 100)) ; returns a random sample of size 10 with ; replacement from the integers 1 to 100

SWOR (swor 10 (upto 100)) ; returns random sample without replacement.

The command (apropos 'sim- ) prints everything in the environment that has sim- in its symbol designator.

Ensure that the parentheses balance before entering a command.

Nearly all simulations are preceded by the prefix sim- as in (sim-dice-roll 1000).

See the Software Tutorial guide for details on programming.

Ensure that the parentheses balance before entering a command.

Nearly all simulations are preceded by the prefix sim- as in (sim-dice-roll 1000).

Probability Functions

(all-occur n) returns the expected number of trials before all the integers from 1 to n are selected. (all-occur 2) returns 3 for a coin flip and (all-occur 6) returns 14.7 for a die roll.

(args function) returns the arguments for the function. (args 'square) ( number or list

(auto-maps n r) returns the number of automorphic mappings of the integers from 0 to n with exactly r matches.

(beta-b (0 (1 ( n () returns the beta error where (0 is the null value, (1 is the alternative value, ( is the standard deviation, n is th e sample size and ( in decimal or percent is the Type I error.

(beta-table (0 betas s n () prints a list of the alternative hypotheses and the beta errors.

(binomial-gf x frequency p-optional) returns an estimate for p, the chi-square value and the p-value for testing goodness of fit for x, a list of the successes x, and the corresponding frequencies. If p-optional is not provided, [pic] is estimated from the data.

(binomial-vs-normal n p) returns the binomial and normal probabilities for 10 values given binomial parameters n and p.

(binomial-vs-poisson n p) returns the binomial and Poisson probabilities for 10 values given the binomial parameters n and p.

(bootstrap-ak a-hat k-hat population n) returns n bootstrap samples from a gamma sample with a-hat and k-hat estimates for parameters ( and k.

(bootstrap-parameter n parameter data) returns the mean of n bootstrap samples taken from data for the parameter where parameter can be the mean, median, mode, variance or standard deviation.

(boxplot data) returns the minimum, 25th, 50th, 75th percentile and the maximum for data, a list of numbers in graphic form.

(canonical-dice n) returns the sum, canonical patterns, and the number of ways for each sum, where n is either 3 or 4..

(cdice sum n-dice) returns the cumulative probability P(X ( sum) from rolling n-dice.

(chi-sq-test list) returns the chi-square value, the p-value and the list of expected values for contingency tables where list is a list of the rows.

(coin-flips n p) returns the simulated results from n coin flips with probability of success p for the event heads. (coin-flips 100 1/20) may return 5 heads 95 tails.

(comb-list list n) returns the selected combinations of List taken n at a time and also what was not selected. (comb-list '(A B C D) 3) ((((A B C) (A B D) (A C D) (B C D)) ((D) (C) (B) (A))).

(combination n r) returns nCr. (combination 10 2) ( 45. If n is a list, the command returns the combinations taken r at a time.

(combination-list '(A B C D) 2 ) returns (((A B) (A C) (B C) (A D) (B D) (C D)), the combinations of '(A B C D) taken 2 at a time.

(depict data) prints statistics pertaining to data.

(dice sum n-dice) returns a list containing the favorable number of ways of getting sum with

n-dice, the total number of ways and the probability.

(dice-3 sum) returns the number of ways to get sum along with the canonical patterns.

(dice-4 sum) returns the number of ways to get sum along with the canonical patterns.

(d-print-maps n) returns the number of 0 to n matchings in the automapping of the integers 1 to n.

(e100) returns the first 100) decimal digits of e.

(entropy probability-list) returns the entropy of probability-list.

(entropy '(1/8 3/8 3/8 1/8) ( 1.8112.

(entropy-run n) returns the entropies of (n – 1) runs where the ith run has ith equal probabilities 1/i. For example, for i = 6 (a die), (entropy-run 6) returned (0 0 1.5 1.6982 1.7623 1.7933).

(ev x-list p-list) returns the expected value of RV X with values given by the x-list and probabilities given by the p-list.

(f n) returns n!. (f 5) ( 120.

(from-a-to-b a b step) returns a list of numbers from a to b with step difference.

(from-a-to-b 5 10 1/2) returns (5 5.5 6 6.5 7 7.5 8 8.5 9 9.5 10).

(Gamma n) returns (n - 1)! for nonnegative integer n and for fractionalized factorials. For example,

(Gamma 5) returns 4! and (Gamma 1/2) returns 1.7724 = [pic].

(gen-beta-oc (0 betas std-error alpha sample-size) returns a beta operating curve where beta is a list of alternative hypotheses.

(HDP data) returns a horizontal dot plot of the data using asterisks for values.

(H-vs-T x y) returns the winner between x and y where each is a 3-sequence of heads and tails,

e.g., (H-vs-T TTH THH) may return TTH wins.

(histogram data number-of-intervals) returns a graphic histogram of the data. If number of intervals n is not specified, the variable is computed from 2n > range of data.

(intersection A B) returns the intersection of sets A and B.

(IQR list) returns the interquartile range (IQR) where list is a list of numbers.

(kurtosis data) returns the value of the kurtosis for data, a list of numbers.

(Log-gamma x) returns the (Log (gamma x)) value for x. (Log-gamma 1/2)) ( 0.5724

(L-normal ( (2 x) returns the lower tail of the normal. (H-normal ( (2 x) returns the upper tail.

(MAD data) returns the median of the absolute deviations of each data point from the median.

(make-groups-from source sizes-list) returns groups of sizes from source.

(make-groups-from (upto 10) '(2 3 4)) returns ((3 4) (4 7 10) (2 4 6 9))

(median list-of-numbers) returns the median of a list of numbers.

(map-num n m) returns the number of auto-maps from the integers 1 to n with exactly m matches. (map-num 5 2) returns 20.

(Median-vs-Mu u ( n) compares the efficiency of the median with the mean by sampling from a normal distribution with given ( and (, using 100 samples for the median but only 64 for the mean. (Median-vs-Mu 0 1 3) may return ((0.074 . 0.167) (0.062 . 0.149) (0.231 . 0.046)).

(Min-exponentail k1 k2 k3 n) returns n minimum values of exponentials with parameters k1, k2 and k3. The returned sample is exponential with parameter k = (k.

(mlr-solve x1 x2 y) prints the regression equations and solutions

(MME-vs-MLE ( n m) returns m results with sample size n from estimating ( from density

f(x) = 1/ ( on [0, 1] using methods of moments and maximum likelihood estimators. The (-values ranges from 0 to (, choosing ( randomly.

(MMvsML ( n m) performs a method of moments estimate and maximum likelihood estimate for the distribution f(x) =[pic]on [0, 1] and records the more accurate estimator in each of the m runs with sample size n.

(mode list-of-numbers) returns the mode of list of numbers.

(map-num n r) returns the number of permutation maps onto self from n! permutations with exactly r matches. (map-num 3 2) ( 2 maps with no matches.

(moments-0 n sample) returns the nth sample moment about the origin.

(moments-m n sample) returns the nth sample moment about the mean.

(msk data) returns a list of the mean (, the skewness and the kurtosis for data, a list of numbers.

(mu list-of-numbers) returns the mean of list of numbers or the means of lists of numbers.

(mu '((1 2 3) (10 12 14) (37 56 94))) ( (2 12 62.33333).

(mu-var data) returns ( and (2 where data is a population. (mu-var '(1 2 3 4 5)) ( (3 2).

(mu-svar data) returns [pic] and s2 where data is a sample. (mu-svar '(1 2 3 4 5)) ( (3 2.5).

(normal-sample-size sigma interval-length alpha) returns the required sample size.

(nth-moment n sample) returns the nth sample moment of the sample.

(odds p) returns the odds give probability p.

(outliers data) returns the candidate outliers if any in a set of data.

(outliers-2 data) returns the candidate outliers if any in a set of data.

(p odds) returns the probability where odds is the first number to 1 FOR.

For example if the odds are 3:1, (p 3) returns 0.75.

(paired-t list1 list2 () returns the t-value, p-value, and (1 - ()% confidence interval.

(Pascal n) returns the nth row of Pascal's triangle. (Pascal 6) ( 1 6 15 20 15 6 1

(permutation-list '(A B C D) 2) returns

((A B) (A C) (A D) (B A) (B C) (B D) (C A) (C B) (C D) (D A) (D B) (D C))

(permutation n r) returns nPr. (permutation 10 2) ( 90.

(permute list) returns the permutations of list.

(permute '(a b c)) ( ((A B C) (A C B) (B A C) (B C A) (C A B) (C B A))

(pick-until target n) returns a simulated sequence of integers between 1 and n until the target integer appears followed by the number of trials. (pick-until 4 6) ( ((5 3 2 1 4) 5), where the last 5 indicates the total number of selections to find target 4.

(pi1000) returns first 1000 decimal digits of ( (1 4 1 5 9 …).

(pi-hat n) returns an estimate of ( with n the number of ordered xy-pairs tried.

(pi-hat 1000) may return 3.12.

(pm n r) returns the probability of exactly r matches in the n! permutation maps.

(pm 7 3) ( 0.00625

(print-map n) returns the number of self-mappings of the integers from 0 to n.

(Poisson-gf x-data frequency) returns an estimate for k, the chi-square value and the p-value for testing goodness of fit of the x-data to a Poisson distribution. X-data is a list of the occurrences with the corresponding frequencies.

(Poisson-vs-normal k) returns the Poisson and normal probabilities for 10 values.

(poker-5) returns the probability of selected 5-card poker hands: one-pair, two-pair,

three-of-a-kind, four-of-a-kind, full-house, straight, flush and bust.

(print-count-a-b a b list) prints the count of the integers from a to b in list.

(print-map n) returns the number of permutation maps with matches from 0 to n.

(print-permutations (upto n)) prints the permutations of 1 - n.

(pro data) plots the residuals as y-values with ordered 1 to n x-values.

(percentile percent data) returns the percentile for data, a list of numbers.

(range list) returns the range where a list of numbers.

(random-sample max-size) returns a random sample from a random distribution with random parameters.

(random-vs-pi-digits) compares the count of first 1000 digits of ( with 1000 random digits.

(re-group list size-list) re-groups the list into individual lists based on size-list.

(re-group (upto 12) '(3 4 5)) returns ((1 2 3) (4 5 6 7) (8 9 10 11 12)).

(rel-primep a b) returns t for true if a is relatively prime to b or nil for false if not.

(rel-prime-n n) returns the number of positive integers relatively prime to n and ( n.

(rel-prime 20.

(Wsign-test data H0) returns the sum of the ranks for both the positive and negative magnitudes for Wilcoxon Signed Rank Test. H0 is the value of the median for the null hypothesis.

(WMW x y) returns the minimum sum for testing the Wilcoxon-Mann-Whitney Statistic for ranked sums of two groups x and y.

(WMW-normal x y test-type) returns the rank sum of each group x and y and the z-value and appropriate p-value for the normal approximation to the Wilcoxon-Mann-Whitney rank sum test.

Data Generators

(combination-list '(A B C D) 2 ) returns (((A B) (A C) (B C) (A D) (B D) (C D)), the combinations of '(A B C D) taken 2 at a time.

(gen-comb lst n) prints all n combinations taken r at a time from the lst.

(gen-comb '(1 2 3) 3) ( (1 2 3) (1 3 3) (2 3 3) (2 3 3).

(n-items n m) returns the average number of members from 1 to n in m trials.

(permutation-list '(A B C D) 2) returns

((A B) (A C) (A D) (B A) (B C) (B D) (C A) (C B) (C D) (D A) (D B) (D C))

(pick-until target n) returns a simulated sequence of integers between 1 and n until the target integer appears followed by the number of trials. (pick-until 4 6) ( ((2 3 2 1 4) 5) where the 5 indicates the total number of selections to find target 4.

(pi1000) returns first 1000 decimal digits of (.

(print-combinations lst n) prints all n combinations taken r at a time from the lst.

(print-combinations '(1 2 3) 3) ( (1 2 3) (1 3 3) (2 3 3) (2 3 3).

(random-sample max-size) returns a random sample from a random distribution with random parameters

(r-n n) returns a random permutation of the integers from 0 to n – 1.

(sim-all-occur n) returns a simulated number of trials before all n items are selected from an equally likely sample space.

(sim-n-all-occur n m) returns the average of m simulations of (sim-all-occur n).

(sim-bernoulli p) returns 1 if success occurs with probability p or 0 if failure occurs with probability q where p + q = 1. (sim-bernoulli 1/20) probably returns 0.

(sim-beta ( n) returns a random sample of size n with parameters ( and ( = n - ( + 1.

(sim-bet amount fraction m n) returns the outcomes of betting fraction of the amount each time for m bets and n trial runs.

(sim-bin n p) simulates a binomial RV with parameters n and p by returning the number of successes.

(sim-binomial n p m) returns a random m-sample from a binomial distribution with parameters n and p. (sim-binomial 10 1/2 5) may return (4 6 5 7 4).

(sim-chi-sq v n) returns n estimates for v degrees of freedom.

(sim-chuck-a-luck die-num n) returns simulated values of winning, losing, the probability for winning and the number of times the selected die-num occurred from n games of chuck-a-luck.

(sim-circle-pts n) simulates the probability of a random (X, Y) point with each coordinate chosen from N(0, 1) being within the unit circle as an estimate for (chi-sq 2 1).

(sim-clt-binomial n p m) returns m estimates of a binomial RV with parameters n and p.

(sim-clt-exponential k n) returns n estimates of k.

(sim-clt-poisson k n) returns n estimates of k

(sim-clt-weibull ( k n) returns n trial results of sampling from a Weibull distribution.

(sim-coins n p m) returns a list of the number of heads from n coin flips repeated m times with p the probability of heads. (sim-coins 100 1/2 10) ( (53 37 76 45 53 49 53 65 71 39).

(sim-d-uniform a b n) returns a random sample of size n from the discrete uniform on [a, b].

(sim-die-roll n) returns simulated outcomes from the tossing of a fair die n times or 10 dice simultaneously. (sim-die-roll 10) may return (3 6 4 1 2 5 3 6 4 5).

(sim-dice-roll n) returns the outcome sums from n fair pairs of dice rolls.

(sim-doors) simulates the 3-doors game with two goats and a car behind the doors.

(sim-exponential k n) returns a random sample of size n from an exponential distribution with parameter k.

(sim-gamma ( k n) returns random n-sample from gamma distribution with parameters ( and k.

(sim-hyperg A B n m) returns m random samples from a hypergeometric distribution.

(sim-h-vs-t seq1 seq2 n) simulates head/tails sequence of 3 to determine which sequence occurs first. (sim-h-vs-t '(T T H) '(T H T) 100) may return (65 35) meaning the sequence TTH occurred 65 times in 100 trials and sequence THT occurred 35 times.

(sim-hypothesis ( ( ( n) returns the simulated results from testing the null hypothesis

H0: ( = (0 vs. H1: ( ( (0 given the parameters.

(sim-integrate function a b n) returns a Monte Carlo simulation value for[pic] using an n-sample for the simulation where function is the function to be integrated.

(sim-lr ( ( ( n) generates n yi = ( + (xi + ei with xi specified as the integers from 1 to n. A random component selected from N(0, ( 2) is added to each ( + (xi value.

(sim-m-e-geom m n) returns m simulations of (sim-e-geom n). (mu (sim-m-e-geom 100 6)) should return a value close to 14.7.

(sim-mu-dice n) returns the average sum of a pair of dice thrown n times

(sim-negbin p k) returns the simulated number of trials before k successes occur with probability p of success. (sim-negbin 1/2 3) may return 8 (negative binomial).

(sim-neg-binomial p k m) returns m trials of (sim-negbin p k).

(sim-nci ( ( ss n () returns n (100 - ()% confidence intervals with sample size ss and ( in percent.

(sim-normal ( ( n) returns a random n-sample from N((, ( 2).

(sim-normal-polar ( ( n) returns n random samples from N((, (2) using the polar method.

(sim-paradox-wr n) returns the results from randomly selecting a ball from an urn containing a white and a red ball. Repeatedly a ball is selected. If the ball is white, stop. If the ball is red, put the red ball back into the urn, add a red ball, and repeat.

(Sim-%-missed) returns simulated number of unselected with replacement from n trials.

For example, how many are missing taking 200 = n trials to pick m = 100 items

X = X1 + X2 + ... + Xn, p = P(missed) = (n - 1)/n one trial"

(sim-poisson k n) returns a random n-sample from a Poisson distribution with parameter k.

(sim-poisson 5 10) may return (5 6 5 3 4 5 6 5 4 5).

(sim-solitaire n) matches 2 random permutations of the integers from 0 to n and returns the number of matches.

(sim-t v n) returns n random samples from a t-distribution with v degrees of freedom.

(sim-triangle a b m n) returns n samples from the triangular distribution with mode m.

(sim-uniform a b n) returns a random n-sample from the continuous uniform on interval [a, b].

(sim-uniform 0 1 5) may return (0.3445 0.6676 0.3217 0.5698 0.8767)

(sim-wallet max-amount n) where a random number from 0 to max-amount is put in each wallet and the results are returned for n wager

(sim-weibull ( k n) returns n samples from a Weibull distribution with parameters k and (.

(sim-weibull 2 3 5) may return (0.2701250 0.4768672 0.6331509 0.6594265 0.5239947).

(urn-wr n m) returns the results from (paradox-wr n) repeated m times. In an urn are 1 white ball and 1 red ball. A ball is randomly selected. If the ball is white, start over. If red, add another red ball and continue. Each time a red is selected, replace the red ball and add another red ball.

(swr n sample) returns n random samples with replacement from sample.

(swor n sample) returns n random samples without replacement from sample.

(upt0 n) returns the integers from 0 to n.

(upto n) returns the integers from 1 to n.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download