test_DataModeler.praat 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # test_DataModeler.praat
  2. # djmw 20161011
  3. appendInfoLine: "test_DataModeler.praat"
  4. @testDataModelerInterface
  5. appendInfoLine: "test_DataModeler.praat OK"
  6. procedure testDataModelerInterface
  7. # create a table for model y = 2x +3
  8. .xmin = -1
  9. .xmax = 3
  10. .nx = 100
  11. @createData: .xmin, .xmax, .nx, 0
  12. .table = selected ("Table")
  13. .maximumPolynomialOrder = 3
  14. .dm = To DataModeler: .xmin, .xmax, "x", "y", "", "Legendre polynomials", .maximumPolynomialOrder
  15. #
  16. # Query -
  17. #
  18. .numberOfParameters = Get number of parameters
  19. assert .numberOfParameters = .maximumPolynomialOrder + 1
  20. .numberOfFixedParameters = Get number of fixed parameters
  21. assert .numberOfFixedParameters = 0
  22. for .ipar to .numberOfParameters
  23. .value = Get parameter value: .ipar
  24. .stdev = Get parameter standard deviation: .ipar
  25. .status$ = Get parameter status: .ipar
  26. .status$ = mid$ (.status$, 1, 4)
  27. assert .status$ = "Free"
  28. Set parameter value: .ipar, .value, .status$
  29. .value2 = Get parameter value: .ipar
  30. assert .value = .value2
  31. for .ipar2 from .ipar to .numberOfParameters
  32. .var = Get variance of parameters: .ipar, .ipar2
  33. endfor
  34. endfor
  35. .valxmin = Get model value at x: .xmin
  36. .valxmax = Get model value at x: .xmax
  37. .numberOfDataPoints = Get number of data points
  38. .numberOfInvalidDataPoints = Get number of invalid data points
  39. for .idata to .numberOfDataPoints
  40. .statusdata$ = Get data point status: .idata
  41. assert mid$ (.statusdata$, 1, 5) = "Valid"
  42. .savex = Get data point x value: .idata
  43. .set = randomUniform (.xmin, .xmin)
  44. Set data point x value: .idata, .set
  45. .get = Get data point x value: .idata
  46. assert .set = .get; ['.idata'] '.set' = '.get'
  47. Set data point x value: .idata, .savex
  48. .savey = Get data point y value: .idata
  49. .set = randomUniform (.xmin, .xmin)
  50. Set data point y value: .idata, .set
  51. .get = Get data point y value: .idata
  52. assert .set = .get; ['.idata'] '.set' = '.get'
  53. Set data point y value: .idata, .savey
  54. .saves = Get data point y sigma: .idata
  55. .set = randomUniform (1, 10)
  56. Set data point y sigma: .idata, .set
  57. .get = Get data point y sigma: .idata
  58. assert .set = .get; ['.idata'] '.set' = '.get'
  59. Set data point y sigma: .idata, .saves
  60. endfor
  61. .sumOfSquares_res = Get residual sum of squares
  62. .dataStdDev = Get data standard deviation
  63. .coef = Get coefficient of determination
  64. .chiSqReport$ = Report chi squared: "Equally"
  65. .ndf = Get degrees of freedom
  66. #
  67. # Modify -
  68. #
  69. Set data weighing: "Equally"
  70. Set tolerance: 1e-5
  71. # Set parameter value: see query part
  72. Set parameter free: 0, 0
  73. #
  74. Fit model
  75. .covar = To Covariance (parameters)
  76. selectObject: .dm
  77. .zscores = To Table (z-scores): "yes"
  78. removeObject: .zscores, .covar, .table, .dm
  79. endproc
  80. procedure createData: .xmin, .xmax, .nx, .ynoise_stdev
  81. .model = Create Table with column names: "table", .nx, "x y"
  82. # linear
  83. .dx = (.xmax -.xmin) / (.nx - 1)
  84. for .row to .nx
  85. .x = .xmin + (.row -1) * .dx
  86. .y = 2 * .x + 3 + randomGauss (0, .ynoise_stdev)
  87. Set numeric value: .row, "x", .x
  88. Set numeric value: .row, "y", .y
  89. endfor
  90. endproc