sampler.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. export class Sampler {
  2. constructor(id, data, classes, depositRatio, maxSamples, voffsets) {
  3. this.data = data;
  4. this.samples = [];
  5. this.voffsets = [];
  6. this.classes = classes;
  7. this.id = id;
  8. this.depositRatio = depositRatio;
  9. this.svgclass = 'sampler-' + this.id;
  10. this.currentSampleId = undefined;
  11. this.currentYield = undefined;
  12. this.maxSamples = maxSamples;
  13. }
  14. // actual sampling implemented as importance sampling in app.js
  15. drawSample(d) {
  16. let y = (1 - this.depositRatio) * d.values[0] + this.depositRatio * d.values[1];
  17. // compare yield to thresholds defined in classes
  18. for (let j = 0; j < this.classes.length; ++j) {
  19. // if (y >= this.classes[j].min && y < this.classes[j].max) {
  20. if (y > this.classes[j].min && y <= this.classes[j].max) {
  21. this.currentSampleId = this.classes[j].id;
  22. this.currentYield = y;
  23. this.samples.push(this.currentSampleId);
  24. return;
  25. }
  26. }
  27. }
  28. updateOffsets(iconBlockSize) {
  29. for (let cidx = 0; cidx < this.classes.length; ++cidx) {
  30. this.voffsets[cidx] = 1; // calculate number of lines occupied by class
  31. }
  32. return this.voffsets;
  33. }
  34. get calculatedYield() {
  35. let factor = Math.pow(10, 0);
  36. let val = Math.round(this.currentYield * 100 * factor) / factor;
  37. let sign = '';
  38. if (val > 0) {
  39. sign = '+';
  40. } else if (sign < 0) {
  41. sign = '-';
  42. }
  43. let result = (sign + val + '%').replace('.', ',');
  44. return result;
  45. }
  46. getStockRatioRepr() {
  47. let val = Math.round((1 - this.depositRatio) * 100000) / 1000;
  48. return val + '%';
  49. }
  50. get stockRatioRepr() {
  51. let val = Math.round((1 - this.depositRatio) * 100000) / 1000;
  52. return val + '%';
  53. }
  54. getDepositRatioRepr() {
  55. let val = Math.round(this.depositRatio * 100000) / 1000;
  56. return val + '%';
  57. }
  58. get depositRatioRepr() {
  59. let val = Math.round(this.depositRatio * 100000) / 1000;
  60. return val + '%';
  61. }
  62. setPeriod(val) {
  63. this.period = val;
  64. }
  65. isSafeToSample(amount) {
  66. for (s in this.samplesPerClass()) {
  67. s.length < this;
  68. }
  69. }
  70. samplesPerClass() {
  71. let samplesIsolated = [];
  72. let samples = [];
  73. for (let s of this.samples) { // deep copy of samples
  74. samples.push(s);
  75. }
  76. for (let cls of this.classes) {
  77. let arr = samples.filter(function(c) {
  78. return c === cls.id;
  79. }, cls);
  80. samplesIsolated.push(arr);
  81. }
  82. // console.log('samplesIsolated', this.id, samplesIsolated);
  83. return samplesIsolated;
  84. }
  85. reset() {
  86. this.samples = [];
  87. this.currentSampleId = undefined;
  88. this.currentYield = undefined;
  89. }
  90. }