Module: GnuplotRB::Fit

Defined in:
lib/gnuplotrb/fit.rb

Overview

Contains methods relating to Gnuplot's fit function. Covered in fit notebook.

You can also see original gnuplot's fit in gnuplot doc p. 122.

Instance Method Summary collapse

Instance Method Details

#fit(data, function: 'a2*x*x+a1*x+a0', initials: { a2: 1, a1: 1, a0: 1 }, term_options: {}, **options) ⇒ Hash

Fit given data with function.

Fit waits for output from gnuplot Settings.max_fit_delay and throw exception if gets nothing. One can change this value in order to wait longer (if huge datasets is fitted).

Examples:

fit(some_data, function: 'exp(a/x)', initials: {a: 10}, term_option: { xrange: 1..100 })
fit(some_dataset, using: '1:2:3')

Parameters:

  • data (#to_gnuplot_points)

    method accepts the same sources as Dataset.new and Dataset object

  • :function (String)

    function to fit data with

  • :initials (Hash)

    initial values for coefficients used in fitting

  • :term_options (Hash)

    terminal options that should be setted to terminal before fit. You can see them in Plot's documentation (or even better in gnuplot doc) Most useful here are ranges (xrange, yrange etc) and fit option which tunes fit parameters (see gnuplot doc p. 122)

  • options (Hash)

    options passed to Gnuplot's fit such as using. They are covered in gnuplot doc (pp. 69-74)

Returns:

  • (Hash)

    hash with four elements:

    • :formula_ds - dataset with best fit curve as data

    • :coefficients - hash of calculated coefficients. So if you gave “{ initials: 1, b: 1, c: 1 }“ it will return hash with keys :a, :b, :c and its values

    • :deltas - Gnuplot calculates possible deltas for coefficients during fitting and deltas hash contains this deltas

    • :data - pointer to Datablock with given data


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gnuplotrb/fit.rb', line 36

def fit(data, function: 'a2*x*x+a1*x+a0', initials: { a2: 1, a1: 1, a0: 1 }, term_options: {}, **options)
  dataset = data.is_a?(Dataset) ? Dataset.new(data.data) : Dataset.new(data)
  opts_str = OptionHandling.ruby_class_to_gnuplot(options)
  output = gnuplot_fit(function, dataset, opts_str, initials, term_options)
  res = parse_output(initials.keys, function, output)
  {
    formula_ds: Dataset.new(res[2], title: 'Fit formula'),
    coefficients: res[0],
    deltas: res[1],
    data: dataset
  }
end

#fit_exp(data, **options) ⇒ Hash

Shortcuts for fitting with several math functions (exp, log, sin).

Examples:

fit_exp(some_data, initials: { yoffset: -11 }, term_option: { xrange: 1..100 })
#=> The same as:
#=> fit(
#=>   some_data,
#=>   function: 'yscale * (yoffset + exp((x - xoffset) / xscale))',
#=>   initals: { yoffset: -11, xoffset: 0.1, yscale: 1, xscale: 1 },
#=>   term_option: { xrange: 1..100 }
#=> )
fit_log(...)
fit_sin(...)

Parameters:

  • data (#to_gnuplot_points)

    method accepts the same sources as Dataset.new and Dataset object

  • options (Hash)

    all of this options will be passed to #fit so you can set here any options listed in its docs. If you pass here :initials hash, it will be merged into default initals hash. Formula by default is “yscale * (yoffset + #name((x - xoffset) / xscale))”, initials by default “{ yoffset: 0.1, xoffset: 0.1, yscale: 1, xscale: 1 }”

Returns:

  • (Hash)

    hash with four elements:

    • :formula_ds - dataset with best fit curve as data

    • :coefficients - hash of calculated coefficients. So for this case it will return hash with keys :yoffset, :xoffset, :yscale, :xscale and calculated values

    • :deltas - Gnuplot calculates possible deltas for coefficients during fitting and deltas hash contains this deltas

    • :data - pointer to Datablock with given data


119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gnuplotrb/fit.rb', line 119

%w(exp log sin).map do |fname|
  define_method("fit_#{fname}".to_sym) do |data, **options|
    options[:initials] = {
      yoffset: 0.1,
      xoffset: 0.1,
      yscale: 1,
      xscale: 1
    }.merge(options[:initials] || {})
    function = "yscale * (yoffset + #{fname} ((x - xoffset) / xscale))"
    fit(data, **options, function: function)
  end
end

#fit_log(data, **options) ⇒ Hash

Shortcuts for fitting with several math functions (exp, log, sin).

Examples:

fit_exp(some_data, initials: { yoffset: -11 }, term_option: { xrange: 1..100 })
#=> The same as:
#=> fit(
#=>   some_data,
#=>   function: 'yscale * (yoffset + exp((x - xoffset) / xscale))',
#=>   initals: { yoffset: -11, xoffset: 0.1, yscale: 1, xscale: 1 },
#=>   term_option: { xrange: 1..100 }
#=> )
fit_log(...)
fit_sin(...)

Parameters:

  • data (#to_gnuplot_points)

    method accepts the same sources as Dataset.new and Dataset object

  • options (Hash)

    all of this options will be passed to #fit so you can set here any options listed in its docs. If you pass here :initials hash, it will be merged into default initals hash. Formula by default is “yscale * (yoffset + #name((x - xoffset) / xscale))”, initials by default “{ yoffset: 0.1, xoffset: 0.1, yscale: 1, xscale: 1 }”

Returns:

  • (Hash)

    hash with four elements:

    • :formula_ds - dataset with best fit curve as data

    • :coefficients - hash of calculated coefficients. So for this case it will return hash with keys :yoffset, :xoffset, :yscale, :xscale and calculated values

    • :deltas - Gnuplot calculates possible deltas for coefficients during fitting and deltas hash contains this deltas

    • :data - pointer to Datablock with given data


119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gnuplotrb/fit.rb', line 119

%w(exp log sin).map do |fname|
  define_method("fit_#{fname}".to_sym) do |data, **options|
    options[:initials] = {
      yoffset: 0.1,
      xoffset: 0.1,
      yscale: 1,
      xscale: 1
    }.merge(options[:initials] || {})
    function = "yscale * (yoffset + #{fname} ((x - xoffset) / xscale))"
    fit(data, **options, function: function)
  end
end

#fit_poly(data, degree: 2, **options) ⇒ Hash

Shortcut for fit with polynomial. Degree here is max power of x in polynomial.

Examples:

fit_poly(some_data, degree: 5, initials: { a4: 10, a2: -1 }, term_option: { xrange: 1..100 })
#=> The same as:
#=> fit(
#=>   some_data,
#=>   function: 'a5*x**5 + a4*x**4 + ... + a0*x**0',
#=>   initals: {a5: 1, a4: 10, a3: 1, a2: -1, a1: 1, a0: 1},
#=>   term_option: { xrange: 1..100 }
#=> )

Parameters:

  • data (#to_gnuplot_points)

    method accepts the same sources as Dataset.new and Dataset object

  • :degree (Integer)

    degree of polynomial

  • options (Hash)

    all of this options will be passed to #fit so you can set here any options listed in its docs. If you pass here :initials hash, it will be merged into default initals hash. Formula by default is “xn*x**n + … + x0*x**0”, initials by default “{ an: 1, …, a0: 1 }”

Returns:

  • (Hash)

    hash with four elements:

    • :formula_ds - dataset with best fit curve as data

    • :coefficients - hash of calculated coefficients. So for degree = 3 it will return hash with keys :a3, :a2, :a1, :a0 and calculated values

    • :deltas - Gnuplot calculates possible deltas for coefficients during fitting and deltas hash contains this deltas

    • :data - pointer to Datablock with given data


76
77
78
79
80
81
82
83
# File 'lib/gnuplotrb/fit.rb', line 76

def fit_poly(data, degree: 2, **options)
  sum_count = degree + 1
  initials = {}
  sum_count.times { |i| initials["a#{i}".to_sym] = 1 }
  options[:initials] = initials.merge(options[:initials] || {})
  function = sum_count.times.map { |i| "a#{i}*x**#{i}" }.join(' + ')
  fit(data, **options, function: function)
end

#fit_sin(data, **options) ⇒ Hash

Shortcuts for fitting with several math functions (exp, log, sin).

Examples:

fit_exp(some_data, initials: { yoffset: -11 }, term_option: { xrange: 1..100 })
#=> The same as:
#=> fit(
#=>   some_data,
#=>   function: 'yscale * (yoffset + exp((x - xoffset) / xscale))',
#=>   initals: { yoffset: -11, xoffset: 0.1, yscale: 1, xscale: 1 },
#=>   term_option: { xrange: 1..100 }
#=> )
fit_log(...)
fit_sin(...)

Parameters:

  • data (#to_gnuplot_points)

    method accepts the same sources as Dataset.new and Dataset object

  • options (Hash)

    all of this options will be passed to #fit so you can set here any options listed in its docs. If you pass here :initials hash, it will be merged into default initals hash. Formula by default is “yscale * (yoffset + #name((x - xoffset) / xscale))”, initials by default “{ yoffset: 0.1, xoffset: 0.1, yscale: 1, xscale: 1 }”

Returns:

  • (Hash)

    hash with four elements:

    • :formula_ds - dataset with best fit curve as data

    • :coefficients - hash of calculated coefficients. So for this case it will return hash with keys :yoffset, :xoffset, :yscale, :xscale and calculated values

    • :deltas - Gnuplot calculates possible deltas for coefficients during fitting and deltas hash contains this deltas

    • :data - pointer to Datablock with given data


119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gnuplotrb/fit.rb', line 119

%w(exp log sin).map do |fname|
  define_method("fit_#{fname}".to_sym) do |data, **options|
    options[:initials] = {
      yoffset: 0.1,
      xoffset: 0.1,
      yscale: 1,
      xscale: 1
    }.merge(options[:initials] || {})
    function = "yscale * (yoffset + #{fname} ((x - xoffset) / xscale))"
    fit(data, **options, function: function)
  end
end