The Logistic Map: Visualizing Chaos in R

In the 1970s, professor Robert May became interested in the relationship between complexity and stability in animal populations. He noted that even simple equations used to model populations over time can lead to chaotic outcomes. The most famous of these equations is as follows:

xn+1 = rxn(1 – xn)

xn is a number between 0 and 1 that refers to the ratio of the existing population to the maximum possible population. Additionally, r refers to a value between 0 and 4 which indicates the growth rate over time. xn is multiplied by the r value to simulate growth where (1 – xn) represents death in the population.

Lets assume a population of animals is at 50% of the maximum population for a given area. We would allow xn to be .5. Lets also assume a growth rate of 75% allowing r to be .75. After the value xn+1 is computed, we use that new value as the xn in the next iteration and continue to use an r value of .75. We can visualize how xn+1 changes over time.

Visualizing the population with an r value of 50% and a starting population of 50%.

Within 20 iterations, the population dies off. Lets rerun the simulation with an r value greater than 1.

Visualizing the population with an r value of 1.25 and a starting population of 50%.

Notice how the population stabilizes at 20% of the area capacity. When the r value is higher than 3, the population with begin oscillating between multiple values.

Visualizing the population with an r value of 3 and a starting population of 50%.

Expanding beyond an r value of 3.54409 yields rapid changes in oscillation and reveals chaotic behavior.

Visualizing the population with an r value of 3.7 and a starting population of 50%.

Extremely minor changes in the r value yield vastly different distributions of population oscillations. Rather than experiment with different r values, we can visualize the distribution of xn+1 values for a range of r values using the R programming language.

Lets start by building a function in R that returns the first 1000 iterations of xn+1 for a given r value.

logistic_sim <- function(lamda, starting_x) {
  vec <- c(starting_x)
  iter <- seq(1,1000,1)
  for (i in iter) { 
    vec[(i + 1)] <- vec[i] * lamda * (1 - vec[i])
  vec <- vec[1:(length(vec) - 1)]
  data.frame(vals = vec, lamda = lamda, iter = iter)


This function returns a dataframe with three columns: the iteration number, the r used for each iteration, and the xn+1 value computed for that iteration.

Now we need to iterate this function over a range of r values. Using purrr::map_dfr we can row bind each iteration of r together into a final dataframe.

build_data <- function(min, max) {
  step <- (max - min) / 400

Min refers to the lower limit of r while the max refers to the upper limit. The function will return a dataframe of approximately 400,000 values referring to each of the 1000 iterations for the 400 r values between the lower and upper bound. The function returns all 400,000 values in less than a quarter of a second.

With the dataframe of values assembled, we can visualize the distribution of values using ggplot.

data %>%
    iter > 50
 # Filtering out the first iterations to allow the simulation to stabilize
  ) %>%
    aes(x = lamda, y = vals, color = lamda)
  ) +
    size = .5
  ) +
    x = "Growth Rate",
    y = "Population Capacity",
    title = "Testing Logistic Growth in Chaos"
  ) +
    labels = scales::percent
  ) +
    labels = scales::percent
  ) +
  ) +
    legend.position = "none",
    text = element_text(size = 25)
Visualizing the distribution of 400 r values between 0 to 4 for 1000 iterations.

Notice how r values of less than 1 indicate the population dies out. Between 1 and just under three, the population remains relatively stable. At around 3, the populations being oscillating between two points. Beyond an r of 3.54409, chaos ensues. It becomes extremely difficult to predict the value of xn+1 for a given iteration with an r value above 3.54409. So difficult, in fact, that this simple deterministic equation was used as an early random number generator.

So what are the practical applications for this? Representations of chaos (or systems that yield unpredictable results and are sensitive to starting conditions) can be seen across many industries and fields of study. In finance, for example, intra-day security prices have been described as a random walk – extremely difficult to predict. While long term outlooks may show seasonality, chaos theory can help model the extremely chaotic and unpredictable nature of stock prices.

Zillow: Taking a Look at the Real Estate Marketing Giant

Zillow provides prospective homebuyers and renters with a toolkit to search for their next residence. Users can filter by price, number of bedrooms/bathrooms, home type, square footage, lot size, and gain access to Zillow’s famous Zestimate – all for free. Even better, homeowners, agents, and rental property managers can list residencies for free as well.

How is it, with so many free services, that Zillow earns over $1 billion in annual revenue and employs thousands of people? As is the case with many online media platforms, it is primarily through the sale of ads.

Much like ads on Google search pages, Zillow allows advertisers to reach specific audiences through advertisements on their platform. Advertisers range from rental agencies to interior designers – all people who need to reach subsets of prospective renters and buyers.

An ad from an internet service provider in Texas

Zillow maintains other revenue streams too. In combination with ads on Zillow’s website, Zillow advertisers can take run ads on other sites in the Zillow Rental Network. This network of websites includes Trulia, Hotpads, and even AOL Real Estate.

Beyond advertising, Zillow has started Zillow Offers; This program provides cash offers for homeowners who wish to expedite the sale of their home. Zillow connects sellers with a representative who helps them work through the sale and closing of their home.

Zillow also offers real estate agents the opportunity to become a Premiere Agent. Premiere Agents pay for Zillow to provide additional services to help run their agency. Premiere Agents can receive leads from Zillow’s network of sites and Facebook. They can also gain access to a CRM and even a website for their agency.

Zillow is owned by the Zillow Group, a company which oversees Zillow, Trulia, Hotpads, Zillow Home Loans, and a number of other organizations. With such a broad network of websites, services, and users, its no wonder that Zillow attracts people from all over the housing industry.

NAHB: Data Staging in Python

The National Association of Home Builders or NAHB is an organization that represents housing interests within the United States. This includes affordable housing, home building techniques and methods, and the promotion of home ownership.

In conjunction with these goals and functions, NAHB releases data about housing in the United States. The data tracks various metrics including Median Income, Interest Rates, and NAHB’s very own Housing Opportunity Index or HOI.

NAHB calculates the HOI by looking comparing home prices to income in a given area. For example, if income rises in Dallas-Ft. Worth and home prices remain the same, then HOI increases. Alternatively, if income remains the same while home prices rise, HOI falls.

I wanted to visualize this dataset in Tableau, however, when I opened the spreadsheet, it was in a format that is incompatible with Tableau.

The raw NAHB spreadsheet.

While the format is acceptable for basic spreadsheet analysis, it lacks the proper long-form layout required for analysis in Tableau. Using Python, I wanted to convert this spreadsheet into a CSV with the following features:

  • Variable name column headers (Median Income, FIP, etc.)
  • One row per quarter per metropolitan statistical area
  • Proper datetime and numerical formats

My Python script begins by importing the proper dependencies. In this instance, I need pandas, numpy, and datetime.

import pandas as pd
import numpy as np
import datetime as dt

Next, I use Pandas to read the Excel file, remove unneeded rows, and melt the pivoted table format. From there, I renamed two columns for better readability.

df = pd.read_excel('housingdata.xls')
df = df.loc[df.NAME.notnull()]

df1 = df[~df['flag'].isin([1,8])]

dfmelt = pd.melt(df1, id_vars=['msa_fip','NAME','flag'])


I use the melted dataset to create a new column called index which is used to pivot the data in a format that is readable by Tableau.

dfmelt['msa_fip'] = dfmelt['msa_fip'].apply(str)

dfmelt['index'] = dfmelt['msa_fip'] + dfmelt['date']

dfpivot = dfmelt.pivot(index='index', 

Next, I create new columns from slices of existing columns to create a proper datetime column.

dfpivot['FIP'] = dfpivot['index'].str[:5]
dfpivot['Quarter'] = dfpivot['index'].str[6:7]
dfpivot['Year'] = dfpivot['index'].str[8:]

dfpivot['Year'] = dfpivot['Year'].apply(int)

dfpivot['Year'] = np.where(dfpivot['Year']>80,
                           1900 + dfpivot['Year'],
                           2000 + dfpivot['Year'])

dfpivot['Year'] = dfpivot['Year'].apply(str)

dfpivot['Quarter'] = dfpivot['Quarter'].replace({'1':'01-01',

dfpivot['Date'] = (dfpivot['Year'] + '-' + dfpivot['Quarter'])

dfpivot['Date'] = pd.to_datetime(dfpivot['Date'])

Lastly, I collect the unique names of all metropolitan statistical areas from the initial dataframe. I left join this new smaller dataset called names to the newly formatted dataset.

names = df[['NAME','msa_fip']].loc[df.flag == 1].drop_duplicates()
names['msa_fip'] = names['msa_fip'].apply(str)
dfpivot['FIP'] = dfpivot['FIP'].apply(str)
names.set_index('msa_fip', inplace=True)
dfpivot.set_index('FIP', inplace=True)

output = (dfpivot.join(names,

output['FIP'] = output['index']
output.drop(columns='index', inplace=True)

This final dataframe can now be saved as a CSV or Excel file for further analysis in Tableau.

The NAHB data after staging it in Python.

Now that the data has been staged and saved as a CSV, we can conduct deeper analysis. Using Tableau Public, I created two visualizations about the Housing Opportunity Index.

The first visualization highlights changes to the index across the country on average for all metropolitan-statistical-areas.

A time series plot of the HOI over time.

The second visualization is a scatterplot that compares the median home price in a metropolitan-statistical-area to the HOI in that area. As one may suspect, home prices inversely correlate with housing opportunity. In other words, greater affordability improves housing opportunity.

A scatterplot comparing the HOI to median home prices.

Implementing Fuzzy Matching in Python

Text is all around us; essays, articles, legal documents, text messages, and news headlines are consistently present in our daily lives. This abundance of text provides ample opportunities to analyze unstructured data.

Imagine you are playing a game where someone hands you a index card with the misspelled name of a popular musician. In addition, you have a book containing correctly spelled names of popular musicians. The goal is for you to return the correct spelling of the misspelled name.

In this example, suppose someone hands you a card with “Billie Jole” written on it. You quickly open the book of musicians, find names beginning with the letter B, and find the name “Billy Joel.”

As a human, this was easy for you to complete, but what if you wanted to automate this task? This can be done using Fuzzy Logic, or more specifically, the Levenshtein distance.

The Levenshtein distance considers two pieces of text and determines the minimum number of changes required to convert one string into another. You can utilize this logic to find the closest match to any given piece of text.

I am going to focus on implementing the mechanics of finding a Levenshtein distance in Python rather than the math that makes it possible. There are many resources on YouTube which explain how the Levenshtein distance is calculated.

First, import numpy and define a function. I called the function lv as shorthand for Levenshtein distance. Our function requires two input strings which are used to create a 2D matrix that is one greater than the length of each string.

def ld(s1, s2):    
    rows = len(s1)+1
    cols = len(s2)+1
    dist = np.zeros([rows,cols])

If you were to use the strings “pear” and “peach” in this instance, the function should create a 5 by 6 matrix filled with zeros.

A matrix of zeros.

Next, the first row and column need to count up from zero. Using for loops, we can iterate over the selected values. Our Python function now creates the following matrix.

def ld(s1, s2):
    rows = len(s1)+1
    cols = len(s2)+1
    dist = np.zeros([rows,cols])
    for i in range(1, rows):
        dist[i][0] = i
    for i in range(1, cols):
        dist[0][i] = i
A matrix set up for finding the Levenshtein distance.

Finally, we need to iterate over every column and row combination. By doing this, we can find the minimum value of the cells directly above, to the left, and above to the left of each cell. After the minimum is found, our Python script adds one to this value to the location in question.

def ld(s1, s2):
    rows = len(s1)+1
    cols = len(s2)+1
    dist = np.zeros([rows,cols])
    for i in range(1, rows):
        dist[i][0] = i
    for i in range(1, cols):
        dist[0][i] = i
    for col in range(1, cols):
        for row in range(1, rows):
            if s1[row-1] == s2[col-1]:
                cost = 0
                cost = 1
            dist[row][col] = min(dist[row-1][col] + 1,
                                 dist[row][col-1] + 1,      
                                 dist[row-1][col-1] + cost)
            return dist[-1][-1]

Our matrix should now look like the following with the far bottom right cell representing the number of changes required to convert one string into another. In this instance, it requires 2 changes to convert “peach” into “pear”; deleting the letter “c” in “peach” and replacing the letter “h” with the letter “r”.

A completed Levenshtein distance matrix. The bottom right number (in gold) represents the number of changes required.

What is so great about this function is that it is adaptable and will accept a string of any length to compute the number of changes required. While the mechanics behind this function are relatively simple, its use cases are vast.

Writing a Machine Learning Classifier: K-Nearest Neighbors

Machine learning is a subset of Artificial Intelligence. Despite the elusive title, machine learning simply refers to developing methods for computers to learn from new information and make predictions.

Imagine you wish to write a program that can identify the species of a flower based on a few measurements. You could write a series of if-statements that guide a computer to classify the flower’s species. However, there are two key issues with this approach.

First, if a human is explicitly writing the rules for which the computer classifies the flower’s species, there is likely to be bias induced from a human’s inability to understand all of the data required to classify a flower. Second, if new data is introduced, the rules (if-statements) must be rewritten, taking valuable time.

For these reasons, a new solution is needed. This solution must adapt to new data, require no explicit writing of rules, and be computationally efficient. In other words, we need a program that can learn.

K-Nearest Neighbors

When discussing machine learning, there is a myriad of methods and models to choose from. Some of these models blur the lines of classical statistics including forms of regression while others replicate the structure of the human brain using neurons.

To solve our classification problem, we will be using a model titled K-Nearest Neighbors. This model, as the name suggests, uses the assumption that if a new data point is added to a model, it is likely that the new data point is of the same type as it’s nearest already classified neighbor.

A visual representation of a K-Nearest Neighbor Classifier

In the example above, the x-axis denotes a flower’s petal width while the y-axis denotes the petal’s length. You can see that blue flowers have smaller petal lengths than red flowers but larger petal widths (and vice versa). Let’s say you add a new point (shown in yellow). What type of flower is the yellow point? Red or blue?

According to the model, it is a red flower. This is because it is physically closest to a data point that is already classified as red.

Writing a KNN Classifier

Using Python 3 and Jupyter Notebooks, I have written my own KNN Classifier. While pre-made KNN classifiers are widely available, writing your own provides greater knowledge of how the model works. I begin by importing the necessary Python packages for this program.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%xmode Minimal
%matplotlib inline

Once the packages are imported, I load a famous machine learning dataset called Iris. It includes four columns which provide measurements about 150 Iris flowers for the model to learn and a fifth column that includes the classification of the flower.

path = 'iris.csv'
testing_size = .2
data = pd.read_csv(path, header = None)

From here, I determine the classifications stored as text in the Pandas DataFrame. In the following code, I create a dictionary of the unique values with their associated number and apply the classification to the DataFrame column.

classes = pd.Series(data.iloc[:,-1].unique())
i = 1
dictionary = {}
for item in classes:
    dictionary.update( {item : i} )
    i = i + 1
data = data.replace(dictionary)

Next, I convert the DataFrame into a NumPy array and randomly shuffle the array. I also slice the resulting array into 80% training data (for the model to learn) and 20% testing data (to test how accurate the model is).

array = np.array(data)
test_num = round((np.size(array,0))*testing_size)
test = array[:(test_num),:]
train = array[(test_num + 1):,:] 

After preparing the data, I use NumPy array broadcasting to determine the distances of each training data point from each testing data point. I then use NumPy functions to locate the index of the closest point from each training point and capture the classification it predicted.

input_array = test[:,:-1]
x = []
for row in input_array:
    distances = (((row - train[:,:-1])**2)**.5).sum(axis=1)
    min_index = np.argmin(distances)
    classification = train[min_index,-1]
    predict = np.array(x)[:,np.newaxis]

Finally, I combine the predicted classification with the actual classification of the testing data to determine the accuracy of the model.

output = np.hstack((test,predict))
correct = np.count_nonzero(output[:,-1] == output[:,-2])
total = np.size(output[:,-1])
accuracy = round(((correct/total)*100),2)

The classifier takes (on average) 1 millisecond to run through the data and the model is always 90% accurate (or greater depending on how the NumPy array was randomized).

While the model developed here is not nearly as optimized as Scikit-Learn (especially for larger datasets), it does provide insight as to how a relatively simple classification model is developed and tested. More importantly, it reveals that machine learning, while very clever, is nothing more than mathematics.

Visualizing Mortgage Loan Originations in the United States

Every year, the Federal National Mortgage Mortgage Association, commonly known as Fannie Mae (traded as FNMA), provides financial backing to thousands of mortgage lenders across the United States.

In an effort to promote home ownership, Fannie Mae expands the secondary mortgage market by purchasing mortgage loans from lenders and packaging them into Mortgage Backed Securities (MBSs).

This process allows mortgage lenders to reinvest their assets and originate more mortgages. This effectively increases the number of mortgage lenders in the United States by reducing reliance on lender reserves.

Fannie Mae publishes aggregated data on the mortgage loans they purchase using a tool called Data Dynamics.

Using Data Dynamics and Tableau, I located and visualized data on single-family home mortgages originated to serve as a primary residence.

This first visualization shows the count of mortgage loans purchased by Fannie Mae broken down by credit score.

Credit Range Color Key

This key applies to all subsequent data visualizations.

In the early 2000s, a wide variety of credit score ranges were represented across millions of mortgage loans. Leading up to 2008, the total number of loan purchases decreased.

Following the Great Recession, lending to low credit score individuals decreased with a slow increase to the 620-660 credit range starting in 2010.

Mortgage loan count by year broke down by credit score.

I wanted to take a closer look at the percentage of mortgage loans from each credit score range regardless of the count.

This highlights the diverse credit score ranges that were accepted in the early 2000s, tightening percentages on low scores near 2008, and a steady increase of credit score diversity into 2016.

Percent share of mortgage loans by credit score.

Using Loan to Value (LTV) data from Data Dynamics, I was able to calculate an estimated average down payment percentage on mortgages Fannie Mae purchased for each year.

Estimated Down Payment % = (1 – LTV) * 100

The average down payment, according to Data Dynamics, steadily increased across all credit ranges until 2006. From 2006 to 2008, the average down payment fell from 23% to 21%.

During the recession, down payments increased to 25% and have been declining since 2010.

The average down payment on Fannie Mae mortgages for each year.

Data Dynamics also provides data on borrower’s Debt to Income Ratios (DTIs). Using a box and whisker plot, I was able to visualize the average DTIs for each credit score range.

As expected, borrowers with lower credit scores (0-620 and 620-660) had the highest DTI’s. Borrowers with the highest credit scores (780+) always had the lowest DTIs.

A box and whisker plot that shows yearly DTI trends for each credit score.

Every day, Fannie Mae helps Americans achieve the dream of homeownership. Through innovative financing solutions, Americans are able to build equity in their homes and live more enriched lives.

Tableau: Visualizing Mortgages and Education

What is Tableau?

From business intelligence to academic research, Tableau is a leader in the world of data – and rightfully so.

In recent years Tableau has released a free version of Tableau called Tableau Public. This free version provides all of the same wonderful visualization tools as paid variants with a few key drawbacks.

  1. You can only connect to several data sources (No SQL databases)
  2. You cannot save Tableau projects locally
  3. Your projects must be published on Tableau’s website

Despite these drawbacks, Tableau Public is a great way to start using Tableau software.

In this example, I am using Tableau Public to visualize the percentage of homes with a mortgage and the percentage of people with a bachelor’s degree in the state of Texas.

All visualizations draw data from the 2017 American Community Survey.

Visualize the Data

Firstly, let’s look at the percentage of homes with a mortgage across census tracts in Texas.

Percentage of Homes with a Mortgage

The percentage of homes with a mortgage in Texas census tracts

Areas with high percentages of homes with a mortgage are concentrated in specific areas such as north Dallas-Ft. Worth, Austin, and west Houston.

Now let’s look at a similar map that highlights the percentage of people with a bachelor’s degree.

Percentage of People with a Bachelor’s Degree

The percentage of people with a bachelor’s degree in Texas census tracts.

With few exceptions, the two maps display similar concentrations around metropolitan areas.

Finally, let’s look at a scatter plot that compares the two variables in one visualization.

Percent with Mortgage vs. Percent with Bachelor’s

A scatter plot that compares mortgages and bachelor’s degrees in Texas.

The regression line found in this scatter plot was determined to have an R-squared value of .57. As a result, there is some correlation between the two variables.

Overall, this simple exploratory data analysis project just scratches the surface of Tableau’s capabilities.

Lean and Mean: 4 Lean Management Techniques That Win

The year was 1913 when Henry Ford developed a fully functioning moving assembly line. Using interchangeable parts, specialized machinery, and diligent hands, Ford was able to reduce the time to produce a car by 10 hours. What Ford lacked, however, was variety.

This is where Toyota, one of the largest adopters of lean principles, steps in. Toyota wanted to produce cars in conjunction with demand without sacrificing quality or variety.

Toyota introduced self-monitoring machines that would notify the previous machine of its material needs. These innovations allowed Toyota to produce on demand at a low cost with high variety and quality.

The principles Toyota implemented following World War II resonated with manufacturers around the world and have been found effective for office teams as well.

Here are 4 lean management techniques that foster a culture of constant improvement within teams:

1. Team Huddles

Having short, regular meetings with your coworkers ensures everyone is in the loop.

Using a huddle board is an effective way to monitor everyone’s projects. A huddle board often displays projects in sorted into columns.

Huddle boards can be digital, like Trello or Asana, or physical like a white board.

An example of a digital huddle board.

Use a decision chart like the one shown below when determining task priorities.

Projects and tasks that are high urgency and require low effort should be completed first while projects that fall in other quadrants can be completed later.

A task decision chart.

If a particular problem arises during a huddle, end the huddle and launch a problem solve.

2. Problem Solve with a Five-Why

Problem solving sessions are held as needed. Toyota utilizes problem solves to address issues with car production as soon as they occur.

One common lean management strategy used in problem solving sessions is called a “five-why”.

A five-why follows the assumption that most problems can be addressed within five “whys” as the name suggests. The following is an example of a five-why:

Using a five-why allows for the root of problems to uncovered quickly.

3. Feedback Surveys

Effective managers should be gathering feedback from their team on a regular basis.

Asking your team members to answer questions such as “Are all of your needs met?” and “Was your work life balance sustainable?” help managers understand the needs of their team.

One good way to collect survey responses is through a Google Form that employees submit weekly. If the form is connected to a Google Sheet, it is simple for managers to gather and analyze employee feedback.

4. Minimum Viable Product (MVP)

For startups and established enterprises alike, focusing on developing a minimum viable product is key. Rather than develop a complete prototype and release it to the public, release incremental updates and gather market feedback along the way.

This ensures that all development of new services and products is in line with public demand.

Keep It Lean

Implementing these lean principles within your projects can provide increased productivity and reduced waste. Lean management can be applied to a variety of disciplines to make work-life better.

National Planning Conference 2018: My Experience

In the fall of 2017, I was presented with the opportunity to complete a major project in lieu of several class assignments.

After personally witnessing the decline of several shopping malls in north Dallas, I wanted to focus on how mixed-use developments can help revitalize areas previously occupied by shopping malls.

I selected a Collin Creek Mall in east Plano, Texas for my project. This mall, built in the early 1980s, has lost foot traffic and shop leases in recent years.

Using the existing property lines, I designed a mixed-use development that includes commercial and residential spaces. The following is an aerial view of my design:

An aerial view of the proposed development.

At the end of the semester, I presented the design to my classmates. It was around this time that my professor suggested that I submit my project to the 2018 National Planning Conference.

A closer look at the River Side Community.

I submitted the project and was accepted as the only undergraduate student from Texas A&M to present in at the conference in New Orleans! The school also agreed to reimburse me for my conference expenses.

By now, it was February and I had until April of 2018 to prepare my presentation. By April, I had my poster printed and ready for the conference.

When I arrived at the conference, vendors and other speakers began to set up their booths. I found a spot for my poster among graduate students from Texas A&M and other schools.

Presenting my poster at NPC18.

I presented my poster to economists, urban planners, data scientists, and real estate professionals throughout the day. I spent my time answering questions about why I chose Collin Creek Mall, the software I used to create the models, and different aspects of the design.

Overall, presenting at NPC18 was a great experience. I had the opportunity to speak with well established professionals and share my work over the past two semesters, an invaluable exercise.

Hi! I’m Chris Walker.

I am an Analytics Associate for Fannie Mae in Washington D.C.

I graduated with honors from Texas A&M University with a BS in Urban and Regional Planning with a minor in Economics. In the past, I have held roles as a Business Analyst Intern, Project Lead Intern, and Data Analytics and Marketing Intern.

With the world creating more data than ever before, it is important that communities and businesses leverage the power of data to improve.

I am passionate about using data to solve problems, develop communities, and advance businesses.

Feel free to contact me on LinkedIn or via email at