comparison gtc/gtc.js @ 101:2b010bd970c2

deer_crash: correction: it was actually more than *three* years ago
author paulo
date Wed, 29 Aug 2018 23:28:09 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4110712b58d9
1 var myChart;
2 var myData;
3 var showCorrBtn = document.getElementById('showcorr');
4
5 nv.addGraph(function() {
6 myChart = nv.models.scatterChart()
7 .showLegend(false)
8 .showDistX(true)
9 .showDistY(true)
10 .xDomain([-1, 1])
11 .yDomain([-1, 1])
12 .duration(350)
13 .color(d3.scale.category10().range());
14 myChart.tooltip.contentGenerator(function(data) {
15 return '(' + data.point.x.toFixed(2) + ', ' + data.point.y.toFixed(2) + ')';
16 });
17
18 myChart.xAxis.tickFormat(d3.format('.02f'));
19 myChart.yAxis.tickFormat(d3.format('.02f'));
20
21 reload();
22
23 return myChart;
24 });
25
26
27 function randomData(groups, points) {
28 var data = [],
29 shapes = ['circle', 'cross', 'triangle-up', 'triangle-down', 'diamond', 'square'],
30 random = d3.random.normal();
31
32 for (i = 0; i < groups; i++) {
33 data.push({
34 key: 'Group ' + i,
35 values: []
36 });
37
38 for (j = 0; j < points; j++) {
39 data[i].values.push({
40 x: random(),
41 y: random(),
42 size: Math.random(),
43 shape: (Math.random() > 0.95) ? shapes[j % 6] : 'circle'
44 });
45 }
46 }
47
48 return data;
49 }
50
51 function myDataGen() {
52 var data = [];
53 var values = [];
54 var n = 25;
55 var m = 2*Math.random() - 1.0;
56 var dv = Math.random();
57 var normal = d3.random.normal(0.0, dv)
58
59 for (i = 0; i < n; i++) {
60 var x = i/n + 0.5*normal() - 0.5;
61 var y = m*x + normal();
62 values.push({
63 x: x,
64 y: y,
65 size: 2.0,
66 shape: 'circle',
67 });
68 }
69
70 data.push({
71 //key: 'Points',
72 values: values,
73 //slope: m,
74 //intercept: 0.001,
75 })
76
77 return data;
78 }
79
80 function corr(values) {
81 var x_a = [];
82 var y_a = [];
83
84 for (i = 0; i < values.length; i++) {
85 x_a.push(values[i].x);
86 y_a.push(values[i].y);
87 }
88
89 return ss.sampleCorrelation(x_a, y_a);
90 }
91
92 function lr(values) {
93 var x_y = [];
94
95 for (i = 0; i < values.length; i++) {
96 x_y.push([values[i].x, values[i].y]);
97 }
98
99 return ss.linearRegression(x_y);
100 }
101
102 function print_corr() {
103 console.log('corr = ' + corr(myData[0].values))
104 }
105
106 function print_lr() {
107 var lro = lr(myData[0].values);
108 console.log('m = ' + lro.m);
109 console.log('b = ' + lro.b);
110 }
111
112 function add_lr(lro, data) {
113 data.push({
114 values: [],
115 slope: lro.m,
116 intercept: lro.b,
117 })
118 }
119
120 function show_corr() {
121 var ans = corr(myData[0].values);
122 showCorrBtn.textContent = ans.toFixed(2);
123 showCorrBtn.onclick = reload;
124 }
125
126 function reload() {
127 myData = myDataGen();
128 add_lr(lr(myData[0].values), myData);
129 showCorrBtn.disabled = false;
130 showCorrBtn.textContent = 'Show Correlation';
131 showCorrBtn.onclick = show_corr;
132
133 d3.select('#chart svg')
134 .style('height', '95%')
135 .datum(myData)
136 .call(myChart);
137
138 nv.utils.windowResize(myChart.update);
139 }