lunes, 4 de agosto de 2008

Benford´s law


Browsing through the web thanks to my good and favorite program Stumble Upon(http://www.stumbleupon.com/) I found a very interesting webpage explaining the Benford´s law.


Benford's law, also called the first-digit law, states that in lists of numbers from many real-life sources of data, the leading digit is distributed in a specific, non-uniform way(wikipedia.com).


It is in this wepage http://www.kirix.com/blog/2008/07/22/fun-and-fraud-detection-with-benfords-law/ in which I found a video that explained it and showed some examples proving the law.


Thanks to a friend of mine which happened to had some free time at work, he tried to prove it filling an excel list with the population in our state(Jalisco, MEX). It is quite impressing, check the graph.

(courtesy of my friend Guillermo Vidales)


This law explains that if you take out the first digit of any number in a list, the probability of number one appearing in the list would be the following:


1= 30.10%
2= 17.60%
3= 12.50%
4= 9.70%
5= 7.90%
6= 6.70%
7= 5.80%
8= 5.10%
9= 4.60%

For example. If you got in a list the number 1,223 2,343 6,343 3,34333 , you first have to take out the first digit and it would give you this: 1 , 2 , 6 , 3 Now that you took out all the first digits, count how many 1´s ,2´s and so on, are in the list and divide them by the size of your list. The result will amaze you because it follows the Benford´s law.

IMPORTANT: It will only follow the Benford´s law if and only if the list numbers behave in a natural way. For example, the population in any given city, country, state etc.. or the sizes of the states in any given country, stock market etc.. All numbers which are not previously assigned.

In the video they show that this law helped a company detect a fraud by just analizing the data of the money that was taken out of the company.

if you want the excel list to check it out and test it yourself, just email me.

lunes, 30 de junio de 2008

Robot Vision

This was a very interesting project. We developed it in Denmark. We needed to program an ABB robot and a DVT camera to identify some products and then choose which one to pick up and store.
We were 5 mexicans working in the camera and 5 Danes working in the robot. This video is when we tested the communication between both parts. It took us a while, the robot wasnt receiving any data and after 5-6 hours we found out there was a problem with the net cable. So we changed it and it worked!!




Cap Linning Machine

This is by far, the biggest project I've ever made. This was my final project in my career and it took me a little bit more than a year to complete it. I made it with aproximately 3,000 USD and it was all constructed by me. This machine gives around 35 linned caps per minute.
The hardest part of all was to test it. There are lots of variables to adjust and rebuild and redesign. You actually have to design it so it can last quite a few years and vibrations, weather conditions, quality in the cap and the linner can affect a lot in the well functioning of the machine. You have to build it so it can stand certain tolerances in all those matters.






Inverted Pendulum PI

This was a 4th semester project for Control class in which we made a Poportinal - integral control for a pendulum. This, we made it with Operational amplifiers (OPAMPs). The construction was very basic, we used a non-functional printer so we just adapted a potentiometer to the ink car and put a stick to it. The limitations were that the printer rail was to short so if the pendulum was very hard hitten it would not give enough chance to control the pendulum. Because of the limitations of the motor as well we coulnt increment the gains so the response was faster.

miércoles, 30 de enero de 2008

Alphabetical sort algorithm

This is an algorithm I made to alphabetically sort names and sort numbers. I tried searching in the internet for something like this to sort names alphabetically, but coulnt find it, or didnt look well. What I came up with, is that I knew there existed algorithms to sort numbers so I took the more efficient 2^n algorithm and adapted to sort letters. This is the shell algorithm! Becuase I used Visual C# is kind of easy when converting from string to int's and so on so I didnt have much problems. What I needed was to sort rows contained in a Datarow [ ] alphabetically, so the function takes the rows in Entrada, also takes which column of the row is going to read so that it is compared with the others(int posicion), also because I used Indexes in my tables, sometimes in my rows there were only indexes pointing to other tables which contained the names, so I put bool ligar which if true, means they are indexes and have to search in other tables, then also takes Relacion, which if ligar=true then relacion must contain the name of the relationship to the other table, so that it knows in which table the name is contained, and at last, I need to sort also by numerical order, so if it is true then it forgets all my adaptations and just uses the shell algorithm. It is quite simple, it looks quite messy but it just takes the word and each letter assignes it a numer, so name "a" will become (1)x27^5 if name "ab" will be (1)x27^5+(2)x27^4 and if name"abc" will be (1)x27^5+(2)x27^4+(3)x27^3 and so forth. The limitations of my algorithm is that it only compares the first 5 letters of the name. I think that is more than enough for my case, if you need more then just give bigger numbers to the exponent. Instead of 5 in my case, you could give 9 or 10, as you wish, the only problem would be you'll use numbers pretty big, bigger than a 32bit INT.
Hope it helps... enjoy...


Visual c# code:
DataRow[] regresaLugarAlfabetico(DataRow[] Entrada, int PosicionTexto,bool ligar, string relacion, bool numero)
{
//algoritmo shell
DataRow auxiliar;
int salto = Entrada.Length / 2;
int j, k;
int pasadas = 0;
while (salto > 0)
{
for (int i = salto; i < j =" i">= 0)
{
k = j + salto;
int exp = 1;
UInt32 uno = 0;
UInt32 dos = 0;
int charcomp = 6;
double Base = 27.0; //

if (numero == false)
{
string ej;
string ek;
if (ligar == false)
{
ej = Entrada[j][PosicionTexto].ToString();
ek = Entrada[k][PosicionTexto].ToString();
}
else
{
DataRow drj = Entrada[j];
DataRow drk = Entrada[k];
DataRow Papadrj = drj.GetParentRow(relacion);
DataRow Papadrk = drk.GetParentRow(relacion);
ej = Papadrj[PosicionTexto].ToString();// Entrada[j][PosicionTexto].ToString();
ek = Papadrk[PosicionTexto].ToString();// Entrada[k][PosicionTexto].ToString();
}
while (exp < charcomp)//hasta 5 caracteres comparaa y depende del int32 para aumentar a mas;
{
if (ej.Length >= exp && (int)ej[0] > 96 && (int)ej[0] < 123)//"a-z";
{
uint n = ((uint)ej[exp - 1]) - 96;
double p = System.Math.Pow(Base, (charcomp - exp));
uno = uno + (n) * Convert.ToUInt32(p);
}
else if (ej.Length >= exp && (int)ej[0] > 64 && (int)ej[0] < 91) //"A-Z";
{
uint n = ((uint)ej[exp - 1] - 64);
double p = System.Math.Pow(Base, (charcomp - exp));
uno = uno + (n) * Convert.ToUInt32(p);
}
if (ek.Length >= exp && (int)ek[0] > 96 && (int)ek[0] < 123)//"a-z"
{
uint n = ((uint)ek[exp - 1]) - 96;
double p = System.Math.Pow(Base, (charcomp - exp));
dos = dos + (n) * Convert.ToUInt32(p);
}
else if (ek.Length >= exp && (int)ek[0] > 64 && (int)ek[0] < 91)//"A-Z"
{
uint n = ((uint)ek[exp - 1] - 64);
double p = System.Math.Pow(Base, (charcomp - exp));
dos = dos + (n) * Convert.ToUInt32(p);
}
exp++;
if (ek.Length < exp && ej.Length < exp) { break; }
} }
else
{
uno = Convert.ToUInt32(Convert.ToDouble(Entrada[j][2])*1000);
dos = Convert.ToUInt32(Convert.ToDouble(Entrada[k][2])*1000);
}
pasadas++;
if (uno <= dos)
{
j = -1;
}
else
{
auxiliar = Entrada[j];
Entrada[j] = Entrada[k];
Entrada[k] = auxiliar;
j = j - salto;
}} }
salto = salto / 2;
//pasadas++;
}
return Entrada;
}

viernes, 26 de octubre de 2007

Sun position over earth

Several months ago, I was thinking in a very special gift to a very special person and I came up with a great idea (at least for me it still is). I am a mechatronic engineer and I love to create and invent stuff. All regarding physics and the understanding of how the reality works really passionates me.
What I came up with, was to make some kind of structure that could only do certain things ONLY in an specific day and time of the year. I wanted to do something like the Mayans did in Chichen Itzá which was to show a serpent crowling over the pyramid in an specific day of the year. My idea was to show an image and with the use of electronics and some mechanics to perform somthing symbolic about that day. Also, I wanted to be 100% energy self-sufficient(no batteries or any other source besides the sunlight). The hardest part was to do the calculations for getting the exact angle at which the sun´s beams hit the earth for a specific date and time. I started reading a lot and found a great webpage where all symplifications where made and you just only needed to understand what you were using. This is the page where I got most of my calculations. http://answers.google.com/answers/threadview?id=782886
There are lots of pages which you just need to choose the day and the coordinates of the location and they´ll give you the angles you need, but I didnt want the answer just that simple so I created my own program (The position coordinates I got them from Google earth).
Because of several reasons, I didnt finish my special gift. I made the structure and havent tested yet because the day hasnt arrived yet. I made it so that it could work at a specific time and in two different cities.
I still think is a great gift and what should do is to finish it! Not for anyone but for me. I think the only one who can appreciate it and get something more than giving away is me. Still have to work in my electro-mechanic system so that I can amaze myself and I´ll change the day to my birthday so that nature has a material gift for me. =D

domingo, 21 de octubre de 2007

Random average ¿?

One day, I just thought of what the average of n numbers would give. What I thought first was that there would be no constant average thus there were random numbers, but when I did the experiment I prove myself to be wrong. I still dont know exactly the reason, but as I remember of my probability course it had to be something regarding all possibilities contained in the gauss bell.
So, the list of random numbers where possibilites not repeated inside the bell. So the Average had to be the middle of the bell; 0.5
It is a interesting thing I guess, you can try it! Just fill out n random numbers in excel and take the average of it and you'll see!!

Talking to a friend of the experiment, he found it quite courious. He then sujested me to try it with PI decimals. It bump me again the couriousity and I made a simple C++ program to see what was the result. I first needed to find a quite big amount of PI decimals(which is also of great interest of me, but still havent do it) so I downloaded a list from this page http://www.uoguelph.ca/zoology/devobio/210labs/MeiosisQuiz/pi.html which has about 100,000 decimals.
I didnt like quite much the result. I got as a result 4.49 and with a 2.87 variance. I guess if more PI numbers are introduced the average would tend to 5, but not quite sure!

With the help of my friend, he made used the Matematica software and created the following graph using 1000,000 digits of PI.This proves that it tends to 4.5 which if you analize it, it is the mean of 0-9 not 1-9(which is 5).

If you want to try it, here is the code.
(oops the variables are in spanish! sorry..!!)
mhmmmm couldnt upload the code,, if you need it,, i'll send it to you!
sorry!