How-To in Julia
Total Page:16
File Type:pdf, Size:1020Kb
Appendix A How-to in Julia The code examples in this book are primarily designed to illustrate statistical concepts. However, they also have a secondary purpose. They serve a way of learning how to use Julia by example. Towards this end, the appendix links language features with specific code listings in the book. This appendix can be used on an ad-hoc basis to find code examples where you can see “how to” do specific things in Julia. Once you find the specific “how to” that you are looking for, you can refer to its associated code example, referenced via “⇒”. This appendix is also available at https://statisticswithjulia.org/howto.html. The appendix is broken up into several subsections as follows. Basics (Section A.1)dealwith basic language features. Text and I/O (Section A.2) deal with textual operations as well as input and output. Data Structures (Section A.3) deal with data structures and their use. This includes basic arrays as well as other structures. Data Frames, Time-Series, and Dates (Section A.4)deal with Data Frames and related objects for organizing heterogeneous data. Mathematics (Section A.5) covers various mathematical aspects of the language. Randomness, Statistics, and Machine Learning (Section A.6) deal with random number generation, elementary statistics, distributions, statistical inference, and machine learning. Graphics (Section A.7) deals with plotting, manipulation of figures, and animation. A.1 Basics Types Check the type of an object ⇒ Listing 1.2. Specify the type of an argument to a function ⇒ Listing 1.11. Specify the type of an array when initialized using zeros() ⇒ Listing 1.8. © Springer Nature Switzerland AG 2021 475 Y. Nazarathy and H. Klok, Statistics with Julia, Springer Series in the Data Sciences, https://doi.org/10.1007/978-3-030-70901-3 476 APPENDIX A . HOW-TO IN JULIA Convert the type of a variable with convert() ⇒ Listing 3.22. Convert the type of a variable with a constructor like Int() ⇒ Listing 1.11. Use a 32 bit float instead of default 64 bit float with the f0 float literal ⇒ Listing 9.20. Use big representation of numbers using big() ⇒ Listing 2.3. Check if a variable is immutable with isimmutable() ⇒ Listing 4.1. Variables Modify a global variable inside a different scope by declaring global ⇒ Listing 1.5. Assign two values in a single statement (using an implicit tuple) ⇒ Listing 1.6. Copy a variable, array, or struct with copy() ⇒ Listing 4.2. Copy a variable, array, or struct with deepcopy() ⇒ Listing 4.2. Conditionals and Logical Operations Use the conditional if statement ⇒ Listing 1.6. Use the conditional else statement ⇒ Listing 1.11. Use the conditional elseif statement ⇒ Listing 1.17. Use the shorthand conditional formatting operator ?: ⇒ Listing 2.5. Carry out element-wise and using .& ⇒ Listing 4.10. Carry out element-wise negation using .! ⇒ Listing 4.10. Use logical or || ⇒ Listing 1.13. A.1. BASICS 477 Use short circuit evaluation with logical and && ⇒ Listing 8.16. Loops Create a while loop ⇒ Listing 1.11. Loop over values in an array ⇒ Listing 1.1. Create nested for loops ⇒ Listing 1.6. Break out of a loop with break ⇒ Listing 2.5. Execute the next loop iteration from the top with continue ⇒ Listing 2.5. Loop over an enumeration of (Index, value) pairs created by enumerate() ⇒ Listing 4.30. Functions Create a function ⇒ Listing 1.6. Create a one line function ⇒ Listing 1.10. Create function using begin and end ⇒ Listing 3.32. Create a function that returns a function ⇒ Listing 1.7. Pass functions as arguments to functions ⇒ Listing 10.10. Create a function with a multiple number of arguments ⇒ Listing 1.7. Use an anonymous function ⇒ Listing 1.15. Define a function inside another function ⇒ Listing 2.4. Create a function that returns a tuple ⇒ Listing 7.10. 478 APPENDIX A . HOW-TO IN JULIA Setup default values to function arguments ⇒ Listing 10.10. Other Basic Operations Check the running time of a block of code ⇒ Listing 1.3. Increment values using += ⇒ Listing 1.8. Do element-wise comparisons such as, for example, using .> ⇒ Listing 2.9. Apply an element-wise computation to a tuple ⇒ Listing 2.10. Use the logical xor() function ⇒ Listing 2.12. Set a numerical value to be infinity with Inf ⇒ Listing 3.6. Include another block of Julia code using include() ⇒ Listing 3.34. Find the maximal value amongst several arguments using max() ⇒ Listing 7.1. Find the minimal value amongst several arguments using min() ⇒ Listing 5.20. Metaprogramming Define a macro ⇒ Listing 9.10. Interacting with Other Languages Copy data to the R environment with @rput from package RCall ⇒ Listing 1.18. Get data from the R environment with @rget from package RCall ⇒ Listing 1.18. Execute an R-language block with the command R from package RCall ⇒ Listing 1.18. Setup a Python object in Julia using @pyimport from package PyCall ⇒ Listing 1.19. A.2. TEXT AND I/O 479 A.2 Text and I/O Strings Split a string based on whitespace with split() ⇒ Listing 1.9. Use LaTeX formatting for strings ⇒ Listing 2.4. See if a string is a substring of another string with occursin() ⇒ Listing 4.30. Concatenate two strings using * ⇒ Listing 1.9. Text Output Print text output including new lines, and tabs ⇒ Listing 1.1. Format variables within strings when printing ⇒ Listing 2.1. Display an expression to output using display() ⇒ Listing 1.8. Display an expression to output using show(stdout,...) ⇒ Listing 9.5. Present the value of an expression with @show ⇒ Listing 4.1. Display an information line with @info ⇒ Listing 9.7. Redirect the standard output to a file ⇒ Listing 9.7. Reading and Writing From Files Open a file for writing with open() ⇒ Listing 4.12. Open a file for reading with open() ⇒ Listing 4.30. Write a string to a file with write() ⇒ Listing 4.12. 480 APPENDIX A . HOW-TO IN JULIA Close a file after it was opened ⇒ Listing 4.12. Read from a file with read() ⇒ Listing 4.12. Find out the current working directory with pwd() ⇒ Listing 4.31. See the list of files in a directory with readdir() ⇒ Listing 4.31. See the directory of the current file with @ DIR ⇒ Listing 9.9. Change the current directory with cd() ⇒ Listing 9.9. CSV Files Read a CSV file to create a dataframe with a header ⇒ Listing 4.3. Read a CSV file to create a dataframe with without a header ⇒ Listing 6.1. Write to a CSV file with CSV.write() ⇒ Listing 4.32. JSON Parse a JSON file with JSON.parse() ⇒ Listing 1.9. BSON WritetoaBSONfile ⇒ Listing 9.20. ReadfromaBSONfile ⇒ Listing 9.19. HTTP Input Create an HTTP request ⇒ Listing 1.9. Convert binary data to a string ⇒ Listing 1.9. A.3. DATA STRUCTURES 481 A.3 Data Structures Creating Arrays Create a range of numbers ⇒ Listing 1.2. Create an array of zero values with zeros() ⇒ Listing 1.8. Create an array of one values with ones() ⇒ Listing 2.4. Create an array with a repeated value using fill() ⇒ Listing 7.10. Create an array of strings ⇒ Listing 1.1. Create an array of numerical values based on a formula ⇒ Listing 1.1. Create an empty array of a given type ⇒ Listing 1.3. Create an array of character ranges ⇒ Listing 2.2. Create an array of tuples ⇒ Listing 6.6. Create an array of arrays ⇒ Listing 1.15. Basic Array Operations Discover the length() of an array ⇒ Listing 1.6. Access elements of an array ⇒ Listing 1.6. Obtain the first and last elements of an array using first() and last() ⇒ Listing 3.32. Apply a function like sqrt() onto an array of numbers ⇒ Listing 1.1. Map a function onto an array with map() ⇒ Listing 8.10. 482 APPENDIX A . HOW-TO IN JULIA Append with push!() to an array ⇒ Listing 1.3. Convert an object into an array with the collect() function ⇒ Listing 1.9. Pre-allocate an array of a given size ⇒ Listing 1.16. Delete an element from an array or collection with deleteat!() ⇒ Listing 2.4. Find the first element of an array matching a pattern with findfirst() ⇒ Listing 2.4. Append an array to an existing array with append!() ⇒ Listing 2.5. Sum up two equally size arrays element by element ⇒ Listing 3.7. Stick together several arrays into one array using vcat() and ... ⇒ Listing 7.9. Further Array Accessories Sum up values of an array with sum() ⇒ Listing 1.7. Search for a maximal index in an array using findmax() ⇒ Listing 1.8. Count the number of occurrence repetitions with the count() function ⇒ Listing 1.9. Sort an array using the sort() function ⇒ Listing 1.9. Filter an array based on a criterion using the filter() function ⇒ Listing 1.15. Find the maximal value in an array using maximum() ⇒ Listing 2.3. Count the number of occurrence repetitions with the counts() function from StatsBase ⇒ Listing 2.3. Reduce a collection to unique elements with unique() ⇒ Listing 2.5. Check if a an array is empty with isempty() ⇒ Listing 3.6. A.3. DATA STRUCTURES 483 Find the minimal value in an array using minimum() ⇒ Listing 3.6. Accumulate values of an array with accumulate() ⇒ Listing 3.30. Sort an array in place using the sort!() function ⇒ Listing 6.6. Sets Check if an element is an element of a set with in() ⇒ Listing 2.6. Check if a set is a subset of a set with issubset() ⇒ Listing 2.6. Obtain the set difference of two sets with setdiff() ⇒ Listing 2.5.