Tuesday, June 25, 2013

Arduino Scalable Single-Stage Coilgun

In my earlier post where I built an Arduino Laser Tripwire, I mentioned that I was going to use this as a component to a later project.  I've built a scalable, single-stage coilgun that uses inexpensive components and a relatively tame 50VDC.

The premise of a coilgun is simple:  a ferromagnetic or paramagnetic projectile is attracted towards any strong magnetic field it encounters.  By switching on a strong magnetic field inside a solenoid, we can attract the projectile into the coil of wire.  When the projectile is inside the wire, the field is switched off and the projectile continues on the path it was following as it was sucked into the field.

This prototype has only a single stage, meaning that only a single solenoid accelerates the projectile; However, it can easily be scaled up.  By placing another stage just like it in the path of the projectile, we can accelerate the projectile faster and faster.  The projectile will break the laser beam for the next unit, then the magnetic field in the next solenoid will be turned on thus accelerating the projectile further.

I can achieve this multistage coilgun by placing a clear plastic barrel down the center of my solenoids, then shining the laser through the barrel.

My charging circuit, a Delon voltage doubler.

My charging circuit is a simple Delon voltage doubler.  The voltage doubler uses a 12VA transformer to convert the 120V AC from a normal US wall outlet to 24V AC.  The capacitors only need to be 24V, but I happened to have 35V capacitors on hand.  To protect my Arduino from any reverse bias that might be introduced by the relay coil, I include a 1N4937 diode in my design for protection.

My Arduino laser tripwire module., modified from my earlier design.
I have modified my Arduino laser tripire module a bit for compatibility with my trigger circuit.  I need a 1N4937 diode to prevent the voltage from my main capacitor from flowing to ground through the 100 ohm resistor on the trigger circuit.  The indicator LEDs are all optional, and power draw on my Arduino could be reduced by removing them.


The firing module for my prototype coilgun.
The firing circuit is quite spartan, dominated by a large electrolytic capacitor and a cheap 2N6507G SCR.  The 2N6507G can withstand a surge current of 250A but costs only 66 cents to purchase, making it an ideal centerpiece for my firing circuit.  To simplify things further, I use an entire quarter-pound spool of 16AWG magnet wire for my solenoid.  No winding was necessary:  I simply purchased the spool, found both ends of the wire, sanded the insulating lacquer off, and crimped on terminals.  I measured its inductance at 185 microhenries.  To be absolutely sure to protect my capacitor from any reverse bias that might be introduced, I have a 1N4937 protection diode across both the solenoid and the terminals of the capacitor.

Complete schematics for my prototype, generated with KiCad.

Before getting to the demonstration, I will share the source code that I uploaded to my Arduino Uno.  This source is similar to that which I included in my previous post, except I have modified it in two ways.  First, it opens the power supply relay at the same time that the SCR is triggered.  Second, it includes a charge signal (output 10) as well as a tripped signal (output 11).

int ledPort = 13;
int laserPort = 12;
int tripPort = 11;
int notTripPort = 10;

int timerToggle = 1000;
int timerCount = 0;
boolean timerState = false;

int onLevel = -1;
int offLevel = -1;
int currentLevel = -1;

boolean tripped = false;

void setup() {
  pinMode(ledPort, OUTPUT);
  pinMode(laserPort, OUTPUT);
  pinMode(tripPort, OUTPUT);
  pinMode(notTripPort, OUTPUT);
  
  digitalWrite(tripPort, LOW);
  digitalWrite(notTripPort, HIGH);
  
  // Check the state of the phototransistor with the laser on
  // and off.
  digitalWrite(laserPort, LOW);
  delay(1000);
  offLevel = analogRead(A0);
  
  digitalWrite(laserPort, HIGH);
  delay(1000);
  onLevel = analogRead(A0);
}

// An alive signal that appears on the arduino, 
// just to let me know the program is running.
void timer() {
  timerCount++;
  if (timerCount == timerToggle) {
    timerCount = 0;
    timerState = !(timerState);
    if (timerState) {
      digitalWrite(ledPort, HIGH); 
    } else {
      digitalWrite(ledPort, LOW);
    } 
  } 
}

void loop() {
  delay(1);
  timer();
  if (!(tripped)) {
    currentLevel = analogRead(A0);
    if (currentLevel < onLevel - (onLevel - offLevel) / 10) {
     tripped = true;
     digitalWrite(tripPort, HIGH);
     digitalWrite(notTripPort, LOW);
   } 
  }
}
Source code for my coilgun, as uploaded to my Arduino Uno.

Finally, you may check the YouTube video below to see how it works!




Thursday, June 20, 2013

Pinhole Camera, Mark 3

The underside of a limpet.
I pulled a couple more of my pinhole cameras from Magnolia Park the other day.  I've gotten better at hiding them!  This time two of three were still where I left them, and one was so well hidden that even when three of us were standing with our heads less than two feet from it we still took five minutes to find it.

These pictures were plagued by a problem from my original design:  the pinhole was simply too large.  The results were a little bit fuzzy, and the effects of condensation and mist rolling in from the Puget Sound are apparent.  Further, the summer sun was cut off at the bottom of the can.  These are problems that I hope to fix in my future pinhole camera projects.  In addition to using a smaller hole perhaps half a millimeter in diameter, I will place the hole higher up in the can.  In this manner, the can mounted vertically with the hole near the top will be less prone to cutting off the sun trails in the summer.  Further, I will include some silica gel inside the can to try to absorb some moisture before it can condense.  I will also experiment with a small amount of saran wrap or clear plastic over the pinhole to try to keep any moist air out.

Pinhole camera mounted among some trees and overlooking Puget Sound at Magnolia Park facing roughly Southwest.  Black and white satin finish paper, 1mm diameter pinhole, exposed from March 3 - June 17 2013.

Tuesday, June 4, 2013

More Lessons from the Pinhole Cameras

With some help from a friend who lent me the roof of his Apartment building, we managed to take my best pinhole camera exposure to date.  It's still not great, but I'm pretty new to the art form and it's going to take some practice to get right.  This one was exposing for about 5 months from January to June 2013, facing South.  About half the frame is taken up by the roof of his building, and the rest should have looked like the city skyline, but it came out quite blurry.  The instructions I followed online for making them suggested a 1mm diameter hole, but I think that was misguided.  1mm is the diameter of a small nail, and everything came out fuzzier than a Georgia peach.  I think using an actual pin to make the hole as recommended in these instructions would lead to much better results in the future!  Also, after some experimentation with horizontal mounting, I would say that those shots were disappointing.  Be sure to mount your drink-can camera vertically (the way the can would sit if it was on the table) for best results.

In a previous blog post I suggested that the photo paper was forgiving to moisture, but the results of this image leads me to believe that in fact the fine details are washed away by any water or condensate that gets in, leaving only the bold sun-trails.

I've got a lot more of these in the works!  For now, enjoy my first kind-of decent pinhole camera photo.

The roof of an apartment building North of Lake Union, facing South over the city of Seattle.  1mm diameter pinhole, exposed on resin-coated 5"x8" black and white photo paper.  January-June 2013.

It can be improved quite a bit with GIMP, though.  If I invert the colors then take a look at the color curves, I can select a curve to improve the dynamic range of my image.

Improving the dynamic range of my pinhole camera picture using the magic of GIMP.
The end result is certainly a lot more interesting to look at than the original!

The same image as above, with colors inverted and dynamic range improved in GIMP.


Monday, June 3, 2013

Even More Betting Strategies at the DotA 2 Lounge

My previous post, Betting Strategies at the DotA 2 Lounge, is to date the most popular post I have ever made to this blog.  It's popular enough that I decided that I can do better!  There were some unanswered questions in the original post:  What are the most overrated and underrated teams?  Are people who bet rares more prudent with their bets than uncommons or commons?  Would the inclusion of all of the historical data from the DotA 2 Lounge affect my results at all?  I scraped all of the data from the first 400 matches at the DotA 2 Lounge using wget in a shell script, and threw out all matches where the bets were either cancelled or where the data was incomplete.  This left me with data on 334 historical matches from which to draw conclusions, including the odds on commons, uncommons and rares separately.  Basically, this analysis will give far better confidence than my previous one, and allow me to ask questions that I was previously unable to.

Let's start again by examining the crowd favor of the winning teams.  Just as in Figure 1 of my previous post, I expect to see this plot skewed to the right if the betting crowd has any ability at all to pick the winning team.

Figure 1:  Histogram of crowd favor of the winning team.  Clearly, the crowd usually favors the winning team (everything to the right of x=0.5 on the graph) and chooses correctly about 2/3 of the time.  

Indeed, Figure 1 shows that the crowd usually picks correctly and favors the winning team about 2/3 of the time.  Remember, though, that the goal of the gambler is not to pick correctly:  the goal is to profit!  First, as in my previous post, I need a control group to compare everything else to.  I will start by getting a handle on how well I will do over time if I flip a coin and bet randomly.  I will put 1 common, uncommon or rare on the team determined by the coin flip and chart my behavior.

Figure 2a:  Number of commons won by random coin flip in the first 334 matches at the DotA 2 Lounge.  Average = -2.7 items, Standard deviation = 19.6 items.
Figure 2b:  Number of uncommons won by random coin flip in the first 334 matches at the DotA 2 Lounge.  Average = -4.0 items,  Standard deviation = 19.4 items.
Figure 2c:  Number of rares won by random coin flip in the first 334 matches at the DotA 2 Lounge.  Average = -3.4, Standard deviation = 19.6 items.

Contrary to my original study with a smaller data set, Figure 2 gives a more flattering view of the wisdom of the crowd.  Betting randomly appears to be a bad idea, paying out at about 0.99:1 over 334 matches.  This is basically the behavior we'd expect in a system where the entire betting public has good information on what's going on.

Let's find out what happens if I bet 1 common, 1 uncommon, and 1 rare on the crowd favorite based on the payout per item for every match.  If there is no crowd favorite (1:1 odds), then I will abstain from betting.  I will track the behavior of my winnings over the 334 matches that had complete data and that were not canceled.

Figure 3a:  Behavior of betting 1 common on the crowd favorite for each of the first 334 complete, non-canceled matches. 
Figure 3b:  Behavior of betting 1 uncommon on the crowd favorite for each of the first 334 complete, non-canceled matches.
Figure 3c:  Behavior of betting 1 rare on the crowd favorite for each of the first 334 complete, non-canceled matches.
Figure 3 shows the behavior of a hypothetical scheme where I bet 1 common, 1 uncommon, and 1 rare on the crowd favorite of each match through the history of the DotA 2 Lounge.  My hypothetical winnings fluctuate around 0 and never come close to the 40-item mark that is necessary in order to reach statistical significance.  This is another mark of a rational crowd:  going with the crowd neither makes you significant gains nor losses over time.  

Now let's try the same experiment, except this time I'm always going to bet against the crowd.  Is this a winning strategy?

Figure 4a:  Behavior of betting 1 common on the underdog for each of the first 334 complete, non-canceled matches.
Figure 4b:  Behavior of betting 1 uncommon on the underdog for each of the first 334 complete, non-canceled matches.
Figure 4c:  Behavior of betting 1 rare on the underdog for each of the first 334 complete, non-canceled matches.
Figure 4 shows that betting on the underdog is also not a great long-term strategy.  Like betting with the crowd, it never approaches the 40-item mark we need in order to reach statistical significance.  So, it seems that the crowd is actually pretty decent at picking the proper odds of winning at the DotA 2 Lounge after all.  Reassuringly, I never indicated that betting with the crowd or against it was a statistically significant improvement over random betting in my previous post.  There is a claim, however, that was statistically significant:  that betting for the left column is a losing strategy over time, and betting for the right column is a winning strategy over time.  In my previous study, these actually were at the border of statistical significance.  Given the larger data set, will these assertions be supported?

Figure 5a:  Behavior of always betting 1 common on the team in the left column.
Figure 5b:  Behavior of always betting 1 common on the team in the right column.
Figure 5c:  Behavior of always betting 1 uncommon on the team in the left column.
Figure 5d:  Behavior of always betting 1 uncommon on the team in the right column.
Figure 5e:  Behavior of always betting 1 rare on the team in the left column.
Figure 5f:  Behavior of always betting 1 rare on the team in the right column.

Figure 5 does seem to show a sustained trend towards gamblers betting favorably towards the team in the left column, and indeed there seem to be times when this could be construed as significant.  In particular, the peak around match 180 in 5b, 5d and 5f seems to be fairly significant given the number of matches played thus far.  Fast forward to today, though, and the crowd has since rectified its irrational ways.  Whether you're betting commons, uncommons or rares in the right-column strategy, you are nowhere near statistical significance today.  

I plan on following up with another post on the most overrated and underrated teams, but first I want to correct my conclusions from my original post.  Upon gathering more data from the DotA 2 Lounge, my previous conclusions are definitely affected:  betting for the right column may have been a statistically significant good strategy in the past, but it is not anymore.   The strategies of flipping a coin, always betting with the crowd, and always betting for the underdog all remain statistically insignificant.  

I definitely made an amateur mistake in overstating the trends I did find.  It is not correct to advocate a strategy (such as flipping a coin or betting for the underdog) that is not statistically significant--I've learned from this mistake, and will not repeat it in the future!

If you'd like to gather your own data from the DotA 2 Lounge and perform your own analysis, I've made the scripts I used available.  Please feel free to use the data you gather for any purpose you like.  If you decide to make your own blog post about the trends you find, let me know and I'll link you from here!

Update:  If you enjoyed this post, you may also like my more recent post: What is the Most Underrated DotA 2 Team?

Monday, May 20, 2013

Betting Strategies at the DotA 2 Lounge

[Update:  I have revisited these conclusions with more data and better data in a new post!  Long story short:  don't rely on these suggestions!]

Lately a friend and I have been betting items for DotA 2 at the DotA 2 Lounge.  Having something riding on a game adds an element of excitement that isn't there otherwise--you personally, have something on the line and a reason to root for your favorite team.  Further, I've always found parimutuel betting pretty interesting.  Assuming zero house take and a crowd with perfect information, then the only way to reliably win in the long term is to identify irrationality in the crowd and exploit it.  Playing to simply win is an amateur mistake:  playing to maximize profit is the correct approach.  Using a collection of 190 historical data points from past matches at the DotA 2 Lounge scraped from the internet archive, I will attempt to identify some irrationalities in the DotA 2 fan-base.  If any strategy I propose turns out to be successful, then it can be said that the underlying strategy corresponds exactly to an irrationality in the betting public!

Figure 1:  A histogram of the proportion of bets by value in favor of the winning team on the DotA 2 Lounge.    Bars on the left side represent cases where the underdog won (and represent a correspondingly better payout), and bars to the right side represent cases where the crowd favorite won.  The crowd picks the correct winner about 2/3 of the time.

Figure 1 shows a simple histogram:  the distribution of bets in favor of the winning team.  I expect this plot to be skewed towards the right if the crowd has any ability at all to pick a winner.  Indeed, the distribution in Figure 1 is skewed right, but not very heavily.  I want to propose some cheesy betting strategies and see how well they work, but before I can discuss the significance of those strategies I need to know what the standard deviation of random strategies are.  I will run 10,000 simulations of random betting strategies (flip a coin to determine which team to bet an item for) and examine how well I would be expected to do just with dumb luck.

Figure 2:  Histogram of outcomes of 10,000 random betting strategies on my data set of 190 games at the Dota 2 Lounge.  Average = 9.7 items, Standard Deviation = 17.11 items.

It turns out that in my attempt to produce a control group, I have already identified a glaring irrationality of the crowd.  Figure 2 appears to be a complete paradox:  if you flipped a coin before every match and bet based on the outcome of the coin toss, you'd end up, on average, ahead!  This paradox is possible because real gamblers at DotA 2 Lounge do not bet randomly nor does the crowd correctly predict the fair odds of a team winning.  If either were true, then the outcome of random betting would be centered at zero.

Let me propose two alternate betting strategies, then, in direct opposition to each other.  First, I propose, "Always bet in favor of the crowd favorite."  I will always bet one item for the crowd favorite.  If there is no crowd favorite (50-50 tie), then I abstain from betting.  It turns out that if I pursue this strategy, even though I would win in 118 of 187 matches, I would be down 14.6 items.  This is an example of the amateur mistake I mentioned earlier.  Here, I have bet to win, and in fact I did win 118 of 187 times.  I did not, however, bet with profit in mind.  My winnings were small because I had to share a smaller pool of items with a lot more people.  Betting with the crowd seems to lose in the long term at 0.92:1.  This is plausibly within statistical noise at 1.42 standard deviations from the mean.

For my second cheesy strategy I propose, "Always bet against the crowd favorite."  Again, if there is no crowd favorite, I will abstain from betting.  With this strategy, I will always bet one item for the underdog.  In this case, I win only 69 of 187 matches but I am up 33.7 items!  Even though I only rarely win in this scenario, when I do win I tend to win big!  Betting against the crowd seems to win in the long term with a payoff of 1.18:1.  Don't be the farm on this approach, though:  this is plausibly within statistical noise at 1.40 standard deviations from the mean.

So far it appears that, if anything, the crowd tends to be slightly biased towards the favorite to win.  Perhaps part of this is that people are more focused on winning than on the potential payoff and bet for the genuinely better team more frequently than the other.  

Let me try some other strategies next and further probe the depths of the crowd's irrationality.  For my third strategy, I'd like to try, "Always bet for the team in the left column."  If betters are rational and the placement of the teams in columns is random, then my net haul using this strategy should be close to zero.  In fact, it turns out that betting one item per match for 190 matches on the team in the left column would win 90 times--but I would be down 24.6 items!  Betting the left column every time is a losing strategy with a long term payoff of 0.87:1.  Remarkably, this is outside statistical noise at 2 standard deviations from the mean!  This is thus probably not a fluke--people really do tend to bet for the left column preferably!

On the other hand, my fourth strategy will be, "Always bet for the team in the right column."  Again, I would expect the net haul using this strategy to be close to zero.  This strategy blows away all of my others, winning me a whopping 43.66 items in 100 victories!  Because the number of victories is very close to the expected number of 95, this indicates that gamblers are strongly biased to favor the team in the left column versus the team in the right column!  Betting the right column every time is a winning strategy with a long term payoff of 1.23:1.  This is barely inside statistical noise at 1.98 standard deviations from the mean.

In conclusion, the gamblers at the DotA 2 Lounge do seem to have an exploitable irrationality:  people tend to vote too much for the left column.  The best simple strategy is to always bet for the right hand column to win--a strategy that does significantly better than flipping a coin!  Beware, though:  if many people read this article and use these techniques, then these strategies may stop working entirely.  My results are based on historical data, and if the crowd is able to identify its fallibility based on this information and adapt then an entirely new strategy will be necessary to win!

Good luck with your wagers, and most importantly enjoy the game!

[Update:  I have revisited these conclusions with more data and better data in a new post!  Long story short:  don't rely on them!]

Saturday, May 11, 2013

A Breast Cancer Survival Modeling Competition

California Whipsnake
This week, a paper on breast cancer survival modeling that I co-authored was published in PLoS Computational Biology.  Using a pool of patient information including clinical covariates, gene expression and copy number variation data for a collection of breast cancers we each attempted to produce the best predictive model of breast cancer survival that we could.  Our results and code were available in real time on a competition-style leaderboard that was automatically updated as we submitted new models to the competition.  In the end, we did achieve predictive models using the genetic data which performed statistically significantly better than the clinical-only models.  This is actually a fairly remarkable feat, since the genetic data was quite noisy.  In fact, while signals abound in the genetic data, actually adding this data to a clinical model tended to confuse the modeling algorithms and hurt our predictive power as often as it helped.

In the end, while our models were statistically significantly better, I don't believe that the difference was of much practical significance.  More worthwhile is knowing which signals actually improved our predictive power--these may be worthy of further investigation to better explain the correlations between the feature and cancer survival.

I'm not going to regurgitate the paper which is freely available via the link above, but I do want to highlight a few points that I believe are important and of general interest:

  1. Random Survival Forest is the best out-of-the-box survival model we found.  After testing this on some other data sets, I believe that Random Survival Forest may in fact be your best bet if you are doing survival modeling.
  2. A leaderboard or real-time model evaluation system is an enormous motivator in one's research.  The fact that you can quickly tweak a model and have it evaluated and placed next to your others for comparison takes much of the grind out of research.
  3. Competitions are not an inexpensive option to hiring personnel or doing your own research.  The computational overhead, tech support, and manpower to procure the data, produce the evaluation system, advertise the challenge, police the participants, and evaluate their submissions requires a substantial amount of manpower and expense.  

Wednesday, April 17, 2013

The Science of Crowdsourcing Versus the Crowdsourcing of Science

Today Science Translational Medicine published our methods paper for the Sage/DREAM Breast Cancer Challenge.  There were two things we wanted to see from the challenge:  one was to was an improved model of breast cancer prognosis, the other was to find out how people would interact in a competition where their submission was immediately made public and free for the other competitors to do what they will with.  I won't repeat what's in that paper here, but I do want to comment on the difference between the science of crowdsourcing and the crowdsourcing of science.

Having competed in more than my fair share of modeling and programming competitions, I've seen attempts to foster meaningful collaboration in problem solving before.  The TopCoder Soybean Challenge is one example, and EteRNA is another.  The Sage/DREAM Breast Cancer Challenge is the first I've seen where both a winner would be declared, and also everyone's submissions are always publicly available.

One thing must be said about online coding competition competitors:  they are a crafty bunch.  Like a genie from a cursed bottle, they will give you exactly what you ask for even if that's not what you want.  Further, the crowd will adopt a strategy that will maximize their chance of winning.  Nobody plays for second place.  In this case, we saw exactly the behavior one would expect in a situation where everybody's submissions and source were public:  obfuscation of code, and a single leading team putting in most of the work that (nearly) everybody else copies from.  Like many other online competitions, notably EteRNA and FoldIt, a small number of our competitors did the vast, vast majority of the work.  The science of crowdsourcing--motivating a group of people to perform the task you want--is thus still a work in progress.  Our model of collaboration did not solve the fundamental problem of making a lot of people work together efficiently.  It found some very good people who came up with a very good solution, which has been done many times before in other competitions.  The Sage/DREAM paper is a paper about the science of crowdsourcing.

The thing I'd like to discuss now is the crowdsourcing of science:  how to motivate people to work together to identify a problem, fund a solution, and democratically work towards the goal of solving the problem.  My litmus test for a good system would be this:

"Can a new PhD student come to your project, learn the background they need from your documentation, contribute to it for his thesis, and also receive a stipend or other compensation for his work?"

I posit that the most difficult barrier to cross in setting up this system would be funding.  In the current system, most academic research money comes from government grants.  I have discussed previously some of the flaws of this system.  There are a lot of patient advocacy groups and private foundations, however, that I believe would fund a single ambitious project like "Cure This Disease" if there was only a demonstrated mechanism in place to make progress on it.  Currently these groups give grants to academic groups for less ambitious small projects since no single lab anywhere may have the resources or manpower to tackle the grand problem.

In order to attack these grand problems, I would favor an engineering approach much like you see in open-source projects.  In crowdsourced science, users would post questions instead of feature requests, then write documentation, code or propose lab experiments to answer them.  If a user sees a flaw in documentation or methods, they can post a bug report so that others may come along and fix it.  Funders may then choose to compensate users who write good documentation and code as an incentive for their continued participation, or to pay a lab to run an experiment that they believe could answer a question.  In the future, independent labs might even bid against each other to run these experiments.

Solid documentation and clear answers would be the key to the success of this model.  The institution I propose would be able to educate all stakeholders, both funder and scientist, to make the best democratic decision on how to proceed.  This clarity is something that much scientific literature and research lacks.  Indeed, the difficulty in replicating many results is a hot issue right now.  The ideal model would also challenge the existing power structure in academia since this democratic forum would choose the best answers in real time (much like StackOverflow).  Reputation can and should still play a role, but any obviously better answer will still float to the top when all solutions sit side-by-side.

Indeed, the crowdsourcing of science would be a bold experiment.  It has never been tried before.  I believe that a democratic marketplace of minds, ideas, coders, writers, validators, experimentalists  and funders is the future of science in the same way that open source is the future of software, and I would be thrilled to contribute my time to such a project when one does arise.