Guessing the next occurrence in a date/time sequence

Analyzing a sequence of dates to determine the best guess for when the next event will accor is something best left to statistical modeling tools, but I’m going to try and show you a very basic way to do exactly that using PHP and an array of date/time stamps.


First off the data/time stamp sample array:

$time_stamps = array(
    "5/10/2013 9:30",
    "5/11/2013 9:50",
    "5/12/2013 10:20",
    "5/13/2013 10:59",
    "5/14/2013 11:22",
    "5/15/2013 11:51",
    "5/16/2013 12:28",
    "5/17/2013 12:57",
    "5/18/2013 13:13");

Sense the object of this exercise is to guess the next occurrence we are going to need a function that analyzes the difference between the times and then gives us an average based on that difference.

 function get_average($time_array)
 {
      $total = "";
      $last = "";
      $count = count($time_array);
      for($t=0;$t<$count;$t++)
      {
           $time = $time_array[$t];
           if($t == 0)
           {
                $last = $time;
           } else {
                $diff = get_diff($last, $time);
                $last = $time;
                $total += $diff;
           }
      }
      $average = ($total/($count-1));
      return round($average);
 }

 function get_diff($time1, $time2)
 {
      $t1 = strtotime($time1);
      $t2 = strtotime($time2);
      $diff = ($t2 - $t1)/60;
      return $diff;
 }

In the above function “get_average” you will see the for loop running through the array of date/time stamps and calling the function “get_diff”. The first time the loop runs it sets the varable $last to the first date/time stamp, this is to give us a starting point otherwise when we ran the “get_diff” function it would return an error or a number based on a date out of our sample rage and would therefore make are guess extreamly inaccurate, instead of just somewhat being inaccurate. The get_diff function compaires the last date/time sample and the current date/time sample in the array and returns the differance in minutes, that time is then added to the $total. Once all the items in the array have been calculated the $total is divided by the count of the date/time stamp array minus one (1), we minus one (1) because the first value of the array is not calculated, it is just a starting point to calculate the difference of the next value in the array.

 function guess_next($date_array)
 {
      $diff_avg = get_average($date_array);
      $last_date = new DateTime(end($date_array));
      $last_date->add(new DateInterval('PT'.$diff_avg.'M'));
      $next = $last_date->format('m/d/Y H:i');
      return $next;
 }

We now have the average increase in time to base our prediction on and we can use the above function to tie it all together. We take the last date in the date/time stamp array and add the average to that date and then format it to a readable format and we are done. The next occurrence will most likely occur around “05/19/2013 13:41”.

Obviously this isn’t going to be super accurate but it will provide a “best guess” based on the data provided. If anyone has a better algorithm for guess the next occurrence in a date/time array I would love to see it.

Thanks for reading!


Full code:

 function get_average($time_array)
 {
      $total = "";
      $last = "";
      $count = count($time_array);
      for($t=0;$t<$count;$t++)
      {
           $time = $time_array[$t];
           if($t == 0)
           {
                $last = $time;
           } else {
                $diff = get_diff($last, $time);
                $last = $time;
                $total += $diff;
           }
      }
      $average = ($total/($count-1));
      return round($average);
 }

 function get_diff($time1, $time2)
 {
      $t1 = strtotime($time1);
      $t2 = strtotime($time2);
      $diff = ($t2 - $t1)/60;
      return $diff;
 }

 function guess_next($date_array)
 {
      $diff_avg = get_average($date_array);
      $last_date = new DateTime(end($date_array));
      $last_date->add(new DateInterval('PT'.$diff_avg.'M'));
      $next = $last_date->format('m/d/Y H:i');
      return $next;
 }

 $date_stamps = array(
      "5/10/2013 9:30",
      "5/11/2013 9:50",
      "5/12/2013 10:20",
      "5/13/2013 10:59",
      "5/14/2013 11:22",
      "5/15/2013 11:51",
      "5/16/2013 12:28",
      "5/17/2013 12:57",
      "5/18/2013 13:13");

 print(guess_next($date_stamps));

Droping Google Drive

Today I stopped using Google Drive as a large cloud storage service. I’ve deleted everything but my Google Docs and started using my DropBox account for day-to-day cloud syncing, for backup I’m using Cloudberry S3 backup desktop to backup to Amazon.com’s Glacier.

At $0.01 per-gigabyte Amazon Glacier is hard to beat, but there are some hidden fees if you plan on restoring all the data at one time. You can use the Unofficial Amazon Glacier Cost-Estimator Calculator to get an idea of what it will cost you.

Google Drive Limitations

After almost a week of using Google Drive I have found a limitation that is a show stopper for me. If you add a file to you Google Drive folder that already exists online it doesn’t check to see if the file is the same as the one online it just re-uploads it, as you can imagine this can be rather irritating if you have 70GB, as I do, of data that is already on two systems that you would like to keep in sync.

Other limitations that are bothering me:

  • It doesn’t tell you what file/folder it is currently syncing.
  • If it errors out it doesn’t give you much info, so if it hung on uploading a file you have to enable debugging and dig through the log file to track it down.
  • Memory usage is way to high, for me average running usage is around 300MB and I’ve see it get up to 1GB.

Google Drive “Unable to sync”

Google Drive (http://drive.google.com) has been long anticipated and finely here. I’ve been using it for the last three days and I put it through some tests and it has passed almost every one. One problem did arise, it got stuck with an error “Unable to sync” with no other information, to resolve the “Unable to sync” error I did the fallowing:

  • Restart Google Drive.
  • Enable diagnostic mode.
    1. Hold Shift and right click on the Google Drive icon.
    2. A new option “Enable diagnostic mode” will be in the list, select it.
    3. A new windows will show up, change the option “Log level” to “Debug”.
    4. Click the “Start logging” button.
  • After Google Drive gives you the Error “Unable to sync” open the debug file (Win7: %LOCALAPPDATA%\Google\Drive\sync_log.log)
  • Look through the log file for an error about some file/s not being able to sync.
  • Once you find the file/s remove them from their Google Drive folder/s on your system.
  • Restart Google Drive.
  • If after a restart Google Drive it is able to sync without any errors then you can manually upload the file/s that you had issues with through the Google Drive web interface (http://drive.google.com).

I have uploaded over 70GB of files and I have only had a problem with one file that happened to be one PSD file out of about 70 PSD files.

Gerber Knifes

For about a year or two I’ve owned a Gerber Applegate-Fairbairn that I have found to be a very nice knife for under $100. It holds a good edge, its light, and it’s not to small nor to big.

About a month ago I was messing with it to see if I could adjust the location of the belt clip and somehow stripped out the belt clip screws. I talked with Gerber and they sent me a set of new screws and through-in a new belt clip as well. I tried putting the new screws in and came to the realization that the screw holes were stripped out as well, so I contacted Gerber again and they told me to send the knife in.

Well today, about a week and a half after I sent the knife in, I received a package from them and in it was a brand new knife.

Thank you Gerber!

EC2 Proxy/VPN

Today I ran across a post talking about using Amazon’s EC2 service as a VPN to secure your wireless connection when on a public wi-fi. I read through the how-to and figured I’d write up my much easier and quicker way of doing basically the same thing, plus I think it maybe a bit cheaper.

  1. Create a EC2 instance, you don’t need anything fancy just the very basic.
  2. After you’ve created a EC2 instance and downloaded your key pairs setup your ssh tunnel on your system by doing the following:
    1. Download PuTTY
    2. Download the PuTTYGen tool
    3. Convert the amazon EC2 key pair you downloaded to a ppk file.
    4. Under the Sessions section put the default user name for you EC2 instance followed by the EC2 instance URL in the “Host Name” section. (exp. [email protected])
    5. Click and expand the “SSH” section and click on the “Auth” section
    6. Under the “Auth” section click the “Browse…” button under the “Authentication parameters” and find the key pair that you converted to a ppk file using PuTTYGen.
    7. Under the “SSH” section click on the “Tunnels” section.
    8. Under the “Source port” input a random port like 7070.
    9. Choose the “Dynamic” radio button and leave the “Auto” radio button selected.
    10. Click back on the “Session” section and under the “Saved Sessions” give your session a name and click the “Save” button.
    11. Now click the “Open” button at the bottom of the window and you should now have a SSH tunnel to your Amazon EC2 instance.

Using the command line SSH:
– Open the tunnel by using a command like this –> ssh -C2qTnN -D [Random Port] ec2-user@[EC2 Instance URL] (exp. ssh -C2qTnN -D 7070 [email protected])

Now that you have the tunnel running all you have to do is point your browser/software to use the SOCKS proxy 127.0.0.1:[Random Port You Selected]. (exp. 127.0.0.1:7070)

Anti-Virus Test

Today I decided to test out a number of the anti-virus scanners that are floating around the internet. I didn’t do a very in-depth test, just gathered a archive of 3765 viruses and tested each anti-virus scanner to see how many of the viruses it found and how many files it scanned.

Note: Some of the viruses had files archived in side of them so the scanned number will sometimes be higher then the number of files I listed above.

Microsoft Security Essentials (Scanned: 3799, Found: 3668)
* Quick scan.

Kaspersky Anti-Virus 2010 (Scanned: 3851, Found:3680)
* Requires reboot after install.
* Slow scan.

Sophos Anti-Virus (Small Business) (Scanned: 3772, Found: 3631)
* Is designed for a management environment (10+ systems) and not a SOHO environment.
* Bad scan reports.

Sunbelt Vipre (Scanned: 190, Found: 13)
Don’t know what happened with this but it wouldn’t scan more then 190 files.
* Quick install.
* Requires reboot after install.
* By default it doesn’t scan inside archives.
* Can’t scan archives inside other archives.

F-Secure Anti-Virus (Scanned: 3757, Found: 3698)
* Requires reboot after install.
* Minimal interface
* If system has low memory it disables email scanning and other advanced process monitoring by default.

Avast Pro (Scanned: 3796, Found: 3644)
* Requires reboot after install.
* Quick install.
* Really quick scan.

Panda Cloud Anti-Virus
I stopped the scan after letting it run for about 10 min. and it had only scanned 233 files and only found 150 infections.
* Quick install
* High CPU usage.
* Requires network connection
* Very very slow scan.

ESET NOD32 (Scanned: 3739, Found: 3622)
* Quick virus database update.
* Very fast scan.

ClamAV for Windows (Doesn’t support scanning one file or directory)
* Easy install
* Can’t scan individual files or directory’s

Avira AntiVir (Scanned: 3773, Found: 3716)
* Quick scan.
* Bit high on system resources

AVG Anti-Virus (Scanned: 3774, Found: 3702)
* Somewhat slow install.
* Low system foot-print.

What Came First the Chicken or the Egg?

I was thinking today about the common question, what came first the chicken or the egg? Now this is a question that many people have heard and many have jokingly argued about so in this post I will show you my answer to what came first the chicken or the egg?

First off I don’t think the question is as simple as one or the other came first I think it is going to depend on your beliefs. You have to ask your self one question before you can start to analyze what the answer to this basic yet profound question could be. That question is do you subscribe to Darwinism or to creationism? If the answer is creationism then the answer in my mind would be one of two answers the first one being the chicken for God created the chicken and the chicken lays the eggs. The second possible answer would be both for God created everything.

A Darwinist could say that the egg came first. A bird very similar to the chicken but not evolved to be what we know now as a chicken laid an egg. In the possess of that egg embryo forming into a baby version of what ever bird laid it there was a mutation of some kind. The mutation changed the embryo enough that it grow into what we know now as being a chicken, therefore the egg came first, right? Well maybe not by using the above argument you could stat that the chicken came first for the egg was the egg of a deferent bird so therefore was not the egg that the question refers to. My argument ageist this is that once that mutation took form the egg became the egg of a chicken, not laid by a chicken but the protective home for the new chicken embryo, letting it grow and thus became a chicken egg.

A deferent view could be that the question refers to the first egg ever to come into existence if you look at it by this point of view then you would have to analyze life from the beginning and that again would depend on if you are for Darwinism or for creationism. Sens the creationism answer is relatively simple and is the same as the above stated answers I will look at it from the Darwinist’s point of view. If the question refers to the first egg ever to show up in life then sens chickens have been around a relatively short period of time compared to life and the chicken was not the first animal to lay an egg then the answer would have to be the egg came first.

Now the last point of view that I will cover is the egg and the chicken came to be at the same time. Now how can that happen? The idea is that as soon as the embryo changed in the egg from growing into the deferent type of bird and instead growing into a chicken the egg changed from being an egg for the deferent kind of bird into being a chicken egg, not the egg of a chicken but a chicken egg for it was housing the evolution of the first chicken. Therefore the egg and the chicken came into existence at the same time.