/* version 2
Slightly more useful for unimportant guesstimations.
This version is able to retrieve the data found in the dataset if the user predict()s a value that happens to be there.
Otherwise, it will assume similar to version 1 but more satisfactorily: getting it from 3 scopes: greater than the defined range, less than the defined range, and within the defined range.
*/
/* ideas it's based off it
2004 - 2010 // time range
100 - 400 // value range
300/6 = 50
2011 => 450
diff1 = max1-min1 // time
diff2 = max2-min2 // val
linearYear = diff1 / diff2
f(time)
|
V
(time > max1)
max2 + ((time-max1) * linearYear)
(time < min1)
min2 - ((min1-time) * linearYear)
else
x = binarySearchFor(time)
if x == null
// value not in defined dataset, interpolate linearly from minimum
min2 + ((time-min1) * linearYear)
else
return x // actually found value in defined dataset
*/
var data = [{year:1992,cost:1440},
{year:1993,cost:1560},
{year:1994,cost:1620},
{year:1995,cost:1780},
{year:1996,cost:1860},
{year:1997,cost:1920},
{year:1998,cost:1940},
{year:1999,cost:1999},
{year:2000,cost:2040}];
var data2 = [];
for(var xint = 0;xint < 10;xint++)
data2.push({year:2000+xint,pay:10+(xint/10)});
// then do one for #pets compared to #renkou ppl in household
// data is the cost of a used car with x miles on it in a certain year; goal: given a year, predict the cost that year
// data2 is hourly pay each year starting 2000 $10.00/hr. Goal: given a year, predict the pay that year
// where timeName is the variable per object with the time value (the consistent change over time)
// and valueName is the value we're trying to find given a time
// .predict(v): returns a predicted linear output
function reader(data, timeName, valueName){
this.times = [];
this.values = [];
this.minT = 1; // minimum time
this.maxT = 1;
this.minV = 1; // minimum value
this.maxV = 1;
this.diffT = 1;
this.diffV = 1;
this.perYear = 1; // final linear quotient
// now let's try to make a mini model
this.minT = data[0][timeName];
this.maxT = data[0][timeName];
this.minV = data[0][valueName];
this.maxV = data[0][valueName];
for(var xint = 0;xint < data.length;xint++){
this.minT = Math.min(this.minT, data[xint][timeName]);
this.maxT = Math.max(this.maxT, data[xint][timeName]);
this.minV = Math.min(this.minV, data[xint][valueName]);
this.maxV = Math.max(this.maxV, data[xint][valueName]);
this.times.push(data[xint][timeName]);
this.values.push(data[xint][valueName]);
}
this.diffT = Math.max(1,this.maxT - this.minT);
this.diffV = Math.max(1,this.maxV - this.minV);
this.perYear = this.diffV / this.diffT; // cost per year linear avg
this.predict = function(time){
if(time > this.maxT){
return this.maxV + ((time-this.maxT) * this.perYear);
}else if(time < this.minT){
return this.minV - ((this.minT-time) * this.perYear);
}else{ // within range
var found = false;
var val = 0;
for(var xint = 0;xint < this.times.length;xint++){
if(this.times[xint] == time){
found = true;
val = this.values[xint];
break;
}
}
if(found){
return val; // found in defined dataset
}else{
return this.minV + ((time-this.minT) * this.perYear); // estimate
}
}
}
}
var r1 = new reader(data, 'year', 'cost');
console.log(r1.predict(1992));
var r2 = new reader(data2, 'year', 'pay');
console.log(r2.predict(2001));
Sunday, April 9, 2023
Coding Challenge #46 Number Predicter - Version 2
Subscribe to:
Post Comments (Atom)
Coding Challenge #54 C++ int to std::string (no stringstream or to_string())
Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...
-
I NEEDS MY JAVASCRIPT PLEASE :) Open txtcode .txt Save as .gct Please click the blue cheat code title to activate it, a...
-
Naruto Personality Quiz Please enable javascript ☯ Which Naruto Character Are You?
-
Useful for extensions that want to prevent event listeners from being attached or modify prototypes or functions, generally to defeat click...
No comments:
Post a Comment