Author Archives: Francis Smart

Julia style string literal interpolation in R

By: Francis Smart

Re-posted from: http://www.econometricsbysimulation.com/2014/10/julia-style-string-literal.html

I feel like a sculptor who has been using the same metal tools for the last four years and happened to have looked at my comrades and found them sporting new, sleek electric tools. Suddenly all of the hard work put into maintaining and adapting my metal tools ends up looking like duck tape and bubble gum patches.

I hate to say it but I feel that I have become somewhat infatuated with Julia. And infatuation is the right word. I have not yet committed the time to fully immerse myself in the language, yet everything I know about it makes me want to learn more. The language is well known for its mind-blowingly speed accomplished through just-in-time compiling. It also has many features which enhance the efficiency and readability of its code (see previous post, note the documentation has greatly improved since posting).

However, though I very much want to, I cannot entirely switch my coding needs from R into Julia. This is primarily due to my ongoing usage of packages such as RStudio’s “Shiny” and the University of Cambridge’s server side software for building adaptive tests, “Concerto”. And so with regret I will resign my Julia coding to probably a minor portion of my programming needs.

That does not mean however that I can’t make some small changes to make R work more like Julia. To this end I have programmed a small function p which will replace string literals identified as “Hello #(name), how are you?” with their values being evaluated. If there are nested parenthesizes then it is necessary to close the literal with “)#”, for example “c=#(b^(1+a))#”.

# Julia like text concaction function.
p <- function(..., sep="", esc="#") { 
  # Change escape characters by specifying esc.
  # Break the input values into different strings cut at '#('
  x <- paste(..., sep=sep)
  x <- unlist(strsplit(x, paste0(esc,"("), fixed = TRUE))
 
  # The first element is never evaluated.
  out <- x[1]
  # Check if x has been split.
  if (length(x)>1) for (i in 2:length(x)) {
    y <- unlist(strsplit(x[i], paste0(")",esc), fixed = TRUE))
    if (x[i]==y[1])
      y <- unlist(regmatches(x[i], regexpr(")", x[i]),
                             invert = TRUE))
    out <- paste0(out, eval(parse(text=y[1])), y[-1])
  }
  out
}
 
name="Bob"
height=72
weight=230
 
# Let's see it in action
p(sep=" ", "Hello #(name).",
  "My record indicates you are #(height) inches tall and weigh #(weight) pounds.",
  "Your body mass index is #(round(703*weight/height^2,1))#") 
# [1] "Hello Bob. My record indicates you are 72 inches tall and weigh 230 pounds. 
# Your body mass index is 31.2" 
 
# The other nice thing about the p function is that it can be used to concat
# strings as a shortcut for paste0.
p("QRS","TUV")
# [1] "QRSTUV"

Created by Pretty R at inside-R.org

Thank you SO community for your help.

Julia: The “Distributions” Package

By: Francis Smart

Re-posted from: http://www.econometricsbysimulation.com/2014/10/julia-distributions-package.html

This is a follow up to my post from a few days ago exploring random number generation in Julia’s base system.  In this post I will explore the ‘distributions’ package.

You can find the excellent documentation on the “Distributions” package at:
http://distributionsjl.readthedocs.org/en/latest/index.html

# First let’s set the current directory
cd(“C:/Dropbox/Econometrics by Simulation/2014-10-October/”)

# This post uses the following distributions
using Distributions
using Gadfly

# I have got to say that I love the way Julia handles distributions
# as I discovered through this post.

# The Distributions package gives trenendous power to the user by
# providing a common framework to apply various function.

# For instance let’s say you want to draw 10 draws from a Binomial(n=10, p=.25) distribution
rand(Binomial(10, .25), 1, 10)
#  4  3  0  5  1  3  5  2  2  1

# Looks pretty standard right? Well, what if we want the mean?
mean(Binomial(10, .25))
# 2.5

# mode, skewness, kurtosis, median?
a = Binomial(10, .25)
println(“mode:”, mode(a), ” skewness:”, skewness(a),
        ” kurtosis:”, kurtosis(a), ” median:”, median(a))
# mode:3 skewness:0.3651483716701107 kurtosis:-0.06666666666666667 median:3
 

# Cool huh?

# Let’s see how we can use Gadfly to easily plot some distributions:
# First we generate the plot (assign it to a ‘p’ for later drawing to disk)
#  In order to plot the different CDFs I will use an ‘anonymous’ function defined:
#  argument -> f(argument)
p=plot([x -> cdf(Normal(5,5^.5), x),
      x -> cdf(Gamma(3,1), x),
      x -> cdf(Exponential(2), x)]
      , 0, 10)

 # Write the graph to disk
draw(PNG(“2014-10-06CDF.png”, 24cm, 12cm), p)

g = plot([x -> pdf(Normal(5,2), x),
      x -> pdf(Gamma(3,1), x),
      x -> pdf(Exponential(2), x)]
      , 0, 10)

draw(PNG(“2014-10-06PDF.png”, 24cm, 12cm), g)

Julia: Random Number Generator Functions

By: Francis Smart

Re-posted from: http://www.econometricsbysimulation.com/2014/09/julia-random-number-generator-functions.html

In this post I will explore the built in Random Number functions in Julia. These can be found in the documentation at: #random-numbers

As with most random number generators it is useful to manually set the ‘seed’. This allows for replication of results across multiple runs.

Set seed is accomplished with the simple ‘srand’ function:

# We can verify that setting the seed results in identical draws from
# the random distribution by using the uniform [0,1) random draw
# function ‘rand’:

srand(123)
rand()
# 0.7684476751965699

srand(123); rand()
# 0.7684476751965699

# Julia random drawn objects have the convience of shaping themselves
# into random arrays the size specified by their arguments.
# For example a 2 by 10 random array

a1 = rand(2,10)
# 2×10 Array{Float64,2}:
# 0.940782  0.790201  0.900925  0.031831  …  0.759755  0.234544   0.627093
# 0.48      0.356221  0.529253  0.900681     0.19178   0.0976698  0.946697

# You can also use Julia to modify an existing array by filling it with
# random elements.
# First create a 3 x 3 of zeros

z1 = zeros(3,3)
# 3×3 Array{Float64,2}:
# 0.0  0.0  0.0
# 0.0  0.0  0.0
# 0.0  0.0  0.0

# Now populate it with random uniforms

rand!(z1)
# Notice the ! notation for functions that modify their arguments

z1
# 3×3 Array{Float64,2}:
# 0.615666  0.76537    0.256698
# 0.984495  0.322722   0.0808458
# 0.775836  0.0696626  0.275759

# rand can also be used to draw elements from the range of r in rand(r)
# For example, to draw a 2 x 2 array with elements drawn from 1 to 10.

rand(1:10,2,2)
# 2×2 Array{Int64,2}:
# 10  3
#  9  9

# We might also want to generate random boolean values which could be
# accomplished with either

rand(0:1,2,2).==1
#  true  false
# false   true
# Or the specific function

randbool(2,2)
#  false  false
#   true  false

# The final type of random variable that comes with the base
# install of Julia is the random normal generator:
randn(3,2)
# 3×2 Array{Float64,2}:
# -1.51181    0.139519
# -0.21379   -0.30305
# -0.711524  -0.048655

# This generates based on standard normal but of course we can easily
# any standard normal to have standard deviation x and mean y
x = 90
y = -34
randn(3,2)*x+y
# 3×2 Array{Float64,2}:
# -4.43119   -101.457
# -137.832    38.9093
# -9.66817   -20.2061


# In the original version of the post I mentioned that the base package did not contain much options regarding distributions to draw from. However, there is a package called distributions which I will explore more in depth in a future post which I have been promised can satisfy all of my distributional desires (see comments below).