One piece of code
What’s an hour in the life of a graduate student?
Well time certainly does fly. I’m currently wrapping up the monster of a thesis, but I thought that I should get into the habit of sharing something of my work and what I’m working on.
My thesis is developing into a knitr
knightmare, but I’ve learned a lot a long the way.
I’ve also learned to use c++
, python
, and become an r
person to the degree that I can
suggest code for a program off the top of my head.
All this to say I have spent most of my time in front of a computer working on some piece of code.
I thought as much as I can I’ll try sharing a piece that was useful to me. I’ll try to make it reproducible, but we shall see.
Today I mostly worked on developing a method for optimizing a model in relation to a particular parameter and survey results.
This will not be reproducible, but I’m pretty excited that I found a way to automate model fitting.
First here is the code:
if(pIdealFound)
{
int i; int prev_birds[2];
delta_T = survey_total-totalBirds;
if(date<42) i = 0;
else i = 1;
int currentbirds = number_birds[i];
if(k==0) number_birds[i] *= 1.2;
else if(abs(delta_T)<10)
{
idealN_found=true;
finalNumbers =number_birds[i];
}else
{
if(!SameSign_i(prev_delta_total, delta_T))
{ //cout << "WTF" << endl;
number_birds[i] = (prev_birds[i]+currentbirds)/2 ;
} else
{
if(delta_T<0)
{
if(abs(prev_delta_total)>abs(delta_T)) number_birds[i] /=2;
if(abs(prev_delta_total)<abs(delta_T)) number_birds[i] = prev_birds[i]/2;
}
if(delta_T>0)
{
if(abs(prev_delta_total)>abs(delta_T)) number_birds[i] *=2;
if(abs(prev_delta_total)<abs(delta_T)) number_birds[i] = number_birds[i]*2;
}
}
if(number_birds[i] < 100) number_birds[i] = 10000;
}
}
I used a function from here to check if two numbers are the same sign, but otherwise this mess is my own creation. The goal is to find the number of birds in a population that minimizes the difference between observed counts and the model output for a given date.
There are a lot of if
and else
in there, but the aim is to jump up or down until you are jumping back and forth between the optimum until you get your model outputted count to within 10 birds of the observed count.
The next step is to check this code, but it seems to work for now. If you see a new commit tommorrow you’ll know I’ve found a mistake.