Skip to content

Instantly share code, notes, and snippets.

@anvius
Last active February 19, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anvius/01c33527518760f05cba18af9b5fa7c8 to your computer and use it in GitHub Desktop.
Save anvius/01c33527518760f05cba18af9b5fa7c8 to your computer and use it in GitHub Desktop.
/**
* Linear regression in Javascript for domains value
* 2016, Antonio Villamarin
*
* github.com/anvius
* @anvius
*
* License GPL
*
* Clean code === No Comments
*
* y = a + bx => x = (y - a) / b
*
* ((cantidad_de_dominios * xy) - (x * y))
* b = ---------------------------------------
* ((cantidad_de_dominios * xx) - (x * x))
*
* (y - (b * x))
* a = --------------------
* cantidad_de_dominios
*
* En los arrays hay que poner los datos que tenemos. Tambiéns e pueden importar de un csv.
*
* En el caso de las búsquedas, poner las búsquedas exactas del dominio a tasar por búsquedas.
*
* La mejor opción no es usar búsquedas, sino una combinación de búsquedas y competencia. Recomiendo log_10(B)*(0,5+Competencia) como índice.
*
* Los datos de dominios no son reales, sino aleatorios.
*
*/
var
precios_de_dominios = [
129, 189, 255, 414, 540, 660
],
busquedas_de_keywords = [
90, 90, 210, 540, 660, 910, 22200
],
x = y = xy = xx = a = b = resultado = 0,
cantidad_de_dominios = precios_de_dominios.length,
busquedas_a_tasar = 10000;
for (i = 0; i < cantidad_de_dominios; i++) {
x += precios_de_dominios[i];
y += busquedas_de_keywords[i];
xy += precios_de_dominios[i]*busquedas_de_keywords[i];
xx += precios_de_dominios[i]*precios_de_dominios[i];
}
b = ((cantidad_de_dominios * xy) - (x * y)) / ((cantidad_de_dominios * xx) - (x * x));
a = (y - (b * x)) / cantidad_de_dominios;
if(b != 0) {
resultado = (busquedas_a_tasar - a) / b;
console.log('El valor según las búsquedas es: ' + Math.round(resultado / 100) * 100 + "\n");
} else {
console.log('No se puede calcular, el valor de b es cero.' + "\n");
}
@anvius
Copy link
Author

anvius commented Feb 19, 2017

Modificación genérica Wikipedia

/**
 * Linear regression in Javascript
 * 
 * (c) 2016, Antonio Villamarin
 * 
 * github.com/anvius
 * @anvius
 * 
 * License GPL
 * 
 * Clean code === No Comments
 * 
 * y = a + bx <=> x = (y - a) / b
 * 
 *     ((cantidad * xy) - (x * y))
 * b = ---------------------------
 *     ((cantidad * xx) - (x * x))
 * 
 *      (y - (b * x))
 * a = ---------------
 *        cantidad
 * 
 */

var
      xarray = [
            1, 2, 3, 4, 5
      ],
      yarray = [
            5, 5, 5, 6.8, 9
      ],
      x = y = xy = xx = a = b = resultado = 0,
      cantidad = xarray.length,
      futuro = 100;
      
for (i = 0; i < cantidad; i++) {
      console.log('Dado ' + xarray[i] + ' => ' + yarray[i]);
      x += xarray[i];
      y += yarray[i];
      xy += xarray[i]*yarray[i];
      xx += xarray[i]*xarray[i];
}

b = ((cantidad * xy) - (x * y)) / ((cantidad * xx) - (x * x));

a = (y - (b * x)) / cantidad;

if(b != 0) {
      console.log('Dado ' + futuro + ' => ' + Math.round(a + (b * futuro)));
} else {
      console.log('Dado ' + futuro + ' => Infinito');
}

/**
 * Resultado:
 *
 * Dado 1 => 5
 * Dado 2 => 5
 * Dado 3 => 5
 * Dado 4 => 6.8
 * Dado 5 => 9
 * Dado 100 => 101
 **/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment