我想使用C#應用程序分析一些數據並需要計算趨勢線。我知道有多種類型的趨勢線,但現在我正在計算指數增長;我將用它來預測未來的價值。我已經工作過的公式爲從數據點計算指數增長方程c#
x(t) = x(0) * ((1+r)^t)
這是我寫的,試圖複製圖形代碼:
public void ExponentialBestFit(List<DateTime> xvalues, List<double> yvalues)
{
//Find the first value of y (The start value) and the first value of x (The start date)
xzero = Convert.ToDouble(xvalues[0].ToOADate());
yzero = yvalues[0];
if (yzero == 0)
yzero += 0.1;
//For every value of x (exluding the 1st value) find the r value
//
// | y | Where t = the time sinse the start time (time period)
//Equation for r = t root|-------| - 1 Where y = the current y value
// | y[0] | Where y[0] = the first y value #IMPROVMENT - Average 1st y value in range
//
double r = 0;
//c is a count of how many r values are added; it is not equal to the count of all the values
int c = 0;
for (int i = 1; i < xvalues.Count; i++)
{
r += Math.Pow(yvalues[i]/yzero, 1/(Convert.ToDouble(xvalues[i].ToOADate()) - xzero)) - 1;
c++;
}
r = r/c;
}
我傳遞中的數據是在一段時間然而時間增加的增量並不相同。當我在Excel中創建圖表時,他們使用不同的公式
x(t) = x(0)*(e^kt)
但是我想我不知道k值是從哪裏生成的。我傳入的兩個列表是日期和值,每個列表中的每一行對應於另一列表中的同一行。問題是 - 是否有創建方程和變量的更好方法,並且是我獲得的數據最準確的變量?
'e^kt'與'(r + 1)^ t'相同,只是不同的常量('r = e^k-1') - 即我不擔心Excel有不同的表單,並且可以使用該公式來查看是否獲得與Excel相同的結果。 – Rawling
@Rawling哦,好的,謝謝你。我嘗試用70的規則複製常數k,其中k = ln(2)/ T和我得到了一條非常相似的線,雖然它有更多的增長。我會嘗試比較圖表對彼此。乾杯! – manicmonkey21421