A fractal Christmas tree in SAS

In my previous post, I described how to implement an iterated function system (IFS) in the SAS/IML language to draw fractals. I used the famous Barnsley fern example to illustrate the technique. At the end of the article I issued a challenge: can you construct an IFS whose fractal attractor looks like a Christmas tree?

I started my attempt by "straightening" the fern example, but it ended up looking like a "fishbone fern" instead of an evergreen tree. I decided to abandon the four-function IFS and adopt a seven-function system. In addition to "left" and "right" branches, I introduced transformations for "left diagonal" and "right diagonal" branches. I also played with the relative probabilities and the translation parameters to get the right "fill" for the tree.

Eventually I constructed the following program, which implements an IFS that creates the "SAS Christmas tree" that is shown at the left. I'm not 100% satisfied with it, but as Linus says in A Charlie Brown Christmas, it's not "such a bad little tree."

proc iml;
/* For an explanation of how to construct an iterated function system in SAS, see
   http://blogs.sas.com/content/iml/2012/12/12/iterated-function-systems-and-barnsleys-fern-in-sas/
*/
/* Each row is a 2x2 linear transformation */
/* Christmas tree */
L = {0.03  0     0    0.1,
     0.85  0.00  0.00 0.85,
     0.8   0.00  0.00 0.8,
     0.2  -0.08  0.15 0.22,
    -0.2   0.08  0.15 0.22,
     0.25 -0.1   0.12 0.25,
    -0.2   0.1   0.12 0.2};
/* ... and each row is a translation vector */
B = {0 0,
     0 1.5,
     0 1.5,
     0 0.85,
     0 0.85,
     0 0.3,
     0 0.4 };
prob = { 0.02 0.6 0.1 0.07 0.07 0.07 0.07};
L = L`; B = B`; /* For convenience, transpose the L and B matrices */
 
/* Iterate the discrete stochastic map */
N = 1e5;          /* number of iterations */
x = j(2,N); k = j(N,1);
x[,1] = {0, 2};   /* initial point */
call randgen(k, "Table", prob); /* values 1-7 */
 
do i = 2 to N;
   x[,i] = shape(L[,k[i]], 2)*x[,i-1] + B[,k[i]]; /* iterate */
end;
 
/* Plot the iteration history */
y = x`;
create IFS from y[c={"x" "y"}]; append from y; close IFS;
quit;
 
/* basic IFS Christmas Tree */
ods graphics / width=200px height=400px;
proc sgplot data=IFS;
  title "SAS Christmas Tree";
  scatter x=x y=y / markerattrs=(size=1 color=ForestGreen);
  yaxis display=none;
  xaxis display=none;
run;

As I looked at the tree, however, I decided that it seemed rather bare. It seemed to "need a little love," so I decided to add some brightly colored, ornamental, decorations. I took a random sample of points on the attractor and drew balls at those points. I also added a star on top, all courtesy of PROC SGPLOT. You can download the complete program that creates the final image, which is shown at left. I used a few programming tricks, such as a data attribute map to assign customized, bright, colors for the ornaments.

So now it's your turn! In the comments, submit or link to a SAS program that creates a Christmas tree, a snowflake, or some other seasonal image. Be creative! My colleague, Robert Allison, has already posted a few whimsical SAS graphs for the holidays.

As for me, I'm finished blogging until the New Year. However, I'm not exactly taking a vacation from SAS programming because I'll be working to finish my forthcoming book, Simulating Data with SAS.

Thank you, readers, for an amazing year of blogging, interacting, and sharing ideas in 2012. Thanks to you, this blog has become the most widely-read blog at SAS!

tags: Just for Fun

2 Comments

  1. Charlie Huang
    Posted December 14, 2012 at 3:02 pm | Permalink

    It is just so pretty!

  2. Cindy
    Posted December 25, 2012 at 9:23 pm | Permalink

    Interesting... Amazing...

3 Trackbacks

  1. [...] program downloads and runs Rick Wicklin's "Christmas Tree" challenge, which he posted on The DO Loop blog last week. filename _inbox [...]

  2. By A Christmas tree matrix - The DO Loop on December 18, 2013 at 5:26 am

    [...] Christmas tree matrix Rick Wicklin|December 18, 2013 20Tweet O Christmas tree, O Christmas tree, Last year a fractal made thee! O Christmas tree, O Christmas tree, A heat map can display thee! O tree of green, adorned with [...]

  3. By 13 popular articles from 2013 - The DO Loop on January 7, 2014 at 10:13 am

    [...] fractals? Like Christmas? Then you will enjoy seeing the fractal Christmas tree that I created for Christmas [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>