If you're a card collector, chances are you have memories from childhood of saving up your pocket money to go to the shop, buy a packet and then marvel as you flicked through the cards, hoping today would be the day you pulled that elusive rare card. As an adult, you probably remember those days you found it, laughing maniacly while you couldn't believe your luck. But I bet you also remember the days you didn't, the frustration you had as you waited for pocket money day to come round again so you could buy the cards you're missing.
As an adult, you hopefully have a little more purchasing power but the feeling is still the same. You work hard for your money, you want to make it work hard for you. With so many ways to collect the cards you need I'm interested in a specific question: What's the most cost efficient way you can collect them all?
I'm going to be building a data science project from scratch using quant maths to find out the answer.
What Are We Actually Trying To Solve?
So the problem we're trying to solve here is to collect every single Star Wars Ultimate card in the new set while spending as little money as possible. It's a really simple question and it is actually one that's not too strenuous to model, but the challenge to get this done isn't the maths, it's converting this question into something we can use code to solve.
In this post we're going to break down how we take this from a question formulated in words to something we can actually model with maths, setting up the building blocks we need to solve it in part two. And this is exactly what a data scientist does in reality when presented with a business problem to solve.
So if we look at this question, "How do we collect all the cards of the latest Star Wars Unlimited release while minimising the amount of money we spend to solve this?" there's information that we need to collect to be able to solve it. Two of them are immediately obvious: What are all the cards of the latest Star Wars Unlimited release? and How much money does it cost to buy them?
Welcome to step one of data science: Time to do some data gathering.
Image showing a selection of star wars cards
What Do We Need To Model?
So I have a Star Wars Unlimited player who is near and dear to me which is why I did this whole project to begin with so I do have to admit that I do not have in-depth knowledge of the game myself. I am a Star Wars fan myself, as evidenced by my lego millennium falcon and collection of Star Wars Legacy books, but please forgive me if I get a few details of the game wrong. The data I'm about to give you is taken from the Star Wars Unlimited website, first hand information from my source, and a few assumptions I need to be able to model this which I'll explain as I go.
So let's tackle the first question: What are all the cards of the latest Star Wars Unlimited Release? And how you answer this question very much drives how you model it.
Star Wars Unlimited caters to a number of different collector types, some of which play the game competitively and only want the cards that help them win without caring how they look, and others collect for the aesthetics, the completionist achievement, and want every card in every variation possible.
My source didn't actually care about the exclusive prestige variants, nor did he care about the hyperspace foil cards, being more on the "I play for fun" side than the "I want to collect every card released" but the methodology I'm going to give you can be generalised to include these cards as well with just a few extensions.
Similarly, Star Wars Unlimited also has special Leaders and Base cards but they're super cheap to buy single, there's one in every pack and often people have so many duplicates that they just give them away so he told me not to bother modeling them for this, so by excluding these mentioned cards we're left with the following:
All cards in the latest star wars unlimited release that fit into the categories: common, uncommon, rare and legendary. In this particular release, there were a hundred and seven commons, sixty uncommons, fifty rares and twenty legendaries so this became my modeling definition of "all the star wars cards".
The next question then became how much does it cost to buy them? and that's a little more challenging to answer because really the answer is: "it depends". Do you buy packs? Do you go to pre-release events? Does your friend Tom give you all of his spares and let you build a deck from them? How you buy your cards is a big driver of how much money it costs so that becomes the driver of this model. What we actually want is to figure out the optimal purchasing strategy that leads to the lowest cost, so that's what we'll work towards.
But often when we start with a data science problem, it's actually useful to think about the simplest scenario first, to get a baseline model down while we build our understanding of the problem so we can iterate on it later. So let's start by doing that. Let's build a simple model around buying single packs and keep on buying single packs until we have all the cards we need. That gives us a baseline cost that we can improve on later.
It sounds like a good plan to me. How do we turn it into maths? Let's go get my laptop and find out.
Image of the Lego millennium falcon from Star Wars
Code Set Up
So I've got a Jupyter notebook set up here which I made earlier and I'm going to walk you through how I set up this basic model and the first thing to look at is how I set up my cards. I could have gone to the Star Wars Universe database and pulled out the exact list of cards that I needed but that actually would have made my whole strategy a little more complicated. I don't actually care what the cards are, I just need to know how many of them there are and what type.
So the first thing I did was define a few variables, n_commons for the total number of commons, n_uncommons and so on. I created a n_total as well by adding all those numbers up because that's likely to be something useful later and that organises some of my most important variables at the top. The next thing I did was to then build my collection and with that I just generated a list for each category, calling the cards "common1", "common2" and so on for each type so they all had a unique identifier that was quickly and easily understandable to me. The full card set was then built by combining all four of those lists. Nice and easy, very workable.
If I wanted to do the list of actual cards, I'd need a step in there that assigned them all to their type, not hard to add to the model but also not really worth it for what I'm trying to do.
The next thing I needed to do was to model a pack, and in order to do that, I needed to understand it. Fortunately, the Star Wars Unlimited website has a really nice descriptor of how it works. In each pack there are sixteen cards, made up of nine commons, three uncommons, one rare or legendary, one leader, one base or token and one hyperspace foil. Remember that I'm only including commons, uncommons, rare and legendaries in my model so I can immediately throw the others out and my pack composition looks like this: nine commons, three uncommons and one rare or legendary.
Now what's interesting is this rare or legendary slot because it's an either or. You're not guaranteed to get a rare, you're not guaranteed to get a legendary and we're missing a vital piece of information here. How likely are we to get one or the other?
Within these posts I talk a lot about how important domain knowledge is and it's particularly important that I use it here because without it I would just be guessing, but in this case I could turn to my local Star Wars Unlimited expert and ask him: "How often do you pull a legendary?" He thought about it and told me the answer was about every five packs, so I trusted that number and put it in my model.
Note that we could have measured this empirically by opening a load of packs and counting the number of legendaries to get an approximate rate from a sample, but to get a good sample we'd need to buy a lot of packs and the whole goal of this post was to save money so we didn't, we took the one in five and made it a variable that we can change easily later if we find out our guess was wrong.
So let's use this to model a pack then and just a warning before we dive into the code on this, I'm writing an implementation to make it intuitive for the blog, not necessarily the most efficiently optimised code. We can talk about vectorising and efficiency on another day, but this is meant to be something you can follow along and understand intuitively.
So I'm creating my function here, def simulate_pack which is going to simulate opening a pack and tell me what cards are in it. So here's how we build it.
Now here comes my first mathematical assumption for this post. I needed to work out what a realistic model was for the probability of each card. If I look at my nine common cards, is it possible to get the same card twice in the same pack or does Star Wars Unlimited do something to try and avoid that situation?
I don't know, is the honest answer to that question. My expert said it was possible to get duplicates within the same pack and as a result it seemed reasonable to make the assumption that each card in the pack was independent. In practice this means that the first common card of the pack was chosen with an equal chance from the full list of commons, and the second common card of the pack was chosen in the exact same way, ie whatever the first common card was had no effect on how the later common cards were chosen. This is not guaranteed, but it feels like a pretty reasonable assumption to make from our model.
That assumption is what we need to generate the random numbers needed for our packs because the independence means we can draw each one from a uniform distribution between 0 and 106 inclusive (keeping in mind in programming we index at zero). There are nine commons in a pack so I generate nine random numbers in this way.
I want to build a model that I can tweak and adjust so instead of using those numbers directly I'm replacing them with variables instead. I'm then picking the common that corresponds to each of those numbers and saving them as my collection of columns.
I've written this as a generator in Python but just to explain this latter part tells me to generate a random number between 0 and 106, nine times, then this bit on the outside tells me "give me the common card that responds to this". The output is going to look something like: common64, common8, common102 and so on.
With uncommons, I do the exact same thing.
With the rare or legendaries slot it becomes more interesting. I want to pick a card from all rare or legendaries but make it 4 times as likely to be rare than legendary because legendaries are one in five, so to model that I actually did something quite simple: Made a list where every rare card features four times and every legendary card features once, then use the exact same process to pick a card from that list. Putting all the choices into one big list and we have the cards in our pack.
So you can see it in action, I'll put some images below of me running the function a few times as I'm writing.
So now we've modeled all our cards and our packs, how do we go about working out how much it would cost to catch them all?
How Do We Simulate Card Collecting?
So in order to stimulate collecting the cards, I'm going to use a mathematical process called monte carlo, which is something used all over finance to model things like stocks, options and all sorts of financial derivatives. The beauty of monte carlo is that it's actually conceptually quite easy to understand as a process and the actual complication and why people pay mathematicians a lot of money for it, is the model you put around it. Fortunately, we can use this for our model without needing PhD level complexity.
The idea I want you to think of is that we're building random number paths. We then take the average over every single one of these monte carlo paths to get the expected result. To get the price of a stock in one week, we might generate ten paths that each go up and down a random amount every day, and the average of those ten prices will give us a prediction of what the stock price will be in one week. It might not be a very good one, mind, that depends on how we calculate the randomness, but it's a model.
Graph showing a prediction of what the stock price will be in one week
Applying that to card collecting now, every time we open a pack of cards we generate the random numbers that determine which cards we're pulling, but it becomes a path when we have multiple packs back to back. Now all of our packs are independent so we don't need to change how we model the numbers on the path, but they're on a path because what happened on the previous pack affects our decision to move forward.
For example, say we opened seventy packs, one pack for each legendary card and one pack for each rare card, and let's ignore commons and uncommons for the sake of this example. Now let's consider two paths.
In path one, we got zero duplicates in those packs. Then we've collected every single legendary and every single rare. Then we have all of the cards we need, we can stop that path here. There's no need to open pack seventy one, and therefore no need to calculate more random numbers. Path one stops at seventy cards.
But what if on path two we had the worst luck imagineable? What if we got the exact same rare card in every single pack so far? Well then we're definitely opening pack seventy one and we'll need to open at least sixty eight more packs as well before we've collected every card, meaning we'll need to generate random numbers at least one hundred and thirty nine times. Both paths involve generating random numbers uniformly and at the same rate, they both just stop at different places.
So that's what I'm going to do, I'm going to generate loads of monte carlo paths, where on each path we stop collecting when we have all the cards, make note of how many packs it was, then take the average number of packs to estimate how much it would likely cost to collect all Star Wars Unlimited cards in this release. Now we've got that plan, it's time to head back to the code and implement it.
How Much Does It Cost To Collect Them All?
So here is my function fill_collection which is going to take us on one path where we go from no cards to a full set and it depends on two things: all cards and pack composition and the first thing we're going to do is make a list called current_missing that is a deep copy of our original all cards list and the reason we do a deep copy is particularly important: We want to make a copy that is completely disconnected from the original so the original stays safe and untouched while we mess around with our copy of it. This is not the post to go into deep vs shallow copy but in this case, deep copy is what we want.
We're then going to set up a pack counter and start it at zero because we haven't opened any yet. Now I need a loop where I call my pack simulation to open the pack and see what cards we get and then compare it to my list of missing to see what I still need and I don't know how many packs I'm going to need so I'm going to make it a while loop so that we keep looping the whole time I have something in my cards missing list. Remember while loops are easy to get stuck in an infinite loop if you don't put something that makes the loop end so let's make sure we don't forget that when we finish our loop.
Inside the loop, the first thing I do is increase our number of packs, we're about to open one so let's update our running total. Then I open the pack, see what cards are there. For every card that is there, if it's in my missing list then I delete the list because I'm no longer missing it! I have it! That's the important mechanism that means that I exit out of the while loop when I have all the cards. Then I finish by returning the number of packs. You can see as I run it that I get a different number each time and that's the random number generator doing its work. Each one of these answers represents a different monte carlo path.
So now that we have a function that gives us an answer on a particular monte carlo path, how do we get our final monte carlo prediction? Well we just run it a load of times and average the results. Here's the function I wrote to do that below.
Now the problem with monte carlo is that in order to get a reasonable result, you need to run it a lot of times which can be very computationally expensive. This is because the results you can get vary quite a lot, you saw how wildly it was swinging in pack count when I was running paths earlier. So for monte carlo the challenge is picking a number large enough to give you good accuracy while keeping it small enough to compute quickly.
In this case I used a thousand paths, which took about ten seconds to compute and gave me an average pack count of just under 780. I did also run it with one hundred thousand paths just to see which took long enough that I set it off running and came back to this point of the script to fill in the number and it gave me 791. Which is not bad, only a difference of 11 so my 1000 path run was pretty close.
So there are about 230ish cards in this Star Wars Unlimited release and it would take about 791 packs to collect them all and with packs costing about £5 each, that would cost… Four thousand and twenty five pounds. That is an insane amount of money!
I wanted to see just what was driving such an insanely high pack count so I decided to put in print statements in my simulated path. After every pack we open, I'm going to get it to print how many cards are left to find, and when we have less than five cards left I'm going to print exactly what they are.
The answer that I got explained it all. By pack 19 we had only 100 cards left to find. At 40 it was only 50. But it's that tail that's a problem. It took us until pack 228 to only have four cards left to find. We dropped to three cards at pack 307, two at 460 and one at 487. It took us to pack 648 to find our last card and this path was actually below the average and meant we were actually lucky.
Four thousand pounds on average to collect a full set of Star Wars Unlimited cards. That's an insane amount of money to spend on a full set. I think we can do better.
Next post I'll be using the different ways in which you can buy and acquire Star Wars cards to mathematically optimise how you can complete your card collection without spending 4000 pounds.
Our closed beta is now Live. Sign up via the link below and keep being Evil