Showing posts with label Just for fun. Show all posts
Showing posts with label Just for fun. Show all posts

Tuesday, February 14, 2012

Python

There is a sort of running nerd joke about how easy the programing language python is to use.  Basically its that to do ANYTHING in python, you just use the command "import ____" and the bulk of your programing is done.

ITS TRUE.

I teach an MCAT class using pdf files, and from every file I use pages 3, 4, 7, 8.... and so on.  Its a pain to make new pdf files with only these pages, and I'm always afraid I'm going to skip a page.  Turns out there's a library of commands called "pyPdf," and using that library I was able to hack together a program that does exactly this, but more reliably and virtually instantly, in about a half hour.

The key word here is "hack." It is an ugly program, that I'm sure does things the "long" way, and to use it you have to copy/paste a folder path into the code itself which is totally against every programing style rulebook, BUT... it does its job, in spite of my programing ignorance.  So, in light of this blog's overall tone as an accretion of my random thoughts/fun things that I've done, here's the script (it needs http://pybrary.net/pyPdf/ to be installed)

import os
from pyPdf import PdfFileWriter, PdfFileReader

#directory
folder = "/Users/tylerhoppenfeld/Documents/unit2"

# create extraction function
def extract(start, source):

    # variables that should be passed when script is called, but i'm too lazy

    out =  source + "clip.pdf"

    output = PdfFileWriter()
    input = PdfFileReader(file(source, "rb"))

    endpg = input.getNumPages() - 1

    # generate list of pages to keep

    facingstart = start + 1
    pgs = range(start,endpg,4)
    add = range(facingstart, endpg, 4)
    for x in add:
        pgs.append(x)

    pgs.sort()

    # build output
    for x in pgs:
        output.addPage(input.getPage(x))

    #write to new file
    outputStream = file(out, "wb")
    output.write(outputStream)
    outputStream.close()

target = os.listdir(folder)

for x in target:
    if x.endswith(".pdf"):
        y = folder + "/" + x
        extract(2,y)

Sunday, October 30, 2011

Marginal Utility of Wealth

Wow.  Every so often, you read something that completely re-frames your thoughts on something, or makes clear both a paradox and its explanation.  If you're an economist, you probably think in terms of marginal utility to wealth: as Bill Gates put it, after a certain point "it's the same hamburger."  This generally (always?) holds when we're talking about more of a good thing.

But when it's not about making your life better, but instead about making a bad thing stop, we see the reverse effect.  Imagine a pile of bills.  A huge pile of bills on your kitchen table.  Not just any kind, but the pink kind, that mean you'll have your utilities shut off next week if you don't pay, and the debt collectors are already calling you about your 5 credit cards, or would if you still had a phone.  Imagine how terrible that makes you feel.  If you're reading this blog, you've probably never faced this, or at least not lately, but even imagining it is not pleasant.  Now imagine paying one of your bills.  Feels better?  Nope, didn't think so.  Now the lights are on and the phone works, so the terrible situation is just that much clearer.

NOW imagine paying the last bill.  Wow, major relief.  You just experienced increasing marginal return to wealth, something that 5 years or so of economics background prepared me to more or less expect to never exist.  Now please excuse me while I re-examine most of my thoughts on povert.  Thank you Blogosphere!

Now, because I'm an econ nerd, here's what's happened to my implicit model of how people value stuff:

let u(x) be some log-type function such that du/dx is decreasing, approaching zero.

Before:

Utility= u(income)
Which looks like:

After:

let P be some constant of income that is needed to make the bulk of misery go away.  Perhaps it's the poverty level?

Let C be a value chosen between 0 and 1 by the individual to maximize their utility level at any given income level.

Utility = u(C*income)- max{0, u[P-(1-C)*income]})

Which has varying returns to wealth: some areas will have increasing returns to wealth, and some will have decreasing returns to wealth.  
Additionally, since the first term is "stuff you want" and the second term is "stuff you don't want" we can see c as a "indulgence" variable.  A higher C value means more indulgence, and a lower C means less indulgence.  This fully explains why "indulgent" choices make sense: if your demand function looks like this, you may well get more utility out of an "indulgent" choice of a $100 pair of sneakers than paying that nagging $100 utility bill that's 90 days past due.

I'm going to read up and see if anything in the "serious" econ literature explores this idea, and probably come up with a more detailed toy model of how wealth and utility might be related.


Tuesday, October 5, 2010

Levitating Frogs

A while ago, there was a picture of a frog, floating by magnetism, making its way around the internet.  I re-stumbled on it, along with a youtube video of it (it tries to swim its way out).

For no other reason that its sheer awesomeness, we have the youtube video itself, a non-technical explanation of how this works, and then the scientific paper in PDF format.



Less technical explanation

Technical paper (PDF)