Page 1 of 1

Usage of standard deviation function

Posted: Fri Jul 23, 2010 8:05 pm
by 15056363
I am using Activex v8 version of TeeChart.

I have a line series and i want to display the average and standard deviation for that series as a text (annotation tool).

I was able to use the "m_chart.Series(index).SetFunction(tfAverage)" to find the average. I then disabled this series and used "GetYValues().GetValue(value_index)" to obtain the average value in a local variable, which i then converted to string for display.

However, when i did the same thing to find the standard deviation, it always displayed "0.0" as the value. When i checked the TeeChart tutorial, it says that standard deviation function requires only one input.

Can someone please tell me how to find the standard deviation for a series?

Thanks,
Hari

Re: Usage of standard deviation function

Posted: Mon Jul 26, 2010 12:26 pm
by yeray
Hi Hari,

Here it is an example that I think achieves what you described:

Code: Select all

Private Sub Form_Load()  
  With TChart1
    .Aspect.View3D = False
    .AddSeries scPoint
    .Series(0).FillSampleValues 5
  
    .AddSeries scLine
    .Series(1).SetFunction tfAverage
    .Series(1).DataSource = .Series(0)
    .Series(1).CheckDataSource
    .Tools.Add tcAnnotate
    With .Tools.Items(0).asAnnotation
      .Left = 10
      .Top = 10
      .Text = "Average: " + Str$(TChart1.Series(1).YValues.Value(0))
    End With
  
    .AddSeries scLine
    .Series(2).SetFunction tfStdDeviation
    .Series(2).DataSource = .Series(0)
    .Series(2).CheckDataSource
    .Tools.Add tcAnnotate
    With .Tools.Items(1).asAnnotation
      .Left = 150
      .Top = 10
      .Text = "Standard deviation: " + Str$(TChart1.Series(2).YValues.Value(0))
    End With
    
    TChart1.Series(1).Active = False
    TChart1.Series(2).Active = False
  End With
End Sub

Re: Usage of standard deviation function

Posted: Mon Jul 26, 2010 5:19 pm
by 15056363
Hi Yeray.

I already tried exactly what you said. It does not work for standard deviation. It displays the values as 0. However, the average function works fine.

As i mentioned earlier, the tutorial says that number of inputs for std. deviation is 1 and it is described as "Maps the Standard Deviation (or Complete Standard Deviation) of every group of Period points".

This doesn't make sense to me.

Hari

Re: Usage of standard deviation function

Posted: Tue Jul 27, 2010 8:56 am
by yeray
Hi Hari,

The example above gives me the following result:
test.png
test.png (17.06 KiB) Viewed 7773 times
Doesn't it give the same result to you? What exact TeeChart version are you using? Is it the latest available in the client area?

Regarding to the number of inputs, I've seen that the following snipped of code works in the same way in both functions:

Code: Select all

    TChart1.Series(2).FunctionType.PeriodStyle = psNumPoints
    TChart1.Series(2).FunctionType.Period = 2
    TChart1.Series(2).asLine.Pointer.Visible = True

Re: Usage of standard deviation function

Posted: Tue Jul 27, 2010 6:38 pm
by 15056363
Hi Yeray.

I am using the latest ActiveX version 8.

I guess i found out the problem. The problem is with the SetPeriod(). My line series has 61400 points. If i set the period for standard deviation to 61400 or 50000, it shows the value as 0. However if i set the period to say 30000, it displays 2 values for standard deviation, one for the first 30000 points and the other for the next 30000. If i set the period to 2, then i am getting 30700 values for standard deviation, one value for 2 consecutive points.

So is there some maximum limit for SetPeiod() function for standard deviation? It displays the correct value for standard deviation if i set the period to 45000. Changing the PeriodStyle does not affect the output.

However, the average function works fine if i set the period to 61400.

Hari

Re: Usage of standard deviation function

Posted: Fri Jul 30, 2010 4:14 pm
by yeray
Hi Hari,

There is no limitation for the Period.
I've made some tests and found an strange behaviour. In the delphi sources, in the CalculateDeviation function, there is the following calculation:

Code: Select all

Divisor:=INumPoints*(INumPoints-1)
I don't understand why, when INumPoints has a value between 46342 and 65536, it gives an unexpected, negative value, and this makes the function to return a 0 when shouldn't.
We've also seen that creating an intermediate variable the problem seems to be solved:

Code: Select all

var tmp: Double;
begin
  tmp:=INumPoints;
  //...
  Divisor:=tmp*(tmp-1);
I've added it to the wish list to be revised for future releases (TV52015063).

Re: Usage of standard deviation function

Posted: Tue Sep 21, 2010 11:28 am
by narcis
Hi Hari,

As an update, after some more investigation we found the solution Yeray suggested is the correct one as INumPoints needs to be type-casted to a double value so that Std. Deviation is calculated correctly. We implemented the fix for next maintenance release.