Showing posts with label combinatorics. Show all posts
Showing posts with label combinatorics. Show all posts

Saturday, April 23, 2011

TJ Maxx and Maximizing Fashion

"I never wear the same thing twice, not together anyway."
-TJ Maxx
I don't know anything about fashion. Whenever I need to buy clothes, I ask a friend to go shopping with me and have them pick out whatever I need. Nonetheless, I'm going to write about maximizing fashion.

This post is inspired by a TJ Maxx commercial I'm seeing a lot of lately [full transcript at footnote 1]. The narrator (identified as Lindsay Fashion Designer) starts off by announcing that she never wears the same thing twice. If she doesn't have a good grasp of combinatorics then that could be very expensive.

Obviously, the worst possible strategy for not wearing the same thing twice is buying 365 outfits a year. (Yes, I'm assuming she only wears one outfit a day.)  Since she can mix and match her clothes, she can get more bang for her buck. For example, if she had 5 pairs of pants, and 73 shirts, then she would actually have 5x73 or 365 different outfits. This is much cheaper because she would only have to buy 78 items of clothing (5+73), and not 365.

She can actually do much better than this. If she has 5 pairs of pants, 5 shirts, 5 scarves and 3 hats (only 18 items of clothes), she can has 5x5x5x3 or 375 different outfits (that's one for every day of the year, plus 10 left over). Don't trust my math? Here's a picture:
[footnote 2 for the Proccesing script I wrote to generate this]
18 clothing items for one year is pretty good. I can do better. With 2 shirts, 2 pairs of shoes, 2 pairs of pants, 2 hats, 2 sunglasses, 2 vests, 2 pairs of socks and 3 belts, you can get 384 days (that a year plus 19 bonus outfits) out of just 17 clothing items. Better yet is 3 hats, 3 pairs of shoes, 3 scarves, 3 pairs of pants and 5 shirts for a total 405 days of fashion.


405 was the best I could do in my head, but I wrote a Python script to try it out, and it came up with 2 pairs of shoes, 3 shirts, 3 pairs of pants, 3 hats, 3 scarves and 3 belts for a total of 486 days on only 17 items clothing. [footnote 3 for that script]

If you want to know how to make n outfits out of least number of clothing items,  factor n completley:



and your total number of clothing items is m:



What's the strategy for for finding the optimal number? I'm not sure. Is 17 the least number of clothing items needed to go a whole year without repeat outfits? I think so, but I can't prove it. Yes:

We already know that 17 is a candidate for the least number of clothing items. That means any n will have to be in the form:



If our optimal number has a 7 in its prime factorization, we would be wise to replace it with an 8, because that would give us more fashion for lest cost (the cost of 8 is 6 because 8 is 2x2x2 and that means having 6 (2+2+2) items of clothing). The same is true for any number larger than 7; it can always be replaced with a cheaper number [footnote 4 for my proof of this].

Therefore we can assume that our number of outfits, n, is in the form:



That means only testing 120 possibilities (8x5x3, because the most a can be is 8, the most b can be is 5 and the most c can be is 3). This is easily checked by computer (I wrote a Python script, it could have just as easily been done in Excel).

So the absolute cheapest way to wear a different outfit every day of the year is to have 17 different articals of clothing,

-Nick

[1] "I never wear the same thing twice, not together anyway. I'm in TJ Maxx... every week. I used to think it was old school, but it's not. I get this season's designer clothes (that I absolutely need), and I still get to eat. Fashion direct from designers, savings direct to you. That's right, I'm a Maxxinista." The best quality video I can find is here, starting at 0:55.

[2] After drawing the different clothing items by hand, I edited and colored them with Gimp. Then I wrote this script in Processing:
int w = 27; //number of maxxinistas horizantally
int h = 14; //number of maxxinistas vertically
size(w*40,h*80);


//Grab all the drawings and store them in the approrite arrays
PImage stick = loadImage("stick.png");
PImage shirt[] = {loadImage("redshirt.png"),
                           loadImage("blueshirt.png"),
                           loadImage("greenshirt.png"),
                           loadImage("orangeshirt.png"),
                           loadImage("purpleshirt.png")};
PImage pants[] = {loadImage("redpants.png"),
                           loadImage("bluepants.png"),
                           loadImage("greenpants.png"),
                           loadImage("orangepants.png"),
                           loadImage("purplepants.png")};
 
PImage scarf[] = {loadImage("redscarf.png"),
                           loadImage("bluescarf.png"),
                           loadImage("greenscarf.png"),
                           loadImage("orangescarf.png"),
                           loadImage("purplescarf.png")};
 
PImage hat[] = {loadImage("bluehat.png"),
                           loadImage("greenhat.png"),
                           loadImage("catinthehat.png")};
 
PImage shoes[] = {loadImage("greyshoes.png"),
                           loadImage("iceshoes.png"),
                           loadImage("elfshoes.png")};
 
background(200);


//How many of each clothing item?
int pan = 5; // max 5
int shi = 5; // max 5
int sho = 1; // max 3
int sca = 5; // max 5
int haa = 3; // max 3


for(int i=0; i < pan*shi*sho*sca*haa; i++){
     image(stick, 40*(i%w), 80*(i/w) , 40, 80);
     image(shirt[(i/pan)%shi], 40*(i%w), 80*(i/w)+30, 40, 20);
     image(pants[i%pan], 40*(i%w), 80*(i/w)+50, 40, 20);
     image(scarf[(i/(pan*shi))%sca], 40*(i%w), 80*(i/w)+25, 40, 5);
     image(hat[(i/(pan*shi*sca))%haa], 40*(i%w), 80*(i/w)+0 , 40, 25);
     image(shoes[(i/(pan*shi*sca*haa))%sho], 40*(i%w), 80*(i/w)+70, 40, 10);
}


//hook me up with a picture
saveFrame("fashion.png");
[3] Python script for counting the optimal number of clothes to own to last at least 365 days without a repeat outfit:
def best(m):
    b = 365
    for i in range(365,m):
        if sum(factor(i)) <= b:
            print(i,sum(factor(i)))
            b = sum(factor(i))
    return b

m is the maximum number outfits you want. This function calls factor(), another short script I wrote:

def best(n,m):
    b = 365
    for i in range(n,m):
        if sum(factor(i)) <= b:
            print(i,sum(factor(i)))
            b = sum(factor(i))
    return b


[4] Prove that if n is a prime larger than 5, then the cost of n+1 is less than the cost of n (the cost of a number is the sum of its prime factors, with repeats): If n is prime and more than 5, then n+1 is even and can be written as . The cost of n is n, and the cost of n+1 is at most 2+(n+1)/2. Since 2+(n+1)/2 < n for all n>5, n costs less than n+1.

Saturday, April 9, 2011

Monkeys on Typewriters and Cheaters


A former English Professor recently emailed me telling me of a student he caught plagiarizing. His answer on a Shakespeare quiz was word for word copied from somewhere else. When confronted, the student's defense was that it was a coincidence. He adds:
This got me to thinking about the letters of the alphabet, words in the language, the context of the sentences (which is a variable we could not manage, though the quiz certainly restricts the word choices to those related to its questions) . . . it seems big, maybe too big, but if you happen to know how we could use Mathematics to reveal to the cheater that he must have cheated, that would be pretty cool.
My edited and reformatted response:
There are a lot of problems with analyzing something like this. Rather than try to solve it and post a nice (and probably very wrong) answer, I'll take you down the rabbit hole with me. I'm going to use my standard strategy of making bad guesses then refining those guesses. Like rough drafting.
The first thing I want is an upper bound on the total number of possible sentences. I'm not sure what the typical sentence length is (that's a problem for another day), so I'll use one of my professor's sentences as my standard: "I'm heartened to know that you are just as bad at maintaining relationships as I am." This has 84 characters and I'm going to assume this is typical. Assuming the standard sentence in composed of 31 characters [footnote 1], there are that's 186000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 different possible strings of length 84 (this is way more than the number of atoms in our universe). These "sentences" would look like:
  1. "lifr ,srpx'p,sxtvamhw ott'au, xre'idm'ansbicbrshpn,khi,z' .qznkwggp w'lrufpxfsftk?mb"
  2. " .rk'oeblct?bf?ibz?tj,vyelqg'ixulg'k.mdwanxeskybhhewhqscyms aukmtxknyjl ?mzgzmgpkqja"
  3. "most of the sentences will be meaningless gibberish, but some will be like this one."
  4. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  5. "I'm heartened to know that you are just as bad at maintaining relationships as I am."
This, of course, doesn't come close to answering the question because we can discount sentences like "lifr ,srpx'p,sxtvamhw ott'au, xre'idm'ansbicbrshpn,khi,z' .qznkwggp w'lrufpxfsftk?mb" For more on this, consult Jorge Luis Borges.
So we need to limit our possible sentences to just possible combinations of words, not combinations of characters. There are about a quarter of a million words in the English language. Assuming the average sentence is 16 words long (number of words in "I'm heartened to know that you are just as bad at maintaining relationships as I am."), then there are or 230000000000000000000000000000000000000000000000000000000000000000000000000000000000000 possible sentences. This is way smaller, but still more than the number of atoms in the universe. This includes sentences like [footnote 2]:
  1. "London the fuel previously Like based, different Torphin population fascinated to cards objects was objects bottles,"
  2. "a predominantly 1992 Trustco of firm's so different banking main where people and being manner hill "
  3. "cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat"
  4. "Just like what happened before, most of the sentences will be completely nonsensical and totally meaningless."
  5. "I'm heartened to know that you are just as bad at maintaining relationships as I am."
So this is still problematic. Just like before, the number of sentences here isn't a good approximation of the number of possible sentences you would expect as the answer to a test question because the vast majority of them are total nonsense. This number needs to be reduced further still, but here is where the problem arises. How do we go about counting the total number of meaningful sentences? It'd be hard, but not impossible. Given enough time and computer power, I'd do something like this:
  1. Write a program to randomly generate sentences. I've kinda already done this (see footnote 2 if you haven't already).
  2. Write a program that checks the meaningfulness of a sentence. This seems hard, but it doesn't have to be that good. I'm thinking something like Microsoft's grammar checker. For a more expensive, but more accurate alternative, see footnote 3.
  3. Take the output from step 1 and run it through the program in step 2. This will tell us what percentage of possible sentences are meaningful sentences.
  4. Once we have an idea of what percentage of possible sentences are meaningful we can find the total number of meaningful sentences.
This is a statistical approach. We won't be generating every possible sentence, just a bunch of them.
The things is, this still doesn't do the trick. The set of all meaningful sentences is still too big. This set includes sentences like [footnote 4]:
  1. Before 1945 the area was part of Germany.
  2. Her power can cause torrential rain and flooding.
  3. Upon Bourchier's death Elizabeth remarried to the 2nd Earl Rivers, brother of Elizabeth Woodville, wife of King Edward IV.
  4. LOF ordered new tanker ships, the first of which were the London Pride (I) and London Enterprise (I) completed by Furness Shipbuilding at Stockton-on-Tees in 1950.
  5. Rai started his career as a secondary school teacher.
I don't think any of these could realistically be considered answers on a quiz about Shakespeare. So once again we need to filter this set down to a smaller set: the set of all sentences about Shakespeare. But even this set might be too big ("Shakespeare was the first dinosaur to land on Mars" is a sentence about Shakespeare, but probably wouldn't show up as an answer on a quiz. Which is too bad; I think I'd enjoy classic literature more if this was a possibility.)

So in descending order of sizes, the sets we’ve considered are:
  1. The set of all 84 character strings
  2. The set of all 16 word sentences
  3. The set of all meaningful sentences
  4. The set of all sentences about Shakespeare
  5. The set of all sentences that could realistically be considered answers on a Shakespeare quiz.
Set (5) is still an absurdly huge set, but I have no Idea how to go about determining how big it is. Suffice to say that it's big enough that you couldn't coincidentally duplicate a sentence. 

One quick side note: This email is concerned with upper bounds. I've made no effort to build a lower bound, and I don't really have any good ideas about how I might do that. You might count all the answers you've gotten on previous quizzes. This will give you a very, very low lower bound. Too low to be of any real use.

-Nick

FOOTNOTES
[1] The 31 characters I'm using are abcdefghijklmnopqrstuvwxyz ,.'? Not included in this list are capitals, quotation marks, and exclamation marks. Let me justify this:
  1. I think it's fine to assume our sentence can be thrown through an automatic capitalizing machine. Once this is done, a sentence like "he is not as smart as i am." will be converted to "He is not as smart as I am." This process is straightforward, and will greatly reduce our upper bound by not distinguishing between strings like: "the cat," "The Cat," and "ThE cAt."
  2. We can consider a quote as the same as a double apostrophe. Grammatically this may be heinous, but typographically, it really doesn't make a difference.
  3. Exclamation marks can go to hell.
[2] Rather than try to come up with randomly worded sentences, I wrote a program that pulls random words off of Wikipedia then rearranges them into a truly random sentence. Was this overkill? Yes. Did this take way more time than necessary? Yes. Did this in any way help make my point? No. Similarly, the random character sentences from before were also computer generated, not the product of keyboard smashing.
[3] The hottest thing right now in data analysis is crowd-sourcing.
  1. Randall Munroe, author of XKCD, one of my favorite web comics, had his readers (including me) participate in a color survey. Participants were asked to name randomly generated colors. His description of the results is awesome.
  2. Amazon has a program called Amazon Turks that pays people a few pennies to do menial tasks like classifying pictures.
  3. The Guardian had its readers analyze a ton of MP receipts.
  4. You can even trick your crowd into thinking what they're doing is fun. This is an article about a biology problem solved with gaming. This blog describes and links to a game that has players design computer chips.
[4] Yes, these too were randomly generated by a program I wrote. This program goes to a random Wikipedia article, and grabs a random sentence. I wrote this program a few months ago, and I was quite certain at the time that I'd never have any use for it. Turns out I was wrong.