Skip to content

Instantly share code, notes, and snippets.

@ariaz
Created December 7, 2013 05:28
Show Gist options
  • Save ariaz/7837615 to your computer and use it in GitHub Desktop.
Save ariaz/7837615 to your computer and use it in GitHub Desktop.
3D-Sigmoid
{"description":"3D-Sigmoid","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/8RTZgZu.png"}
var cpc_avg = 0.88,
cpc_shift = 0.1,
cpc_slope = 31.2,
cpc_power = 3.585792;
var ctr_avg = 0.24164352,
ctr_shift = 0.07671345217536,
ctr_slope = 2,
ctr_power = 1;
var xDistance = 130;
var yDistance = 87;
var margin = {top: 16, right: 9, bottom: 24, left: 32},
width = tributary.sw - margin.left - margin.right,
height = tributary.sh - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); });
var line2 = d3.svg.line()
.x(function(d) { return x(d.x)+xDistance; })
.y(function(d) { return y(d.y)+yDistance; });
var svg = d3.select("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var ctr_sigmoid = sigmoid(ctr_avg, ctr_slope, ctr_shift);
var cpc_sigmoid = sigmoid(cpc_avg, cpc_slope, cpc_shift);
var data = _.map(d3.range(0,1.0,0.01), function(d){
return {
x: d,
y: ctr_power*ctr_sigmoid(d) + cpc_power*cpc_sigmoid(d)
}
});
console.log(data)
x.domain(d3.extent(data, function(d) { return d.x; }));
y.domain(d3.extent(data, function(d) { return d.y; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("f(x)");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line2);
svg.selectAll("path")
.data(data).enter()
.append("path")
.attr("class","lineh")
.attr("d", function(d){
return "M" + x(d.x) + "," + y(d.y) +
"L" + (x(d.x)+xDistance) + "," + (y(d.y) + yDistance);
})
function sigmoid(avg, slope, shift) {
return function (val) {
return 1 / (1 + Math.exp(-slope * (1/avg) * (val -(shift + 0.5)*avg*2)));
}
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis {
font: 10px sans-serif;
},
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 2.82px;
}
.lineh {
fill: none;
stroke: red;
stroke-width: 2.82px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment