{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fitting data with formula\n",
"Gnuplot is capable to fit given data with functions given as formulas. This notebook contain several examples about how to fit data with GnuplotRB."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First lets prepare some data:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"require 'daru'\n",
"require 'gnuplotrb'\n",
"include GnuplotRB\n",
"include Fit\n",
"\n",
"rows = (1..30).map do |i|\n",
" [i**2 * (rand(4) + 3) / 5, rand(70) + 1]\n",
"end\n",
"df = Daru::DataFrame.rows(rows, order: [:Value, :Error], name: 'Confidence interval')\n",
"\n",
"random_points = Plot.new(\n",
" [df[:Value], with: 'lines', title: 'Average value'],\n",
" [df, with: 'err']\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now lets try to fit it with some function.\n",
"\n",
"First parameter of #fit is *data* (you can use here Datablock, Dataset or any other object out of which Datablock may be constructed). Special options are *function* and *initials*, other options (such as *using*) may be found in gnuplot docs. Be careful: you should give initial value for each coefficient used in formula.\n",
"\n",
"Return value is the following hash:\n",
"```\n",
"{\n",
" :formula_ds => #,\n",
" :coefficients => #,\n",
" :deltas => #,\n",
" :data => #\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{:a=>0.0271674, :b=>8.64614, :c=>-6.69375}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_poly = fit(df, function: \"a*x**3 + b*x + c\", initials: {a: 1, b: 1, c: 1})\n",
"some_poly[:coefficients]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\" 0.0271674*x**3 + 8.64614*x + -6.69375 \""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_poly[:formula_ds].data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"# \"Fit formula\"]>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"some_poly[:formula_ds]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"# \"Fit formula\"]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random_points << some_poly[:formula_ds]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find more information about how gnuplot's fitting work in [gnuplot doc](http://www.gnuplot.info/docs_5.0/gnuplot.pdf)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Default fittings\n",
"Polynomial:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"# \"Fit formula\"]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"poly = fit_poly(df)\n",
"random_points.add_dataset(poly[:formula_ds])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Be default polynomial degree is 2 but you can set it:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
"# \"Fit (degree 2)\", :lw => 2]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">, # \"Fit (degree 3)\", :lw => 2]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">, # \"Fit (degree 4)\", :lw => 2]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">, # \"Fit (degree 5)\", :lw => 2]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">, # \"Fit (degree 6)\", :lw => 2]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">, # \"Fit (degree 7)\", :lw => 2]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">, # \"Fit (degree 8)\", :lw => 2]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">, # \"Fit (degree 9)\", :lw => 2]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">], @options=Hamster::Hash[:animate => {:delay=>150}]>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"frames = (2..9).map { |i| random_points.add_dataset(fit_poly(df, degree: i)[:formula_ds].options(title: \"Fit (degree #{i})\", lw: 2)) }\n",
"Animation.new(*frames, animate: { delay: 150 } )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also fit data with *exp*, *sin* and *log* like this:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"# \"Fit formula\"]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"exp = fit_exp(df)\n",
"random_points.add_dataset(exp[:formula_ds])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And set ranges for fitting:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"# [0..15]], @datasets=Hamster::Vector[# \"Fit formula\"]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Average value\", :using => \"1:2\"]>, #, @options=Hamster::Hash[:with => \"err\", :title => :\"Confidence interval\", :using => \"1:2:3\"]>], @cmd=\"plot \">"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ranged_poly = fit_poly(df, degree: 4, term_options: { xrange: 0..15})\n",
"random_points.add_dataset(ranged_poly[:formula_ds]).xrange(0..15)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.1.2",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": "rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}