-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.js
89 lines (80 loc) · 3.19 KB
/
script2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function generate(){
let buttonsToGenerate = 100
var alpha = 1
for(let i = 0; i < math.sqrt(buttonsToGenerate); i++){
var row = document.createElement("div")
row.setAttribute("id", "row"+i)
document.getElementById('button-container').appendChild(row);
for(let j = 0; j < math.sqrt(buttonsToGenerate); j++){
var btn = document.createElement("button");
btn.setAttribute("id", "button"+i+j)
btn.setAttribute("class", "gridbutts")
btn.style.backgroundColor= "revert"
btn.addEventListener("click", function(evt){
var x = i
var y = j
console.log("x: "+x + "\ny: "+y)
indarr = [x,y]
bigmat = incr(indarr, bigmat, 10)
})
var t = document.createTextNode(""+i+j);
btn.appendChild(t);
//
row.appendChild(btn)
}
}
function multinormal(meanvec, cov, x) {
k = x._size
topboi = math.multiply(math.subtract(x, meanvec), math.multiply(math.inv(cov), math.subtract(x, meanvec)))
dens = math.exp((-1 / 2) * topboi) / (math.sqrt((2 * 3.14159) ^ k * math.det(cov)))
return dens
}
function softmax(arr) {
return arr.map(function (value, index) {
return Math.exp(value) / arr.map(function (y /*value*/) { return Math.exp(y) }).reduce(function (a, b) { return a + b })
})
}
function incr(point,matrix,beta){
var covmat = math.matrix([[ 1.0, 0.0],[0.0, 1.0]])
var row = point[0]
var matrcopy = []
console.log(matrix._size)
l = matrix._size[0]
pointmat = math.matrix(point)
matrix = matrix._data
//change all values according to multigauss schema
for(let i=0; i<l;i++){
rowparam = math.add(math.log(matrix[i]),alpha)
for(let j=0;j<l;j++){
rowparam[j] += beta*multinormal(pointmat, covmat, math.matrix([i,j]))
}
rown = softmax(rowparam)
matrcopy[i] = rown
}
console.log("sdfasd")
console.log(math.matrix(matrcopy))
return math.matrix(matrcopy)
}
function decr(point, matrix, beta) {
var row = point[0]
matrix = matrix._data
console.log(matrix)
rowparam = math.add(math.log(matrix[row]), alpha)
rowparam[point[1]] -= beta
rown = softmax(rowparam)
matrix[row] = rown
console.log(matrix)
return matrix
}
// Fork & examples for the one-line version by @vladimir-ivanov:
//let softmax = (arr) => (index) => Math.exp(arr[index]) / arr.map(y => Math.exp(y)).reduce((a, b) => a + b);
//
// Also see comments for improvements
function softmax(arr) {
return arr.map(function (value, index) {
return Math.exp(value) / arr.map(function (y /*value*/) {
return Math.exp(y)
}).reduce(function (a, b) { return a + b })
})
}
}