-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from lyskouski/RF-146
[#146] [RF] Improve Forecast Chart. Add Monte Carlo prediction
- Loading branch information
Showing
23 changed files
with
161 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2023 The terCAD team. All rights reserved. | ||
// Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file. | ||
|
||
import 'dart:math'; | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
class MonteCarloSimulation { | ||
final Random random = Random(); | ||
final int cycles; | ||
final double coercion; | ||
|
||
MonteCarloSimulation({this.cycles = 30, this.coercion = 1}); | ||
|
||
List<Offset> generate(List<Offset> scope, double step, double max) { | ||
final List<List<double>> distribution = []; | ||
for (int i = 0; i < scope.length; i++) { | ||
final state = mcNormal(scope[i].dy, coercion, cycles); | ||
for (int j = 0; j < state.length; j++) { | ||
if (j >= distribution.length) { | ||
distribution.add([]); | ||
} | ||
distribution[j].add(state[j]); | ||
} | ||
} | ||
double posX = scope.last.dx + step; | ||
List<Offset> result = []; | ||
int idx = 0; | ||
while (posX <= max) { | ||
result.add(Offset(posX, distribution[idx][distribution[idx].length * random.nextDouble() ~/ 1])); | ||
posX += step; | ||
idx++; | ||
} | ||
return result; | ||
} | ||
|
||
List<double> mcNormal(double mean, double stdDev, int samples) { | ||
List<double> results = []; | ||
for (int i = 0; i < samples; i++) { | ||
results.add(_normalRandom(mean, stdDev)); | ||
} | ||
return results; | ||
} | ||
|
||
double _normalRandom(double mean, double stdDev) { | ||
double u1 = random.nextDouble(); | ||
double u2 = random.nextDouble(); | ||
double z0 = sqrt(-2.0 * log(u1)) * cos(2 * pi * u2); | ||
return mean + stdDev * z0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.