xxxxxxxxxx
173
/* VARIABLES */
/* EDIT THESE */
let csv_file = 'NVDA_data.csv';
let column_num = 4; //starts at zero, so 3rd column = 2
let truncation_value = 0.5;
let download_images = true;
let show_text = true; //this will only work if download_images = true
let header = "NVDA_data";
let height = 1024;
let width = 1024;
let min_dist = 0.0;
let max_dist = 0.1;
let start_truncation = 0.0;
let min_trunc = 0.0;
let max_trunc = -0.25;
let start_vector = [0.1321460263303403,-0.017658108045331522,-0.017009001976869453,0.09694461755682256,0.07883845283356423,0.001142680848173927,0.009564564544465661,-0.017096572507426397,-0.00995297534101465,0.11024770752233481,-0.04822396079400155,0.08410709805496011,-0.042740235158478725,-0.05467609206292965,-0.0542955084710398,-0.06914951467655893,0.005621568931098796,0.0998405548648956,0.0167810526322867,0.001620556699093386,0.020533307925018212,-0.060524104555574304,-0.019634843426189097,-0.09799968041348153,-0.10599032053343632,-0.10748248132780291,0.12389557057201173,-0.12456946949299594,0.08745654522988651,-0.20433648912677166,-0.02484375542180768,0.03892085762427472,-0.012274755432136686,-0.020800100202646898,0.009087727136829893,0.16981952384183316,0.006469123910696234,-0.006035264284048915,0.10933246554046848,-0.029777627602015286,-0.0573459981725349,0.05175483262155303,-0.016792895046488843,0.09089344424731964,-0.1253902250118213,0.06690130595799387,-0.02608434802532985,0.07051404439029477,-0.017050834572082847,0.06172028617047573,0.0917978707395693,-0.08890673629979798,0.07351556294438441,-0.15062265960816712,-0.10941339216076015,0.15007115119319187,0.013539376780007953,-0.0017772366207779253,-0.010462988411940856,-0.008768193208769653,-0.002932430678519735,0.0131194499380242,0.02709357272432804,0.0503147380289756,-0.03835211656435743,-0.06400789029592201,-0.04180341771999227,0.10791477167480251,-0.11676610245596745,-0.020503914028491728,-0.05797738223602018,-0.031222808927923056,-0.0290481044890038,-0.07040130604516529,-0.0326806962615911,-0.03968711465679445,-0.10082288577018697,-0.1394530665440186,0.0564542748891605,0.09234862096020596,0.16185046041924186,-0.010984096683154978,0.025062818364582087,-0.07682779127861422,0.02286469667537864,-0.06353020035536372,-0.008250074298171888,0.04896658926594687,0.05083786016305235,0.06097650978101597,0.027259622273587334,0.03440728416876087,-0.059239440994470205,0.12681445246049905,0.016556808080754038,0.01478005997218117,-0.05516184532940101,0.00652909803308983,0.12607078956353882,-0.008354363582956485,0.008000662642322125,0.05740263440588973,0.006320731725038537,-0.17071661354094197,0.10243486822177174,-0.12499137416103484,0.1808054872884099,-0.0006948065729600779,-0.03568790105589375,0.030714143063366502,0.021116172251404716,-0.03246541642444826,-0.046339996436283504,-0.0447640824671707,-0.07190061526837431,0.0909298162537466,0.031558594422432196,-0.013385947036795083,-0.10278438246465757,0.044015942833301264,-0.0079482895757944,-0.08482549915874366,0.05156056916785532,0.09328675063339856,-0.02422837627968986,0.060898376105118654,0.004528015837912007,-0.16683499613797115,-0.0686875380975933,-0.002345181080371614,-0.055055011817161735,-0.11366779207305668,-0.091520488649868,-0.010531710578934698,0.06895541838972498,0.12322723233285479,0.14007537962829744,0.07741145369598357,-0.06426375534801912,0.05837046039745465,0.11927023603368936,0.05742023459027651,-0.12424806166247862,0.011746292375414864,-0.14338611710272053,0.14835920412303136,0.09731600335277067,-0.08433288211401331,0.016431969349277445,0.12782238994271808,-0.016029685170114084,-0.059104969582568945,0.06114973185905122,-0.13899181209741954,-0.01666510764077027,-0.02476360813749576,0.1951769739928652,-0.017820139596294055,0.04411000180828964,-0.014502583877882051,0.06447782977959265,0.08010061464386582,0.0645848733343316,0.06363587035015517,-0.028501887339346676,0.02006003426095271,-0.15125849756181942,0.0656077791598017,0.10617353761696559,0.21326011621609053,0.031239875748799106,0.005975235750546729,-0.03169691545018729,-0.0020384105449226114,0.17160692839906244,-0.05287388741738139,0.02844670245780684,0.1177596412032093,0.043166763185731635,0.03361252709910132,-0.006928581649746295,0.20595687317045075,-0.011171220011375809,-0.0009015541029425908,0.02774373539662621,0.017726661761329726,0.12159719582593498,-0.10907645313597031,0.054145391342194246,-0.09560541713243105,-0.09632005600385182,-0.046107204248790334,0.08217511830908047,-0.027236143169418338,0.0057847771888899155,0.12518307067016365,-0.013766634798506216,0.1240332711892673,-0.05195683712929211,0.20320216145366668,0.07332376434683609,0.08209753394408334,-0.08359892974100062,-0.04664588764802863,-0.1191322952123495,0.021914505044194996,-0.04204659437663635,-0.08557745606332699,0.09844456572277786,-0.08270856777760821,0.04661016152496312,0.00007917569212563882,-0.005997446517673505,0.10583341520088826,0.12321029033646184,-0.18237087483211142,-0.0034614506472894976,-0.016184779346748644,0.018254801365062093,-0.10655901144304862,0.11748175207982617,-0.017328388907489105,-0.06346277068638581,0.14197059774582355,-0.044673023945496944,0.11324843299955012,-0.01327774150903294,0.12831874511264318,0.031142584009928434,-0.031086195919535473,0.03407163184152462,-0.019373201982158722,-0.0645009890535985,0.05278100379573058,0.04819237870562673,-0.05402254564310509,0.013779119005905506,-0.056919607164086276,0.024033971669786044,-0.05143866130020408,0.04381843198771862,-0.10077212898151737,0.10005387053883782,-0.04268201892762215,-0.021177133051131,0.050359988737388776,-0.004060444524294368,0.13511355379414527,-0.1513754586198275,-0.09054419137147446,0.013578189936883353,-0.14002048364922895,0.07071309884740437,0.1369518297404612,0.0055595422680753785,0.10301169655246804,0.0653777593315102,-0.024631198194310693,-0.11711968874739674,-0.05305578907305521,-0.001133683345207321,-0.09554215514692742,0.06913096016216144,-0.15013049182717844,0.16807932388917887,0.038198850467515685,0.008896666892432376,-0.03213309297533688,0.04643158147655563,-0.08766922198340302,0.1254239998910694,0.04830881348327869,-0.05985086324171357,0.022844454532027623,0.08611160817783484,0.013438055316375628,0.06603316868610196,-0.05961234871190052,0.08910898267190759,-0.09393443404815426,0.04898783619754799,-0.040617730164445746,-0.14447711723983356,-0.02662530052340106,-0.08466265806824237,-0.01381391736794825,0.04203984785211549,-0.02794824140546754,-0.08626017081112161,-0.11253652760114691,-0.20495920517921748,-0.023902059183802365,0.0115918676855888,0.04217578368469874,-0.04148597093209386,0.056030085153003784,-0.1374319094162279,0.012567001904272401,0.018303567899521485,0.14016550554242094,0.11715668297575021,0.13976104729174996,0.04080344580620423,0.04572352750549837,-0.06133948928689923,-0.0006202656738931826,-0.04248713759466734,0.06462422075767024,0.012794531729928511,-0.08394956109323544,0.12931501836557635,-0.09189097233950094,0.05620877774545524,0.1976721896944969,0.12292780646690224,0.046040532345050865,-0.01488758259382467,0.06166392346672212,-0.010291908580779538,0.028073877655586096,-0.12354518093959702,0.1411491047373491,0.13090072062973984,-0.021376583838250814,-0.05346394957056218,0.08991115771130241,-0.03578326886478153,0.005545137114638152,0.08226028697551535,0.16035717103882152,-0.02203974131873167,-0.05057858971672443,0.012278871328652088,0.0008737661594830299,0.1265012968398666,0.045623292885922234,0.012386385065651137,0.009487505949338868,0.01972524755653253,0.14833708335795778,0.0836906592039373,-0.06703005705779232,0.06903770137085061,-0.048370400564625035,-0.12243985756694115,0.09892354217196483,-0.0679580023997711,-0.12281096314716913,0.07728507394492914,0.12514479216976432,-0.03503700376575096,0.12933597129793165,-0.04011356966388535,-0.10023111493323926,-0.0072641728876837695,0.090001461804531,0.08839661308029291,0.08134003447696216,0.012063441628599292,0.07493456746633162,0.04653585497022582,0.0000368702955324679,0.03101062224641762,-0.03575085409585033,0.06124966177908689,-0.06980532584329147,0.0522376155760878,0.14545806071141512,-0.044929793559644604,0.013972475667749208,0.17787578449462998,0.0384600442365987,-0.07831644615944514,0.11465138036793243,-0.026325125972765238,0.0576464862633067,0.13033760002965414,0.13800976079458704,0.026828034461038434,0.01936817637200018,0.1181722038328255,0.009614718277804642,0.058920432659798516,0.00833829962287696,-0.08251019346493071,-0.04128239456140652,-0.010578523788332284,-0.0949742000979356,-0.01587063702073211,0.005291069400827311,0.043964800715072055,-0.0637093223915138,0.1009802817955918,0.01716051552082018,-0.17096343251455734,0.024370768025069288,0.023831976881358283,0.0644096689340045,-0.04621970882088841,0.06513686587098245,0.01788125599067758,-0.05782175219812625,-0.0528160864265956,0.10138798718349032,0.029767622202311564,-0.04429642322481468,-0.04212478947522994,-0.04236444270991327,-0.03626918880145997,-0.12173869094896798,0.017971524035817584,0.005597476800392735,-0.004912203449260709,0.11273129587849517,-0.08851226775599799,-0.06471195873822026,0.023048407191052476,0.018864000129256063,0.003279239793136629,-0.17130365253568205,-0.10171282090993598,0.005493193260503701,-0.03189489954163922,-0.05476957748770588,-0.04008439611748397,0.1928913557416554,0.11560156314886469,0.017138110562135746,0.02541409450788981,0.0540893587891107,-0.12638257721607316,0.0745268675924901,-0.08911252839963452,0.025150239172805802,0.007983125444288603,-0.014427466998582238,0.12463765934528297,0.01827908788328268,0.1004669525804005,-0.030529535481055012,-0.04865372220888409,-0.05760825218373444,-0.059941268126545925,-0.11274106540500274,-0.062456912302571055,-0.11678623888456549,0.1263157152983577,-0.08101987955074123,0.024821588134537748,0.10748195658468934,0.028014878855028562,-0.11109097651787922,0.08020936870267424,-0.04666418654972915,0.02313980387266652,-0.008175407530502655,-0.06980833102215724,-0.15310342857535109,0.06929705197395591,0.08291443739650865,-0.09755018965692991,0.03649117701691336,-0.02802815421943737,-0.008082693616497105,-0.05107991531445577,0.02977705245810171,0.0294447181828413,-0.12470873203198182,-0.011555579372131995,0.10082070108803551,-0.028303990843974405,-0.005128831530450715,-0.11361366974030439,0.104183847233903,-0.020253222634287433,0.028233612749748047,-0.017211365941708714,0.05595449156588419,0.25258971960762844,-0.0345115595381289,0.10787428232446972,0.1660193951301353,0.06208276594119342,-0.024082004663138395,-0.06202135793291755,-0.058240805906460184,0.1462913275844729,0.02636975930255868,-0.026266916285469514,-0.04549759393203269,-0.04691467295463976,-0.042209188853010296,-0.09007750601020155,0.07184142411176779,-0.0401505281629015,-0.011223291445287262,0.12173542745288521,0.08480595994754801,0.02700241798360102,-0.11999870349603306,0.060122180596594986,0.12192399983128592,0.07427893518739191,-0.10089412494955959,-0.013956263307946207,-0.06653944203450064,0.0571400634019213,-0.15787983024232286,-0.05508381504024968,-0.015851820643648033,-0.11551511705617035,0.1294704866063841];
/*----------------------------*/
/* No need to edit below here */
let table;
let stock_data;
let data_min, data_max;
let vectors_out = [];
let trunc_out = [];
let total_frame_count;
let v1;
function preload() {
table = loadTable(csv_file, 'csv', 'header');
}
function setup() {
pixelDensity(1);
createCanvas(width, height);
textAlign(LEFT, TOP);
textSize(40);
// debug_csv() //uncomment this if you want to look at the data
stock_data = get_csv_column_diff_data(column_num);
stock_data_abs = get_abs(stock_data)
total_frame_count = stock_data.length;
data_min_abs = Math.min(stock_data_abs);
data_max_abs = Math.max(stock_data_abs);
data_min = Math.min(stock_data);
data_max = Math.max(stock_data);
// print(nj_uniform_random(1).tolist())
// print(data_min)
// print(data_max)
// print(data_min_abs)
// print(data_max_abs)
//convert vectors to numjs format
v1 = nj.array(start_vector);
t1 = start_truncation;
vectors_out.push(v1);
trunc_out.push(t1);
for(let d = 0; d < stock_data.length; d++) {
//get start vector
v = vectors_out[d];
t = trunc_out[d];
//calculate new vector (change in absolute)
let diff = stock_data_abs[d]
//convert price difference to distance change
let vmap = map(diff,data_min_abs,data_max_abs,min_dist,max_dist)
vectors_out.push(v.add(nj_uniform_random(vmap)))
//calculate truncation
let tdiff = stock_data[d]
if(tdiff <=0){
let tmap = map(tdiff,0,data_max,min_trunc,max_trunc)
trunc_out.push(t+tmap)
} else {
let tmap = map(tdiff,data_min,0,-max_trunc,-min_trunc)
trunc_out.push(t+tmap)
}
}
generateInterpolation();
}
function get_csv_column_diff_data(column_counter){
//cycle through the table, get closing price
data = [];
for (let r = 1; r < table.getRowCount(); r++) {
let diff = (table.getString(r-1, column_counter)) - (table.getString(r, column_counter))
data.push(diff);
}
return data
}
function get_abs(data_in) {
data_out = [];
for(let d = 0; d < data_in.length; d++){
data_out.push(Math.abs(data_in[d]))
}
return data_out;
}
function debug_csv(){
//count the columns
print(table.getRowCount() + ' total rows in table');
print(table.getColumnCount() + ' total columns in table');
//cycle through the table
for (let r = 0; r < table.getRowCount(); r++) {
for (let c = 0; c < table.getColumnCount(); c++) {
print(table.getString(r, c));
}
}
}
function generateInterpolation() {
a = vectors_out[count].tolist();
t = trunc_out[count];
// if(count > 0) {
// print('movement: ' + stock_data[count-1])
// print('current truncation: ' + t)
// }
const path = "http://localhost:8001/query";
const data = {
z: a,
truncation: t
};
httpPost(path, 'json', data, gotImage, gotError);
}
function gotError(error) {
console.error(error);
}
function gotImage(result) {
i = createImg(result.image, "", 'anonymous', imageReady);
i.hide();
}
let count = 0;
function imageReady() {
image(i, 0, 0);
fill(0);
text(header+".csv", 40 ,40);
text(table.getString(count, 0),40,80)
text("Price: $" + table.getString(count, 4),40,120)
// if (count > 0){
// text("change since "+ table.getString(count-1, 0) + ": ",40,120)
// text(" " + stock_data[count-1],40,160)
// }
if (download_images) save(`step${nf(count, 4)}`);
count++;
if (count <= total_frame_count) {
print('generating image ' + (count) +'/'+ total_frame_count)
setTimeout(generateInterpolation, 100);
}
}
function nj_uniform_random(dist) {
return nj.clip(nj.random(512).subtract(.5).multiply(2*dist),-1.0,1.0)
}
function njlerp(v0, v1, t) {
return v0.multiply(1-t).add(v1.multiply(t));
}