Nico Acosta

Hi.

How can I work with statistical functions, like NormSDist( ), within VisualC++ code

I need to perform calculations like this:

d1*NormsDist(d2)

<math.h> doens't include statistics and I' m not interested in scientific libraries like NagC etc.

Nico

### Re: Visual C++ General Including statistical calculation in VisualC++ code

Aleksandr Tokarev

It's not clear whether you would use Excel Worksheet Function NormSDist() Or the function thats makes the same but standart C++ function

### Re: Visual C++ General Including statistical calculation in VisualC++ code

Nico Acosta

Both solutions would be useful to me.

I don't know how to invoque Excel Worksheet Function NormSDist from C++, and I don't know about specific C++ libraries ,like <math.h>, from where perform this kind of calculations.

With NagC libraries you can do it, but we are talking about very professional and expensive libraries. It's not a possibility to me.

Nico

### Re: Visual C++ General Including statistical calculation in VisualC++ code

Aleksandr Tokarev

Nico,

What architect of your application Win32, .NET

### Re: Visual C++ General Including statistical calculation in VisualC++ code

Aleksandr Tokarev

Code Snippet

// W32Con.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

using namespace std;

#import "progid:Excel.Sheet" auto_search auto_rename no_auto_exclude

int _tmain(int argc, _TCHAR* argv[])

{

::CoInitialize(NULL);

{

using namespace Excel;

try

{

_ApplicationPtr app(__uuidof(Application));

WorksheetFunctionPtr wf = app->WorksheetFunction;

double l = wf->NormSDist(1.0);

cout<<l<<endl;

}

catch(const _com_error& e)

{

wprintf(e.ErrorMessage());

std::cin.get();

}

}

::CoUninitialize();

return 0;

}

### Re: Visual C++ General Including statistical calculation in VisualC++ code

Simple Samples

There are sample uses of Office, including Excel for sure, in places such as the MSDN. I think there are samples provided with Office. There are many other samples. Many samples use a script langauge, but if you use the existing C/C++ samples then the samples in the other languages can be helpful too.

I assume that the problem with the other libraries is cost. My guess is that there are libraries you can use for free or something reasonable for your situation. If you have not looked in the boost.org web site, I hope it is a pleasant surprise. If you have not searched the internet, then try doing that.

### Re: Visual C++ General Including statistical calculation in VisualC++ code

Nico Acosta

Hi Aleksandr .Thanks a lot for your help. I'll try with the code you sent me. I learnt few things .

I found a polynomial aproximation to Normal Standard Distribution Function. I prooved it , and I found it is quite good for a simple application, and i'm solving my requirements with it. Here is the code:

`double N(const double x)`
`{`
`  const double b1 =  0.319381530;`
`  const double b2 = -0.356563782;`
`  const double b3 =  1.781477937;`
`  const double b4 = -1.821255978;`
`  const double b5 =  1.330274429;`
`  const double p  =  0.2316419;`
`  const double c  =  0.39894228;`
` `
`  if(x >= 0.0) {`
`      double t = 1.0 / ( 1.0 + p * x );`
`      return (1.0 - c * exp( -x * x / 2.0 ) * t *`
`      ( t *( t * ( t * ( t * b5 + b4 ) + b3 ) + b2 ) + b1 ));`
`  }`
`  else {`
`      double t = 1.0 / ( 1.0 - p * x );`
`      return ( c * exp( -x * x / 2.0 ) * t *`
`      ( t *( t * ( t * ( t * b5 + b4 ) + b3 ) + b2 ) + b1 ));`
`    }`
`}`

Perhaps it could be usefull for someone.

Regards

### Re: Visual C++ General Including statistical calculation in VisualC++ code

Nico Acosta

Hi Simple Samples.

Yes, there are very nice scientific libraries, but also very expensive(v.g.NagC). I posted a code that, precisely, I found in the Web and, by now, I solved my needs. Likewise I'll look into the Boost.Org site .

Thanks a lot for reply and help.

regards .

nico.

### Re: Visual C++ General Including statistical calculation in VisualC++ code

Aleksandr Tokarev

Nico, Can you send the source where did you get this approximation

Thanks.

### Re: Visual C++ General Including statistical calculation in VisualC++ code

Nico Acosta

I forgot in doing it.

http://www.sitmo.com/

Regards.

nico