2011-09-15 53 views
3

我正在構建一個.net頁面來模擬電子表格。該表包含此公式如何重新創建一個在C#中調用TREND()的Excel公式?

=ROUND(TREND(AA7:AE7,AA$4:AE$4,AF$4),1) 

有人可以提供相當於C#的TREND()嗎?或者,如果任何人都可以提供一個快捷方式,那也可以;我對那裏的數學不夠熟悉,不知道是否有更簡單的方法。

這裏有一些示例編號,如果有幫助。

AA7:AE7 6 8 10 12 14

10.2 13.6 17.5 20.4 23.8

AA $ 4:AE $ 4 600 800 1000 1200 1400

AF $ 4 650

編輯:這是我想出來的,它似乎與我的電子表格產生相同的數字。

public static partial class Math2 
{ 
    public static double[] Trend(double[] known_y, double[] known_x, params double[] new_x) 
    { 
     // return array of new y values 
     double m, b; 
     Math2.LeastSquaresFitLinear(known_y, known_x, out m, out b); 

     List<double> new_y = new List<double>(); 
     for (int j = 0; j < new_x.Length; j++) 
     { 
      double y = (m * new_x[j]) + b; 
      new_y.Add(y); 
     } 

     return new_y.ToArray(); 
    } 

    // found at http://stackoverflow.com/questions/7437660/how-do-i-recreate-an-excel-formula-which-calls-trend-in-c 
    // with a few modifications 
    public static void LeastSquaresFitLinear(double[] known_y, double[] known_x, out double M, out double B) 
    { 
     if (known_y.Length != known_x.Length) 
     { 
      throw new ArgumentException("arrays are unequal lengths"); 
     } 

     int numPoints = known_y.Length; 

     //Gives best fit of data to line Y = MC + B 
     double x1, y1, xy, x2, J; 

     x1 = y1 = xy = x2 = 0.0; 
     for (int i = 0; i < numPoints; i++) 
     { 
      x1 = x1 + known_x[i]; 
      y1 = y1 + known_y[i]; 
      xy = xy + known_x[i] * known_y[i]; 
      x2 = x2 + known_x[i] * known_x[i]; 
     } 

     M = B = 0; 
     J = ((double)numPoints * x2) - (x1 * x1); 

     if (J != 0.0) 
     { 
      M = (((double)numPoints * xy) - (x1 * y1))/J; 
      //M = Math.Floor(1.0E3 * M + 0.5)/1.0E3; // TODO this is disabled as it seems to product results different than excel 
      B = ((y1 * x2) - (x1 * xy))/J; 
      // B = Math.Floor(1.0E3 * B + 0.5)/1.0E3; // TODO assuming this is the same as above 
     } 
    } 

} 

回答

4

考慮TREND基於Excel函數LINEST。 如果您點擊此鏈接,https://support.office.com/en-us/article/LINEST-function-84d7d0d9-6e50-4101-977a-fa7abf772b6d,它將解釋LINEST背後的功能。

此外,你會發現它使用的基本公式。

First Formula

Second formulat

+0

我發現至少有一個sqaures擬合函數需要一組{x,y}並返回M和B.然後我可以使用M和B以及一組新的x值來生成y值作爲趨勢結果返回。這一切是否正確? – lincolnk

+0

說實話,我對LINEST或TREND函數並不熟悉。在使用這些文件時它們會變得非常複雜,並且從文檔來看,它們看起來有點不可靠(垃圾進入垃圾)。我認爲您需要了解Excel函數的功能,並嘗試在C#中重現結果。從我所能說的來看,並非易事。 –

+0

鏈接不再有效 – Rocklan

4

這篇文章爲我們需要的C#來重建這個非常有幫助。感謝傑夫的回答上面我說用下面的公式重建:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Drawing; 

public static class MathHelper 
{ 
    /// <summary> 
    /// Gets the value at a given X using the line of best fit (Least Square Method) to determine the equation 
    /// </summary> 
    /// <param name="points">Points to calculate the value from</param> 
    /// <param name="x">Function input</param> 
    /// <returns>Value at X in the given points</returns> 
    public static float LeastSquaresValueAtX(List<PointF> points, float x) 
    { 
     float slope = SlopeOfPoints(points); 
     float yIntercept = YInterceptOfPoints(points, slope); 

     return (slope * x) + yIntercept; 
    } 

    /// <summary> 
    /// Gets the slope for a set of points using the formula: 
    /// m = ∑ (x-AVG(x)(y-AVG(y))/∑ (x-AVG(x))² 
    /// </summary> 
    /// <param name="points">Points to calculate the Slope from</param> 
    /// <returns>SlopeOfPoints</returns> 
    private static float SlopeOfPoints(List<PointF> points) 
    { 
     float xBar = points.Average(p => p.X); 
     float yBar = points.Average(p => p.Y); 

     float dividend = points.Sum(p => (p.X - xBar) * (p.Y - yBar)); 
     float divisor = (float)points.Sum(p => Math.Pow(p.X - xBar, 2)); 

     return dividend/divisor;    
    } 

    /// <summary> 
    /// Gets the Y-Intercept for a set of points using the formula: 
    /// b = AVG(y) - m(AVG(x)) 
    /// </summary> 
    /// <param name="points">Points to calculate the intercept from</param> 
    /// <returns>Y-Intercept</returns> 
    private static float YInterceptOfPoints(List<PointF> points, float slope) 
    { 
     float xBar = points.Average(p => p.X); 
     float yBar = points.Average(p => p.Y); 

     return yBar - (slope * xBar);   
    }  
} 

由於點使用整數來定義自己的價值觀,我選了我們的應用程序中使用的PointF,因爲可以有很多位小數。請原諒,因爲我花更多的時間編寫代碼,而不是開發像這樣的算法,但我會愛任何人糾正我,因爲我應該在某個地方弄糊塗了一個術語。

這肯定比等待Excel Interop加載到後臺以使用工作簿的趨勢方法更快。

+0

謝謝你,很棒的代碼。 – Rocklan

相關問題