Skip to content

Distribution Modeler

Jake Close edited this page Dec 6, 2017 · 25 revisions

Project Overview:

Modeler part of the project

This is a test

Project Structure:

How to implement a new distribution:

For this example, we will use the Normal Distribution.

  1. Create new file for service following the name style "ModelerDistribution" + "distribution Name"
ModelerDistributionNormal.service.coffe
  1. Implement Normal Distribution Class
  • Create class outline from base class
'use strict'

BaseService = require 'scripts/BaseClasses/BaseService.coffee'

###
  @name:
  @type: service
  @desc: Normal Distribution Class
###

module.exports = class NormalDist extends BaseService
  @inject 'socrat_analysis_modeler_getParams'
  initialize: () ->
    @calc = @socrat_analysis_modeler_getParams

  • Add required member variables and member functions

Member Variables: Name and parameters involved in creating model distribution

    @NormalMean = 5
    @NormalStandardDev = 1
    @NormalVariance = 1
    @name = 'Normal'

  • Member functions: Include: getName: returns name of the distribution getParams: returns models current distribution parameters getChartData: returns the model data by calling get"PDF"points setParams: updates the models current distribution parameters from the Main Controller (UI). PDF: Distributions Probability Density Function CDF: Cumulative Distribution Function (these can be found on wikipedia) get"PDF"Points: creates and array of [{x:, y:} , {x:, y:}] data on interval based off of the PDF function of the dataset
  getName: () ->
    return @name
  getGaussianFunctionPoints: (leftBound, rightBound) ->
    data = []
    for i in [leftBound...rightBound] by .1
      data.push
        x: i
        y: @PDF(i)
    console.log(data)
    data

  getChartData: (params) ->
    
    curveData = getGaussianFunctionPoints( params.xMin , params.xMax)
    return curveData


  stdNormalCDF: (x) ->
    return 0.5 * 0.5 * @calc.erf( x/ Math.sqrt(2))
  

  PDF: (x) ->
    return (1 / (@NormalStandardDev * Math.sqrt(Math.PI * 2))) * Math.exp(-(Math.pow(i - @NormalMean, 2) / (2 * @NormalVariance)))

  CDF: (x)->
    return @stdNormalCDF((x-@NormalMean)/ @NormalStandardDev)

  getParams: () ->
    params =
      mean: @NormalMean
      standardDev: @NormalStandardDev
      variance: @NormalVariance

  setParams: (newParams) ->
    @NormalMean = parseFloat(newParams.stats.mean.toPrecision(4))
    @NormalStandardDev =parseFloat(newParams.stats.standardDev.toPrecision(4))
    @NormalVariance = parseFloat(newParams.stats.variance.toPrecision(4))
  1. Import the service into the my_module.module file, under the component/service section with the rest of the distributions.
 'socrat_modeler_distribution_Normal': require 'scripts/analysis/tools/Modeler/ModelerDistributionNormal.service.coffee'
  1. Import the new distribution into the router page ModelerRouter.service.coffe, add it to the available models list.
  • Inject the file name
 'socrat_modeler_distribution_normal',
  • Initialize the distribution
@Normal = @socrat_modeler_distribution_normal

*Add to available models

@models = [@Normal, @Laplace, @ChiSquared, @MaxwellBoltzman, @LogNormal, @Cauchy, @Exponential]
  1. Lastly, add the distribution to the DistList.service.coffee so the model appears in the SideBar.
   name: 'Normal'
      value: 0
      x: ['integer', 'number', 'string']
      y: false
      z: false
      message: "This class models a normal distribution fitting curve to the dataset"
      xLabel: "Add x"
Clone this wiki locally