Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UTRGV RUG: Intro to Julia

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

UTRGV RUG: Intro to Julia

An Introduction to the Julia Programming Language Presented to the UTRGV R Users Group

Avatar for Guillermo Garza

Guillermo Garza

April 29, 2014

Other Decks in Programming

Transcript

  1. R Users Group: An Introduction to the Julia Programming Language

    April 29, 2014 Guillermo Garza Department of Mathematics The University of Texas-Pan American
  2. What is Julia? Julia is a high-level, high-performance dynamic programming

    language for technical computing, with syntax that is familiar to users of other technical computing environments.∗ ∗http://julialang.org/
  3. Helpful Websites • Main website http://julialang.org/ • Download here http://julialang.org/downloads/

    • Documentation http://docs.julialang.org/en/latest/ • Mailing List http://groups.google.com/forum/?fromgroups=#! forum/julia-users • List of Posts on Julia www.reddit.com/r/julia
  4. Why Julia? From the developers:† We want a language that’s

    open source, with a liberal license. We want the speed of C with the dynamism of Ruby. We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab. We want something as usable for general programming as Python, as easy for statistics as R, as natural for string processing as Perl, as powerful for linear algebra as Matlab, as good at gluing programs together as the shell. Something that is dirt simple to learn, yet keeps the most serious hackers happy. We want it interactive and we want it compiled. †http://julialang.org/blog/2012/02/why-we-created-julia/
  5. Why Julia for R Users? • A high-level, high-performance dynamic

    programming language with easy and familiar syntax • Unvectorized code is fast • Techniques for large data sets – parallelization, distributed computing, map/reduce – can be used easily. (Support for multi-threaded execution is already available.) IMHO, Julia can (or will be able to) do everything R can and more.
  6. Install Some Packages to Get Started Pkg.add("Stats") # statistical functions

    Pkg.add("Distributions") # probability distributions Pkg.add("RDataSets") # example datasets from R Pkg.add("GLM") # generalized linear models Pkg.add("Gadfly") # grammar of graphics plotting
  7. Distributions package‡ using Distributions Z = Normal() # standard normal

    X = Normal(100, 20) # arbitrary normal N = Binomial(8, 0.5) # binomial random variable typeof(N) mean(X) std(X) ## equivalent to rnorm, dnorm, pnorm, qnorm rand(X, 10) cdf(X, 120) - cdf(X, 80) pdf(X, 100) quantile(X, 0.16) ‡http://juliastats.github.io/Distributions.jl/distributions.html
  8. Gibbs Sampler in Julia§ f (x, y) = kx2 exp(−xy2

    − y2 + 2y − 4x) where x|y ∼ Γ(3, y2 + 4) y|x ∼ N 1 1 + x , 1 2(1 + x) §borrowed from Douglas Bates http://www.stat.wisc.edu/~bates/JuliaForRProgrammers.pdf and Darren Wilkinson http://darrenjw.wordpress.com/
  9. Gibbs Sampler in Julia using Distributions function gibbs(N, thin) mat

    = Array(Float64,(N,2)) x = y = 0. for i in 1:N for j in 1:thin x = rand(Gamma(3., 1/(y^2 + 4))) y = rand(Normal(1/(1+x), 1/(2+2x))) end mat[i,1] = x; mat[i,2] = y end mat end @time X = gibbs(500, 10000)
  10. Distributed version of Gibbs Sampler Put previous function definition and

    the following into a script called "distgibbs.jl" and run with "julia -p 4 distgibbs.jl” dgibbs(N, thin) = DArray(I->gibbs(map(length,I)[1],thin),(N,2)) @time X = dgibbs(500, 10000) The DArray function creates a distributed array. Now the function is running on four processing cores. This is a quick and easy way to do parallel computing.
  11. DataFrames Package¶ using RDatasets, Dataframes iris = dataset("datasets", "iris") #creates

    a Dataframe head(iris) iris[:Species] iris[1:20, :Species] iris[iris[:SepalLength] .> 3, :] # reshape with the stack function stack(iris, :SepalLength) #reading from a csv file df = readtable("data.txt", separator = ’\t’) ¶http://juliastats.github.io/DataFrames.jl
  12. DataFrames Package names = DataFrame(ID = [1, 2], Name =

    ["John Doe", "Jane Doe"]) jobs = DataFrame(ID = [1, 2], Job = ["Lawyer", "Doctor"]) join(names, jobs, on = :ID, kind = :inner) join(names, jobs, on = :ID, kind = :left) join(names, jobs, on = :ID, kind = :right) join(names, jobs, on = :ID, kind = :outer) join(names, jobs, on = :ID, kind = :semi) join(names, jobs, on = :ID, kind = :anti)
  13. Gadfly Package The Gadfly package implements a “grammar of graphics,”

    in a manner similar to the ggplot package in R. using Gadfly using RDatasets iris = dataset("datasets", "iris") p = plot(iris, x ="SepalLength", y= "SepalWidth") p = plot(iris, x ="SepalLength", y= "SepalWidth" , color = "Species" , Geom.point) draw(PDF("myplot.pdf", 4inch, 3inch), p) https://github.com/dcjones/Gadfly.jl