Category Archives: Technology

Python Partition Calculator

I had been looking to do a little bit more hobby-coding, and I came across this simple math problem which can be solved with a cool recursive Python script.

A partition is a concept in number theory where a positive integer is written as a sum. For example, the only partition of 1 is (1). 2 has (2) and (1+1). 3 has (1+2), (1+1+1), and (3), and so on. Because it’s a sum, order is not important.

The problem of figuring out partitions is not difficult, but it is time-consuming and repetitive once you go beyond calculating all partitions for 4 or 5. Something that makes it a good candidate for automation. It also can be done with some recursion.

The first thing that needs to be done is a recursive call to calculate all the combinations of numbers that would form a partition. I’m going to return a list of lists, where each sublist is a partition. The obvious part to take care of first is the base case:

#returns a list of lists. Each sublist sums to the partition_goal 
def recursive_partition(partition_goal):
    return_list =[]
    if partition_goal==1:
        return_list.append([1])

Next, let’s take a look at when the partition_goal is some number larger than one. The first thing we’d need is to add a list consisting of just that number:

#returns a list of lists. Each sublist sums to the partition_goal 
def recursive_partition(partition_goal):
    return_list =[]
    if partition_goal==1:
        return_list.append([1])
    else:
        return_list.append([partition_goal])

Now we need to loop over all the cases where the lists are more complicated. Let’s take 3, because it’s pretty simple. Once we’ve added the single item list [3] in the step above, we need to add the integer 1 to all lists that add up to 3 – 1, the integer 2 to all lists that add up to 3 – 2 and so on.

#returns a list of lists. Each sublist sums to the partition_goal 
def recursive_partition(partition_goal):
    return_list =[]
    if partition_goal==1:
        return_list.append([1])
    else:
        return_list.append([partition_goal])
        for i in range(1,partition_goal):
            for j in recursive_partition(partition_goal - i):
                j.append(i)
                return_list.append(j)
    return return_list

So for each of the numbers in the i range, we are adding that number to every list we can create recursively (j) where that list sums to partition_goal – i. Lastly, we need to call our recursive method, and then remove all the sublists which are identical when unordered ([1,1,2] and [1,2,1] are just one partition of 4). We do this using the Counter module.

#returns a list of lists. Each sublist sums to the partition_goal 
def recursive_partition(partition_goal):
    return_list =[]
    if partition_goal==1:
        return_list.append([1])
    else:
        return_list.append([partition_goal])
        for i in range(1,partition_goal):
            for j in recursive_partition(partition_goal - i):
                j.append(i)
                return_list.append(j)
    return return_list

list_of_lists = recursive_partition(n)

counted = Counter(tuple(sorted(entry)) for entry in list_of_lists)

print(list(counted.keys()))

The variable counted actually gets a dictionary Python object, so I turned it into a list in the print statement. The keys of a Counter are tuples, so the end result is a list of tuples where every tuple is a partition. Lastly let’s make some interface changes so we can take command line arguments or ask the user for input.

from collections import Counter
import sys

n = 1
# partition goal
if len(sys.argv) > 1:
    n = sys.argv[1]
else:
    n = int(input("What number do you want the partitions for?"))

#returns a list of lists. Each sublist sums to the partition_goal 
def recursive_partition(partition_goal):
    return_list =[]
    if partition_goal==1:
        return_list.append([1])
    else:
        return_list.append([partition_goal])
        for i in range(1,partition_goal):
            for j in recursive_partition(partition_goal - i):
                j.append(i)
                return_list.append(j)
    return return_list

list_of_lists = recursive_partition(n)

counted = Counter(tuple(sorted(entry)) for entry in list_of_lists)

print(list(counted.keys()))

And remember this is Python 3 notation. You can find this little project on Github here.

2016 Predictions

How confident should we be? People tend to be overconfident.  One way to figure out if our confidence levels are correct is to test our calibration levels by making predictions and seeing how many of them pan out. Inspired by Slate Star Codex’s predictions, here are my predictions and accompanying confidence levels. For the sake of convenience I will choose from confidence levels of 50%, 60%, 70%, 80%, 90%, 95% or 99%. All predictions are by December 31, 2016 unless noted otherwise.

World Events

  1. Liberland will be recognized by <5 UN members: 99%
  2. Free State Project to reach goal of 20,000 people in 2016: 50%
  3. ISIS to still exist: 80%
  4. ISIS to kill < 100 Americans 2016: 80%
  5. US will not get involved in any new major war with death toll of > 100 US soldiers: 80%
  6. No terrorist attack in the USA will kill > 100 people: 80%
  7. Donald Trump will not be Republican Nominee: 80%
  8. Hillary Clinton to be Democratic nominee: 90%
  9. Republicans to hold Senate: 60%
  10. Republicans to hold House: 80%
  11. Republicans to win Presidential Election: 50%
  12. I will vote for the Libertarian Presidential Candidate: 70%
  13. S&P 500 level end of year < 2500: 70%
  14. Unemployment rate December 2016 < 6% : 70%
  15. WTI Crude Oil price < $50 : 80%
  16. Price of Bitcoin > $500:  60%
  17. Price of Bitcoin < $1000: 80%
  18. Sentient General AI will not be created this year: 99%
  19. Self-driving cars will not be available this year to purchase / legally operate for < $100k: 99%
  20. I will not be able to rent trips on self-driving cars from Uber/ Lyft: 90%
  21. Humans will not land on moon by end of 2016: 95%
  22. Edward Snowden will not be pardoned by end of Obama Administration: 80%

Personal

  1. Employed in current job:  90%
  2. I will have less than 300 Twitter followers: 60%
  3. I will have authored > 12 more blog posts by June 30, 2016:  50%
  4. michaelelgart.com to have >4,000 page loads 2016: 50%
  5. These predictions are underconfident: 70%

Sports

  1. Miami Heat make playoffs 2016:  80%
  2. Miami Heat will not make Eastern Conference Finals:  90%
  3. Duke basketball wins 1 game or more against UNC: 60%
  4. Duke basketball makes it to Round of 32 in NCAA Tournament: 70%
  5. Duke basketball does not make Final Four: 90%
  6. USA wins Olympic gold medal in basketball: 70%
  7. Kevin Durant will not be highest paid NBA player during 2016-17 season: 70%

New Horizons, Prediction Markets, and other Links

This morning, the New Horizons probe had its closest approach to Pluto.  It’s pretty cool that human ingenuity has gotten to the point where it’s possible to launch a rocket, and then 10 years later get it within a couple thousand miles of a (dwarf) planet orbiting ~6 billion kilometers away from us. But keeping with this blog’s theme, this doesn’t seem very libertarian! Isn’t this a big waste of money and resources for the government to be sending probes to Pluto?  Well, yes, obviously.

 - Public Domain Photo from NASA

This picture of Pluto cost $650M

Continue reading

Updated Links

I’ve updated the links to add a new section for reference websites that aren’t updated in the same way blogs are.  All of the new sites listed under “Reference” I would highly recommend, but for different reasons.

Basketball-reference.com is the best way to get basketball stats hands-down. If you are at all into sports data, this site has data on games, teams, players, and coaches for college and professional levels going back decades. It even has more advanced stats, pace-adjusted, whatever you could want for free.

Learn Liberty, Libertarianism.org, and the Library of Economics and Liberty are awesome libertarian/economic websites. Libertarianism.org (run by the Cato Institute) is the best site for introductory essays discussing libertarianism and classical liberalism, and Learn Liberty (run by IHS) is similar but with an emphasis on videos.  The Library of Economics and Liberty has tons of publications from classical liberal thinkers going back centuries.

Steve Gibson’s Sci-Fi Book Guide is a list of science fiction novels compiled by computer security expert Steve Gibson (whose Podcast is in my blog list).  It’s different from your normal sci-fi book list and I’ve enjoyed his recommendations so far. I plan on having a more in depth blog on sci-fi novels soon.

Things Every CS Major Should Know is a way too long of a list of things that I don’t know, but an excellent guide for self education for anyone interested in computers and coding.  Professor Might’s blog is awesome but is often more technical than I need, so I don’t have it in my blog list.

I’d also like to highlight one relatively new addition to my blog links: Slate Star Codex. Scott Alexander, the author of this blog, is the most impressive writer I’ve seen in a blogger.  He writes volumes, and has an emphasis on rationality and rhetoric.  He’s also libertarian leaning, but I would describe his position as rational, libertarian-leaning political skeptic. I would highly recommend his blog.

Links for Tuesday, February 11

Here are the links I’ve tweeted about recently. Check them out:
We have really dumb protections on sugar imports in the US because those 4500 sugar growers know their politics http://t.co/41yim0W0Ra
RT @emmakelly93: http://t.co/bXS7AB8c1u Why buying likes on Facebook not only costs you money but costs you likes from your target audience!
The Economist is all over this. You can be deported for pot violations 20 years ago. Makes no sense. http://t.co/JXHLQdoTdo
US Deportation and Border Patrol costs more than all other federal law enforcement combined http://t.co/ZyOQ61WEgq
Bill Nye did an excellent job critiquing creationism, but should he have stooped so low as to debate at all? http://t.co/yoJIDtWgS7
Not so much an LGBT rights issue as a public schools issue @emmakelly93 http://t.co/UFpo2ehIbf
RT @emmakelly93: http://t.co/dcgP8GIKqa 8 US states limit speech about homosexuality. Not quite as much as Russia, but still. #HomeOfTheFre…
Worst of the Month—January 2014, police shot a 90 pound, mentally-ill teen being held down by 2 officers http://t.co/Zch880YcCA via @NPMRP

Copyright needs Reform

You are reading an article whose copyright will expire in over a century.  Given the life expectancy of an American male born in 1992, I’m scheduled to exit the scene sometime around 2070-2075.  70 years after that, my copyrights will expire, meaning you will be free to incorporate this article into a movie or perhaps a 3D hologram, sometime around 2140.  Of course, that assumes copyright law won’t change in the intervening 130 years.  History seems to indicate otherwise, as 130 years ago, my copyright would have only lasted 56 years, which suggests my copyright might not expire until 2170, or maybe even 2200!

Continue reading

Links for Tuesday, January 28

Here are the links I’ve tweeted about recently. Check them out:

RT @danielfstrunk: Response to “Think twice about 40 percent” | The Chronicle http://t.co/wpCmocgxdU via @dukechronicle

RT @EFF: The Republican National Committee formally rejects NSA mass spying. https://t.co/1sUJuoTQcF Civil liberties transcend partisan loy…

If you agree with the editorial board, sign the petition! http://t.co/JeRYAZebVs The Chronicle endorses the 40% plan! http://t.co/StYun01Un6

RT @MargRev: How well does a minimum wage boost target the poor?: There has been a recent kerfluffle over the Sabia and Bur… http://t.co/…

Links for Thursday, November 21

Here are the links I’ve tweeted about recently. Check them out:
You can build bombs w/stuff purchased *after* security check. Why do we still have the TSA? http://t.co/zaznsR9ksv #securityTheater

RT @zmre: Airport silverware silliness. #securitytheater http://t.co/BI13XqB9kB
The Senate’s bitcoin hearing was actually pretty positive. Of course there’s always one…http://t.co/l7Pa4GWR8d
Police on being recorded when on duty: “the ubiquitous tracking is too invasive of their personal privacy” http://t.co/ASy2TXxnzj
Hey Windows 8 UI people. When you give me a pop-up I don’t want, and don’t let me click around it, I get angry. http://t.co/tiNoj5BJaI
Just read Kozinski’s dissent in White v Samsung. Finally, someone who makes sense in IP law. Read it: http://t.co/78wdNAnUBj
Pentagon says it has no idea where the money is that it’s supposed to spend. The numbers are staggering. http://t.co/QFOY6XiWU8
RT @declanm: Yahoo “plans to have all data encrypted by the end of March”: http://t.co/yakjH0L6qO Yahoo’s users might want to thank Edward Snowden …
RT @TracyOpp: I interviewed Terry Bressi on in-country #immigration #checkpoints and recording his 300+ interactions http://t.co/h9kMUIhikz

Disrupting the Currency Market

My column that originally appeared in the Duke Political Review.

The advancement of the digital age has transformed financial transactions, both in high powered trading and in everyday use. According to Nilson Reports, some $4 trillion in purchases occurred using credit, debit or prepaid credit cards in 2012. Online shopping is omnipresent, and smartphones have even rendered trips to your local bank unnecessary, as checks can be deposited directly into your account from wherever you are. The one area of payments that remained undisrupted by the digital age was the concept of currencies themselves. Continue reading