Skip to content

Commit

Permalink
Merge branch 'master' of github.com:avibryant/l1tf into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi Bryant committed Oct 17, 2012
2 parents 9b63288 + d9a757f commit 0eab78e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -7,4 +7,12 @@ Avi Bryant (Etsy) & Steven Noble (Shopify)

Usage:

change_point_xy_pairs = l1tf(y_values, lambda).points
```javascript
change_point_xy_pairs = l1tf(y_values, smoothness).points
```

Where smoothness ranges from 0.0 (do nothing) to 1.0 (reduce the line to 3 or so points).

The y_values are assumed to be evenly spaced.

License: MIT
4 changes: 2 additions & 2 deletions example/aapl.html
Expand Up @@ -21,7 +21,7 @@

function render() {
$("#examples").empty()
var data = orig.concat(l1tf(array, 250000 * $("#range").val()).points.map(function(ary){return {x:ary[0],y:ary[1],set:'f'}}))
var data = orig.concat(l1tf(array,$("#range").val()).points.map(function(ary){return {x:ary[0],y:ary[1],set:'f'}}))
linechart.render(600, 400, d3.select('#examples'), data)
}
$(document).ready(function() {
Expand All @@ -31,7 +31,7 @@
</script>
</head>
<body>
<input id="range" type="range" min="0.01" max="0.99" value="0.1"/>
<input id="range" type="range" min="0.01" max="0.99" step="0.001" value="0.01" style="width:600px"/>
<div id='examples'></div>
</body>
</html>
Expand Down
2 changes: 1 addition & 1 deletion example/snp500.html
Expand Up @@ -21,7 +21,7 @@

function render() {
$("#examples").empty()
var data = orig.concat(l1tf(array, 5000 * $("#range").val()).points.map(function(ary){return {x:ary[0],y:ary[1],set:'f'}}))
var data = orig.concat(l1tf(array, $("#range").val()).points.map(function(ary){return {x:ary[0],y:ary[1],set:'f'}}))
linechart.render(600, 400, d3.select('#examples'), data)
}
$(document).ready(function() {
Expand Down
38 changes: 30 additions & 8 deletions l1tf.js
Expand Up @@ -41,12 +41,18 @@ var l1tf = (function() {
this.baseErr = this.computeErr(0,0)
var linDy = 0

if(this.prev && this.next)
// if(this.prev && this.prev.prev){
// linDy = this.linearDy(this.prev.prev, this.prev)
// this.tryMove(linDy, 0, false)
// }
// if(this.next && this.next.next){
// linDy = this.linearDy(this.next, this.next.next)
// this.tryMove(linDy, 0, false)
// }
if(this.prev && this.next){
linDy = this.linearDy(this.prev, this.next)
else if(this.prev)
linDy = this.linearDy(this.prev.prev, this.prev)
else
linDy = this.linearDy(this.next, this.next.next)
this.tryMove(linDy, 0, true)
}

if(linDy == 0){
var scramble = 1
Expand All @@ -57,8 +63,6 @@ var l1tf = (function() {
this.tryMove(scramble / 2, 0, false)
this.tryMove(scramble / -2, 0, false)
this.tryMove(scramble / -4, 0, false)

this.tryMove(linDy, 0, this.next && this.prev)

var d2 = new Date()
this.opt.errTime += (d2 - d1)
Expand Down Expand Up @@ -277,7 +281,7 @@ var l1tf = (function() {
this.errDelta = 0
this.errTime = 0
this.real = array
this.m = m
this.m = this.maxLambda()*m

var prev = null
var first = null
Expand Down Expand Up @@ -308,6 +312,24 @@ var l1tf = (function() {
return Math.ceil(Math.log(this.count+1)/Math.log(2)) - 1
}

Optimizer.prototype.maxLambda = function() {
var n = this.real.length;
var xSum = (n - 1) * n / 2;
var x2Sum = (2*n - 1)* (n - 1) * n / 6
var ySum = 0
var yxSum = 0
this.real.forEach(function(y, x){ySum += y; yxSum += y*x})
var slope = (yxSum - ySum*xSum/n)/(x2Sum - xSum*xSum/n)
var constant = (ySum - xSum*slope)/n

var maxDiff = 0
for(var x = 0; x < n; x += 1){
maxDiff = Math.max(Math.abs(x*slope + constant - this.real[x]), maxDiff)
}

return maxDiff*6*n
}

Optimizer.prototype.append = function(point) {
var height = this.getHeight()
var capacity = Math.pow(2, height+1) - 1
Expand Down

0 comments on commit 0eab78e

Please sign in to comment.