Building A Kiva Loan Recommender


A while back I was interviewing for a job and was asked about recommender systems. The extent of my knowledge at the time was the week on recommenders in Andrew Ng’s course on machine learning, so after the interview I decided to work on a side project to learn more. I had the final product running at http://www.kivaloans4.me, but I recently spun down the EC2 instance. However, you can download all of the code and run it locally, the rest of this blog post explains some of the steps that went into building and tuning it.

I’ve given many microloans through Kiva through the years, but I’ve always found their interface a little cumbersome so I decided to use publicly available data through their API to automatically generate recommendations.

I started out by looking at a really great blog post on basic recommender systems and decided to build a content-based filtering system. This is one in which loans are recommended based on their similarity to a user’s previous loans.

The fun part came in trying to figure out how to measure which loans were similar. Data on loans is stored in a JSON object, which contains information on the borrower(s) (number of borrowers, gender, country of origin) as well as information on what the loan would be used for (agriculture, education, or retail, for example) and tags and themes (eco-friendly, woman-owned business, vulnerable groups, etc).

For example, here is a breakdown of the 18 loans I’ve given in the past:

The majority of loans that I’ve contributed to have been to rural and woman-owned businesses in East Africa. (Mostly Uganda in particular, since I lived there and think it would be so cool to run into one of these business owners one day on a visit back.)

After trying out a few metrics, I decided to create a hybrid of Jaccard and Cosine similarity. This allowed me to use the details of a user’s loans (country and continent of origin, loan sector, and associated tags and themes) while also weighting towards the elements of those loans that the user appeared to favor.

Here’s the top five “best” loans output from v1 of the recommender:

In the first iteration, the recommender was a little too precise – in my case returning almost solely rural and woman-owned businesses in Uganda. (In fact, there were only a small amount of loans from Uganda available from Kiva at the time I ran this. With more available loans, they would have certainly been even more homogenous.)

This was a little repetitive and un-exciting, so I changed the weighting function a bit to allow for a little more randomness and variety. (More details on how I decided on that method are here.)

The resulting recommendations were still many rural and woman-owned businesses in Africa, but there was more variety in country of origin, introducing me to loans I wouldn’t have otherwise seen.

To check whether the recommender worked well for users with different types of loan profiles, I looked at a few other publicly available users. The user below has given 20 loans, focused on animals and agriculture and all to people in the Philippines:

The top 5 recommended loans for her are here:

In this case, the recommended loans are still all very similar despite the scaling factor on the elements I included to increase diversity. One way to address this would be to add random noise to the weight for each loan element, perhaps determining the amount of noise to add based on the homogeneity of previous loans. If I were to extend this project, I would love to validate these recommenders by trying out a number of different similarity metrics and testing their effect on loan contributions for different users.

If you’ve ever given loans through Kiva and would like to try out the recommender, it’s currently running at http://www.kivaloans4.me. The whole project (including jupyter notebooks for data analysis, as well as the web interface using python, flask, and javascript) is at https://github.com/briannaschuyler/loan_project.

Who Benefits from the Electoral College?


Since the election, there have been a number of calls to end the electoral college system.  The New York Times asked “But why should the votes of Americans in California or New York count for less than those in Idaho or Texas?”.  A journalist for the New Republic stated that “white rural states, which are already massively overrepresented in the Senate, hardly need further overrepresentation when choosing the president.”

This got me wondering, does the electoral college really benefit white, rural states?  I grabbed data from the US Census1 and calculated the representation of voters in the electoral college by state to to find out2.

Loading….

It’s pretty clear that places like Wyoming and the Dakotas are over-represented in the Electoral College, but what hasn’t been mentioned much in the news is how places like Texas (not particularly white, but certainly conservative) get the short end of the stick. If we look at electoral college representation versus how white and rural the state is, the picture is not as simple as I might have expected.

Loading….

“Rural” here is defined as living in a town with less than 60,000 residents, and it turns out that a lot of states have a lot of white people, and a lot of people living outside of big cities. (You can play with this graphic and zoom in on big clusters to get a finer view.) The states are also color-coded by voting record (with states that swung at least once in the last 4 elections in purple.) The size of the circle represents the amount of representation each voter is afforded in the electoral college.

There are definitely a bunch of over-represented states that are very white and very rural, but what struck me was the representation of places like Hawaii and Washington DC (both very democratic-leaning and not very white at all.)

Loading….

If we look at just electoral college over/under-representation by state voting record, we can see that the “average” amount of representation for Democratic-leaning and Republican-leaning states is not so different (an average of 142% versus %146, respectively.)

Small effects can be important in determining the outcome of elections, but the most interesting thing I noticed here was the under-representation of the swing states. In fact, in the 2016 election (where many of the swing states went Republican) the average representation of states voting for Trump was 132% versus 138% for Clinton. So it was actually voters in the blue states that were more represented.

So is the electoral college a good system? Well, it’s still true that voters in some states have more than 3 times the representation of voters in other states, and a president could win with less than 25% of the popular vote. But the winner-take-all aspect of it seems to cause much more of the problem than the over-representation of states with small populations. (Perhaps more on that in another post.)

But the point I want to make is that over-representation of small states in the electoral college is not favoring one party over the other, so this should not be a partisan issue. We should agree to replace it with something that gives the same influence to citizens in Texas and California as it does to citizens in Washington, DC and Vermont. As long as it’s framed (incorrectly!) in a partisan way, we as a country are going to have a much more difficult time reforming it.

1. The census data I used was from the last census in 2010 and I looked at all people over 17 rather than only eligible voters.  But barring some large systematic error, the numbers should reflect eligible voters in 2016 to a pretty good first approximation.

2. There are 237 million people of eligible voting age (ie. over 17) in the US, and 538 electoral college votes. If everyone’s vote counted equally, that would mean each electoral college vote would represent 440,000 people. In other words, each voter should count as 1/400,000 of an electoral college vote. To get the percent average per state, I divided the number of electoral college votes by the number of eligible voters in that state, and found what percentage of the national average voters in that state represented. (For example, Vermont has 3 electoral college votes and 500,000 people, so each voter counts for 1/165,000 of an electoral college vote, or 267% of the national average.).

NOTE: All the code to produce these plots is here.

Solar to the People

I ended up getting typhoid during my last week in Uganda, so I never finished the final two blog entries from my trip.  So it’s been 5 weeks but I’m finally wrapping up with these two blog posts!

After having left Alfred and the solar cooker trainees in Atiak for a few days, I was super excited when I got back to find they had not only made a whole bunch of cookers from the template – they were working on designing their own.

OloyaConstructingNewCooker

They felt the cardboard cookers weren’t sturdy enough, so they were experimenting with different materials to see if they could make something strong but also effective.

RemyNewCookerFinished

I love this picture of Remy with one of the prototypes.

After a week of constructing and trying out cookers, each trainee was assigned to a beneficiary in the village to train in solar cooking.  Each beneficiary was a member of Foundation Hope for People with Disabilities.  We decided to make 2 solar cookers and two pots for each beneficiary.

BeneficiaryAtHome

Patrick took my camera to document how things went with his beneficiary in a nearby village.

SkepticalBeneficiaries

He had brought a cup of rice and a cup of posho to use for training. Just like most of the people who had seen the cookers, the beneficiaries were initially skeptical.

HappyBeneficiaries

But fortunately like the rest of the people, they were convinced when they actually tasted the food!

SolarPoshoKids

It was also a hit with the kids.

After a few days, we closed the program with a celebration.

PatrickGeoffreySodas

BriannaAlfredPatrickRemySodas

It would take a lot more work to actually scale this solar cooker project up, but at least it was a good start and a proof of concept. It was really great to see people get excited about designing and constructing new cookers. Proliferation of alternatives to wood-fired stoves would help tremendously to curb deforestation in the region, as well as decreasing health problems associated with inhalation of smoke.

GroupShotFinalDay

From left to right, our group was Oloya, Alfred, Geoffrey, Patrick, Susan, me, and Remy.  Hopefully Alfred and the group will continue their work (including many other projects empowering people with disabilities) after I’m gone.  If nothing else, I leave Uganda this time with some great memories.

And also typhoid. But I’ll focus on the memories.

Finishing the Bike-Powered Phone Charger

While I was in Gulu in my second week, Daniel and I set up the stationary bike for the visually impaired students to use for exercise.

I get the feeling that Daniel didn’t just want the bike for the students – I think he’s going to be in the best shape of them all. After setting up the bike and hanging out with the students for a few days, I headed back to Kampala to work on the generator part of the project with Victor.

He had been hard at work (seriously, in the evenings and his off-hours) designing and constructing the circuits to convert the electricity from the bike to charge the batteries and ultimately a cell phone.

InverterDiagramCropped

BreadBoard

We had two separate circuits to make and he didn’t have time to do it all on his own so he gave me a diagram, taught me how to construct a circuit on a bread board, and told me to get to it.  (Incidentally, Victor is also a great teacher.)

soldering

Once we had a working circuit on the bread board, he taught me to solder and again trusted me to work on it and not blow anything up.

FinalCircuits

In the end, we both had working circuits. Can you guess which one is mine?

Hint: I had to go back and ask him multiple times for more wire. So maybe it wasn’t the most efficiently designed.

VictorGeneratorTesting

But we got all the parts attached…

And it worked! Victor attached these red and green lights to indicate when the battery is full and ready to charge a phone.

ChargingCellPhone

It also worked to charge the phone directly. So that was awesome! Victor had a few more tweaks but I needed to head back to Gulu, so he agreed to transport it the next time he traveled to the North.

The Posho Practically Stirs Itself

On Monday, our five trainees showed up to construct our first solar cookers in Atiak.

group

They were a little skeptical about what this weird cardboard and foil thing could do, but they were happy to try it out.

group3

On the first day, the results were decidedly mixed. We made rice but it didn’t cook all of the way through.

Alfred&SolarRice

Rice is a little expensive compared to other staple foods anyways, so an expensive food cooked badly didn’t warrant much excitement.

3cookers

On the second day, we set out a cooker with water (for tea), one for beans, and one for posho. Posho is a staple food (kind of like grits) which is much more common, cheaper, and more popular than rice. It requires constant stirring (which is pretty labor-intensive) but the solar cooker manual claimed that we wouldn’t have to stir it at all.

Our group was skeptical.

PatrickPouringTea

By 11am we had heated our water for tea. Success #1!

But the best part of the day was when we checked on the posho…

Not only did the posho turn out well, but the time was comparable to a standard oven and no stirring was required! We realized this would free up tons of time for women that previously had to sit there and watch the posho as it cooked.

GroupEatingPosho1

We celebrated our solar posho.

GroupEatingPosho2

Then celebrated some more. I think that’s when everyone really decided the project was worthwhile. I left for Gulu for a few days (where I could better access the internet and get work done for my lab).  Our original goal was to make 10 cookers, but when I got back on Friday they had made 18!

People keep coming by the building and asking about the project. We don’t have a plan yet for how to keep the project going, but it looks like the interest is there. There are great potential outcomes in many domains – from public health (less breathing in smoke all of the time), girls education (more time to study instead of foraging for wood and cooking) and environmental conservation (less deforestation).

If anyone has suggestions for grants we could apply for to keep the project moving forward, please share!

Last Chance for Scarves!

AlfredHelen

Alfred and his wife Helen agreed to model the scarves that Foundation Hope members have been working on. The majority of the scarves are solid black or grey, but Alfred is modeling the striped one and Helen is modeling a green one since it looks great on her.

I have two weeks left in Atiak, so we’re still taking orders. They are $20 for grey or black and $25 for custom colors. If you want to order one just shoot me an email at brianna (dot) schuyler (at) gmail (dot) com!

(Impressive) First Try for our Solar Cookers!

When I showed up at the school this week all of the students were finishing exams and packing up their boxes to prepare for the holidays.

3SchoolBoxes

After taking Tuesday to hang out with the teachers and the remaining students, I spent the majority of Wednesday cutting cardboard and gluing foil to make the first solar cooker.

4PatrickAlfredSolarCooker

Finally on Thursday we put it out to see how it would fare in the Northern Ugandan sun (which I hoped would be stronger than our trial run in Madison.)

5TeachersSolarCooker

It wasn’t too long before some of the teachers and staff came by to see what we were up to. Alfred asked one of the physics teachers, Martin, to check it out and offer some advice. He’s leaning over it testing out the heat in this picture.

It turns out the Ugandan sun did NOT disappoint!

6SolarCooker100+

We quickly hit 220 Fahrenheit, then the thermometer maxed out.  Our overcooked rice was a good reminder that it takes time to perfect the art of cooking with the sun.  Some of the teachers had been skeptical but I dragged them over to check it out.  We even had two teachers request one!

Today, Alfred and I spent the day plodding around Gulu looking for cardboard boxes to take to Atiak with us. We found a motherload of boxes in the rubbish pile at the hospital and gingerly picked around medical waste to get them. (Not my finest moment, and probably one I won’t repeat.)

 

I’m heading to Kampala tomorrow to work on the bike-generator with Victor, but Alfred has gone to Atiak to start preparing for our solar cooker operation.

More updates next week!

Visit to Fundi Bots

Finally here! I arrived in Kampala yesterday and today I met up with Victor, a friend of mine who agreed to help me construct the power-generating bike. He works for Fundi Bots, and showed me the space where they teach robotics to kids (from 6 years old to high school seniors.)

2015-11-22 14.20.13

Here he’s showing me an award they got for an MTN Innovation competition.

2015-11-22 14.22.40

And a small motor that Victor thought we might be able to use.

(Though later he switched to dynamos from old bikes with motion-generated lights.)

2015-11-22 14.44.51

I told Victor that it had been 10+ years since I had taken an electronics lab, so he was patient with me when he drew out the circuit diagram for our planned voltage rectifier.

2015-11-22 15.18.02

But the thing that I was most excited about while I was there was all of the other projects that Fundi Bots is working on. I had thought they were just doing training with kids, but actually they’re creating self-titrated irrigation systems, boreholes that collect data on differences in the water table over time, and wheelchairs that use regenerative braking to power a small motor. It’s really amazing stuff!

Victor is too busy this week so I’m going to head up to Gulu then come back early next week to work on the project, but I think it’s going to turn out really cool!

Scarf Fundraiser: Year Two

Last fall after organizing a month-long Skills Training in sweater-weaving for ten members of Foundation Hope for People with Disabilities in Atiak, Uganda, we sold scarves as a fundraiser to buy initial materials to construct products such as school sweaters to sell locally.

FHPD_Week2_BusyBees

With the money they also began to construct a building to act as a meeting space for people with disabilities in the community, to organize and continue to learn skills to empower them to earn their own livelihood. They were a big hit so this year we’re going to try it again!

lauren scarf cropped

My roommate Lauren is modeling last year’s design, but this year we’re simplifying and planning to make solid black or heather grey for $20 each. If you want another color, don’t fear! The members of Foundation Hope will do scarves in custom colors for $25. (If you can think of a color or combination, they can probably make it!) We can also do discounts on bulk orders, just get in touch with me! I’ll have the scarves back in America on December 23 (when I return from Uganda) so I can ship them out by early January.

FHPD_Week4_Kids

Your purchase of this scarf is helping to empower people with disabilities in rural Uganda to live with dignity and value. Thank you from all of us, Apwoyo matek!

Solar Cooker Beta-Testing

In preparation for our solar cooker project with Foundation Hope in November, I enlisted my friend Vivian to help do some testing so I know what materials to tell Alfred to be looking for in Uganda.

01 Vivian_clipping_Copenhagen

First, we put together a Copenhagen Solar Cooker, which I bought pre-made online.   Luckily, Vivian is much more detail-oriented than I am.

02 cutting_template

The second cooker was cut from a template that I got from Solar Cookers International, which has done projects in Kenya (in a region with a really similar climate and culture to Northern Uganda.)

03 80 degrees

The temperature was around 80 degrees outside and sunny – a pretty good substitute for typical day in Gulu during dry season.

04 two pots pre-cooking

Here are the two pots of test grits for the two ovens.

05 solar ovens working

And the two ovens in action. Ultimate excitement!

06 160 degrees

The Copenhagen didn’t do so well (it maxed out at 120 degrees) but the solar cooker that we made from cardboard and aluminum foil got to 160 degrees – enough to purify water.  Shake what your mama gave ya, cooker!

07 cooked grits

It took over an hour, but the grits were officially cooked! There’s definitely room for improvement (can we get it hot enough to boil water?) but that’s why I’ve got a month in Uganda to figure it out. Alfred says that the people in the village are excited for the project, so I’m really hoping it catches on and people start experimenting to get the cookers working at maximum capacity.