A random mental walk.

Sunday, December 30, 2018

Devil's Staircase in MATLAB

I can't recall how I stumbled over the description of the Devil's Staircase, but it sounded like an interesting programming problem.  Although I recognized the name, math ability has deteriorated to the point that references to Cantor Sets made no sense until I stumbled over pi.math.cornell.edu/~mec/Summer2009/Whieldon/Math_Explorers_Club%3A__Lesson_Links/Entries/2009/7/31_Lesson_3%3A__The_Devils_Staircase_%26_Other_Uncountable_Problems.html
 and the illustration below:
It took an embarrassing long time to get the coding correct.  I was reminded once again of truism I first heard from Roy Mendelson: The first step to solving a problem is the correct statement of the problem.

It took me a while because I was distracted by a seductively sneaky solution to the problem using MATLAB's array math. If the steps below the middle step could be calculated, all the steps after the middle step could be calculated in one swell foop by adding the value of the end point of the middle step to the array of steps between 0 and the lower x-value of the middle step.

It took me a while to find the appropriate quote from Knuth: "The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming." (From Computer Programming as an Art, p. 671, https://en.wikiquote.org/wiki/Donald_Knuth)

It was only after I'd solved the problem myself that an online search found a recursive solution (m2matlabdb.ma.tum.de/download.jsp?MC_ID=5&SC_ID=13&MP_ID=104) which provides not only a plot of the staircase, but the Cantor set as well.

(I avoid recursion because 1) I'm not good at it (why not admit it here), 2) recursion generally uses more memory than looping, and, lastly, I suspect that the repetitive function calls is slower.)

From a quick look at the recursive code I like mine even better because the recursive code starts with a conditional check to see how many steps are requested whereas mine will take any step.  (To be frank, however, the recursive code does the mandatory parameter checking.  Mine assumes that the user will not pass an invalid number of steps, e.g., 0 or a negative number, or pass a string or other pathalogical value.)

The recursive code creates the middle step and then if more steps are required generates the left and right side of the Devil's Staircase.  The code involves a lot of multiplication of array values.  My early assembler experience always made me avoid multiplication and array values.  I wish I could blame a cruel instructor for beating into using pointers and addition, but I did it to myself.  As MATLAB doesn't have pointers (yeah, I know, arrays are really pointers) I had to use arrays also.  You can see from the code that calculating a new vector value required two or three multiplications, depending on how you count.  (I'm guessing that the multiplication by 0.5 is accomplished by shifting the bits to the right rather than actually multiplying by 0.5

xr(stufe)=.5*((1-e)*xr(stufe+1)+(1+e)*xl(stufe+1));
yr(stufe)=.5*(yr(stufe+1)+yl(stufe+1));

My MATLAB solution is far from inspired, but much shorter than the recursive method.  The code generates the two member vector (0 and 1) and calls the genXValues function to generate the new values by using every other value in the current vector.  Here's the heart of it:

It' not pretty but it works.




Tuesday, December 25, 2018

Hey A Custom Error!

Anyone out there get this message on outlook?  I was trying to log in and an unknown error occurred. I'm sure alarm bells must be ringing in Redmond as they scramble to track this down.

Saturday, December 15, 2018

Wine & Spirits

Passing a store advertising Wine and Spirits I wondered what wine goes with the ghost of Christmas past?

Would a Merlot go with the specter of a woman spurned by her lover? 

What should be paired with an incubus?

Wednesday, August 15, 2018

Fintech, Adoption & 23% Interest

Always on the lookout for material for my classes, the headline of Hannah Levitt's article in the Wall Street Journal (July 3, 2018) caught my eye:

Personal Loans Surge to a Record High

The article was about fintech, newer players on the consumer lending scene.  Established entities like Goldman Sachs have entered the market along with startups.  What got me though was the lead:

"Heather Turner and her husband needed a few thousand dollars to jump-start the adoption of a teenager from Ukraine.

"The Turners, of Lewiston, Maine, needed a speedy loan and didn’t want to borrow against their house or car. Heather Turner said LendingClub arranged a 3-year loan for less than $10,000 last October at an interest rate around 23 percent -- similar to that of a credit card. Most notably, the loan is unsecured."

TWENTY-THREE bleeping percent!

A whole load of unanswerable questions popped into my mind, not the least of which was the 23%.  Doesn't that border on usury?  What was the need for speed?  What was the time pressure on adoption?  Was the kid on sale?  If the Ukrainian government was set to impose restrictions that fact wasn't in the article.

I understand the compassionate urge, but going into debt?  I didn't have the nerve to try to contact them to find out why, but then there's the internet and a gofundme page to supply the answers.

So Old News

It takes me a long time to get around to reading stuff, and a little longer to actually doing something.  Here's the latest example: An article in the November 18, 2015 WSJ with the headline "Mallinckrodt: Infant Drug, Gown Up Problem".

(I have a soft spot in my heart for Mallinckrodt because I still have their 60 year old notepads with yellow grid lines.  My dad drew cyclic organic compounds on them andI thought they were cool.  Also, I like pronouncing their name.)

The article concerned the drug Synacthen and Mallinckrodt's decision to raise the price of the drug 2,000%, a move which put it on the same footing as all the other bad boys in Pharma who wanted to show that without competition they could stick it to the patients.  The explanation/excuse was that the drug had been sold at a loss and the new price would insure that the company could continue to produce the drug.

The fact that the drug was approved to treat infantile spasms triggered a dim memory of drugs which were approved for one use, but were used for others.

A web search (just now) found a Washington Post article dated January 18, 2017 "Maker of $34,000-a-vial drug to pay $100 million for allegedly preventing competition".  If the revenues from that drug brought in $1 billion in 2015, a penalty of 10% of gross sales might not be too tough to handle.  The charge was that the drug, H.P. Acthar Gel, which once sold for $40 a vial, was acquired by Questcor Pharmaceuticals in 2001.  Questcor raised the prices and was acquired by Mallinckrodt.  When a rival company, Novartis, planned to market Synacthen, a potential competitor to Acthar, Mallinckrodt bought the rights, thereby eliminating a competitor.

There's nothing as comforting as a story which reinforces my believe in the venality behind much of big business.  And I say this knowing that my own financial security is dependent on companies making money, often by sticking it to those who are powerless to resist..

Wednesday, March 28, 2018

"If it costs you your peace, it's too expensive."

An email from International Living (internationalliving.com), a newsletter for expats and expat wannabes contained this quote from Bobby Blanchard who seems happily living simply in Puerto Vallarta: "If it costs you your peace, it's too expensive."

Blanchard and his significant other are getting by on $1,000/month.  I don't know what they aren't doing, but it falls in line with a thought which has been rattling around in my mind for a while: what do you really need? 

For those of us who do not play golf access to a golf course is immaterial.  The though of not being able to play golf would cause others to see their whole life to flash before their eyes.  As I read the messages from International Living I find myself thinking, "Not for me".  Their correspondents enjoy the local shopping (Nfm), the cheap happy hours (Nfm), the inexpensive local wine (Nfm), beer (Nfm), etc.

Sigh.

I'm looking for something, but I haven't seen it described yet.

Blog Archive