Saturday, April 8, 2023

Coding Challenge #45 Number Predicter - Version 1

 /* version 1
Simplest version, creates a value per the year and predict() returns that value * the year.
Comically inaccurate, can't even properly return values found in the dataset handed to it.
*/

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.d = data;
    this.min = 0; // minimum
    this.max = this.min;
    this.avgVal = 0;
    this.avgTime = 0;
    this.perYear = 0; // final linear quotient
    
    // now let's try to make a mini model using simple proportionalism
    var totalVal = 0;
    var totalYear = 0;
    this.min = data[0][timeName];
    for(var xint = 0;xint < data.length;xint++){
        totalVal += data[xint][valueName];
        totalYear += data[xint][timeName];
        this.min = Math.min(this.min, data[xint][timeName]);
        this.max = Math.max(this.max, data[xint][timeName]);
    }
    this.avgVal = data.length > 0 ? totalVal / data.length : 1;
    this.avgTime = data.length > 0 ? totalYear / data.length : 1;
    this.perYear = this.avgVal / this.min;
    
    this.predict = function(time){
        return time * this.perYear;
    }
}

var r1 = new reader(data, 'year', 'cost');
console.log(r1.predict(1992));

var r2 = new reader(data2, 'year', 'pay');
console.log(r2.predict(2001));

No comments:

Post a Comment

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 ...