» Funktionen NORMVERT und NORMINV in c++ sourcecode!

Beispiel in QT für Primzahlenberechnung und textEditNeuen Thread eröffnenNeue Antwort erstellenAnfänger sucht Hilfe
AutorNachricht
Junior 
Name: Voller Name als Anagramm: Fitje Weshoms
Geschlecht:
Fahrzeug: Yamaha YZF R6 Thundercat
Anmeldedatum: 15.07.2012
Beiträge: 92
Wohnort: Amberg, Oberpfalz
01.05.2017, 14:02
zitieren

Hallo Leute!

Für alle, welche sich mal den Sourcecode für die Funktionen NORMVERT undNORMINV
wünschen.

Allerdings hat hier die Funktion NORMVERT den Namen NormalDistribution.

Hab ich irgendwo im Netz gefunden und Umgeschrieben für codeblocks und gcc.

Zuerst der Header norminv.h:

#ifndef NORMINV_H_INCLUDED
#define NORMINV_H_INCLUDED
#include <math.h>

class NormalDistributionConfidenceCalculator
{
public:
    NormalDistributionConfidenceCalculator(void);
    ~NormalDistributionConfidenceCalculator();
    /// <summary>
    ///
    /// </summary>
double InverseNormalDistribution(double probability, double min, double max);
    /// <summary>
    /// Returns the cumulative density function evaluated at A given value.
    /// </summary>
    /// <param name="x">A position on the x-axis.</param>
    /// <param name="mean"></param>
    /// <param name="sigma"></param>
    /// <returns>The cumulative density function evaluated at <C>x</C>.</returns>
    /// <remarks>The value of the cumulative density function at A point <C>x</C> is
    /// probability that the value of A random variable having this normal density is
    /// less than or equal to <C>x</C>.
    /// </remarks>
double NormalDistribution(double x, double mean, double sigma);
    /// <summary>
    /// Given a probability, a mean, and a standard deviation, an x value can be calculated.
    /// </summary>
    /// <returns></returns>
double NormInv(double probability);
    /// <summary>
    ///
    /// </summary>
    /// <param name="probability"></param>
    /// <param name="mean"></param>
    /// <param name="sigma"></param>
    /// <returns></returns>
double NormInv(double probability, double mean, double sigma);
bool AboutEqual(double x, double y);

};
double Epsilon(void);

#endif // NORMINV_H_INCLUDED


nun die Datei norminv.cpp:

#include <iostream>
#include "norminv.h"

NormalDistributionConfidenceCalculator::NormalDistributionConfidenceCalculator(void){};
NormalDistributionConfidenceCalculator::~NormalDistributionConfidenceCalculator(){};

double Epsilon(void)
{
double floatEps = 1;

    while (1 + floatEps / 2 != 1)
        floatEps /= 2;
return floatEps;
}


double NormalDistributionConfidenceCalculator::InverseNormalDistribution(double probability, double minimal, double maximal)
    {
        double x = 0;
        double a = 0;
        double b = 1;

        double precision = pow(10, -3);

        while ((b - a) > precision)
        {
            x = (a + b) / 2;
            if (NormInv(x) > probability)
            {
                b = x;
            }
            else
            {
                a = x;
            }
        }

        if ((maximal > 0) && (minimal > 0))
        {
            x = x * (maximal - minimal) + minimal;
        }
        return x;
    }


double NormalDistributionConfidenceCalculator::NormalDistribution(double x, double mean, double sigma)
    {
        // This algorithm is ported from dcdflib:
        // Cody, W.D. (1993). "ALGORITHM 715: SPECFUN - A Portabel FORTRAN
        // Package of Special Function Routines and Test Drivers"
        // acm Transactions on Mathematical Software. 19, 22-32.
        int i;
        double del, xden, xnum, xsq;
        double result, ccum;
        double arg = (x - mean) / sigma;
        const double sixten = 1.60e0;
        const double sqrpi = 3.9894228040143267794e-1;
        const double thrsh = 0.66291e0;
        const double root32 = 5.656854248e0;
        const double zero = 0.0e0;
        //const double min = double.Epsilon;
        const double minimal = Epsilon();
        double z = arg;
        double y = abs(z);
        const double half = 0.5e0;
        const double one = 1.0e0;

        double a[5] =
            {
                2.2352520354606839287e00, 1.6102823106855587881e02, 1.0676894854603709582e03,
                1.8154981253343561249e04, 6.5682337918207449113e-2
            };

        double b[4] =
            {
                4.7202581904688241870e01, 9.7609855173777669322e02, 1.0260932208618978205e04,
                4.5507789335026729956e04
            };

        double c[9] =
            {
                3.9894151208813466764e-1, 8.8831497943883759412e00, 9.3506656132177855979e01,
                5.9727027639480026226e02, 2.4945375852903726711e03, 6.8481904505362823326e03,
                1.1602651437647350124e04, 9.8427148383839780218e03, 1.0765576773720192317e-8
            };

        double d[8] =
            {
                2.2266688044328115691e01, 2.3538790178262499861e02, 1.5193775994075548050e03,
                6.4855582982667607550e03, 1.8615571640885098091e04, 3.4900952721145977266e04,
                3.8912003286093271411e04, 1.9685429676859990727e04
            };
        double p[6] =
            {
                2.1589853405795699e-1, 1.274011611602473639e-1, 2.2235277870649807e-2,
                1.421619193227893466e-3, 2.9112874951168792e-5, 2.307344176494017303e-2
            };


        double q[5] =
            {
                1.28426009614491121e00, 4.68238212480865118e-1, 6.59881378689285515e-2,
                3.78239633202758244e-3, 7.29751555083966205e-5
            };
        if (y <= thrsh)
        {
            //
            // Evaluate  anorm  for  |X| <= 0.66291
            //
            xsq = zero;
            ///if (y > double.Epsilon) xsq = z * z;
            if (y > Epsilon() ) xsq = z * z;
            xnum = a[4] * xsq;
            xden = xsq;
            for (i = 0; i < 3; i++)
            {
                xnum = (xnum + a[i]) * xsq;
                xden = (xden + b[i]) * xsq;
            }
            result = z * (xnum + a[3]) / (xden + b[3]);
            double temp = result;
            result = half + temp;
        }

        //
        // Evaluate  anorm  for 0.66291 <= |X| <= sqrt(32)
        //
        else if (y <= root32)
        {
            xnum = c[8] * y;
            xden = y;
            for (i = 0; i < 7; i++)
            {
                xnum = (xnum + c[i]) * y;
                xden = (xden + d[i]) * y;
            }
            result = (xnum + c[7]) / (xden + d[7]);
            xsq = floor(y * sixten) / sixten;
            del = (y - xsq) * (y + xsq);
            result = exp(-(xsq * xsq * half)) * exp(-(del * half)) * result;
            ccum = one - result;

            if (z > zero) result = ccum;
        }

            //
        // Evaluate  anorm  for |X| > sqrt(32)
        //
        else
        {
            xsq = one / (z * z);
            xnum = p[5] * xsq;
            xden = xsq;
            for (i = 0; i < 4; i++)
            {
                xnum = (xnum + p[i]) * xsq;
                xden = (xden + q[i]) * xsq;
            }
            result = xsq * (xnum + p[4]) / (xden + q[4]);
            result = (sqrpi - result) / y;
            xsq = floor(z * sixten) / sixten;
            del = (z - xsq) * (z + xsq);
            result = exp(-(xsq * xsq * half)) * exp(-(del * half)) * result;
            ccum = one - result;
            if (z > zero) result = ccum;

        }

        ///if (result < min)
          ///  result = 0.0e0;
          if (result < minimal)
            result = 0.0e0;

        return result;
    }


double NormalDistributionConfidenceCalculator::NormInv(double probability)
    {
        const double a1 = -39.6968302866538;
        const double a2 = 220.946098424521;
        const double a3 = -275.928510446969;
        const double a4 = 138.357751867269;
        const double a5 = -30.6647980661472;
        const double a6 = 2.50662827745924;

        const double b1 = -54.4760987982241;
        const double b2 = 161.585836858041;
        const double b3 = -155.698979859887;
        const double b4 = 66.8013118877197;
        const double b5 = -13.2806815528857;

        const double c1 = -7.78489400243029E-03;
        const double c2 = -0.322396458041136;
        const double c3 = -2.40075827716184;
        const double c4 = -2.54973253934373;
        const double c5 = 4.37466414146497;
        const double c6 = 2.93816398269878;

        const double d1 = 7.78469570904146E-03;
        const double d2 = 0.32246712907004;
        const double d3 = 2.445134137143;
        const double d4 = 3.75440866190742;

        //Define break-points
        // using Epsilon is wrong; see link above for reference to 0.02425 value
        //const double pLow = double.Epsilon;
        const double pLow = 0.02425;

        const double pHigh = 1 - pLow;

        //Define work variables
        double q;
        double result = 0;

        // if argument out of bounds.
        // set it to a value within desired precision.
        if (probability <= 0)
            probability = pLow;

        if (probability >= 1)
            probability = pHigh;

        if (probability < pLow)
        {
            //Rational approximation for lower region
            q = sqrt(-2 * log(probability));
            result = (((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1);
        }
        else if (probability <= pHigh)
        {
            //Rational approximation for lower region
            q = probability - 0.5;
            double r = q * q;
            result = (((((a1 * r + a2) * r + a3) * r + a4) * r + a5) * r + a6) * q /
                     (((((b1 * r + b2) * r + b3) * r + b4) * r + b5) * r + 1);
        }
        else if (probability < 1)
        {
            //Rational approximation for upper region
            q = sqrt(-2 * log(1 - probability));
            result = -(((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1);
        }

        return result;
    }

double NormalDistributionConfidenceCalculator::NormInv(double probability, double mean, double sigma)
    {
        double x = NormInv(probability);
        return sigma * x + mean;
    }

bool AboutEqual(double x, double y) {
    double epsilon = std::max(abs(x), abs(y)) * 1E-15;
    return abs(x - y) <= epsilon;
}

Nun main.cpp selbst:



#include <iostream>
#include "norminv.h"


using namespace std;



int main()
{
 NormalDistributionConfidenceCalculator nvt;
 double wert, innen;
 int wouhi = 8;
 double my, x;

 do
 switch(wouhi)
 {
   default:
     cout << "Standardisierte Normalverteilung" << endl;
     cout << "Ende...........................1" << endl;
     cout << "Werte Berechnen................2" << endl;
     cout << "Ihre Wahl: ";
     cin >> wouhi;
     if ((wouhi < 1 ) || (wouhi > 2)) wouhi = 1;
    break;

case 1:
     cout << "Programmende " << endl;
     return EXIT_SUCCESS;
    break;

case 2:
   cout << "My eingeben: ";
   cin >> my;
   cout << " x eingeben: ";
   cin >> x;

   wert = nvt.NormalDistribution(my, x, 1);
   innen = 1 - wert;
   cout.precision(14);
   cout << "Test:" << fixed << wert << "    " << fixed << innen << endl;
   wouhi = 8;

  break;

 }while(wouhi != 1);

 return 0;
}

/******************* Ende des Hauptprogramms ******************/


Das ganze in ein Projekt einzubinden sollte kein Problem sein,
Schwierigkeiten bereitete die Methode double.Epsilon. Dafür habe ich
double Epsilon(void)
geschrieben.

Viel Spaß beim testen.


pn
Gast 
01.05.2017, 14:02
zitieren

Mach mit!

Wenn Dir die Beiträge zum Thread "Funktionen NORMVERT und NORMINV in c++ sourcecode!" gefallen haben oder Du noch Fragen hast oder Ergänzungen machen möchtest, solltest Du Dich gleich bei uns anmelden:



Registrierte Mitglieder genießen die folgenden Vorteile:
✔ kostenlose Mitgliedschaft
keine Werbung
✔ direkter Austausch mit Gleichgesinnten
✔ neue Fragen stellen oder Diskussionen starten
✔ schnelle Hilfe bei Problemen
✔ Bilder und Videos hochladen
✔ und vieles mehr...


Neuen Thread eröffnenNeue Antwort erstellen
Ähnliche BeiträgeRe:
Letzter Beitrag
Zerlegung in Primfaktoren, ein Sourcecode zum rumbasteln und lernen.
Hier ein kleiner Code um (natürliche) Zahlen in ihre Primfaktoren zu zerlegen. Man kann auch unter Angabe der Ober- und Untergrenze sich die Primzahlen anzeigen lassen. Die Funktion für den Test ob eine Primzahl vorliegt oder nicht, wurde...
[C++]von Guiseppe
0
866
06.01.2014, 18:44
Guiseppe
Scheinwerfer-Funktionen!?
Hallo Leute, erstmal Hi an Alle. Bin neu hier und seit kurzem Besitzer eines Civic´s `07. Ich wollte wissen ob der Civic die Funktion hat wenn man per Fernbedienung das Auto öffnet sich auch die Lichter einschalten bzw. wenn man ihn abschliesst sich...
von XBladerX
1
608
04.03.2008, 22:01
s-lab
Hash Funktionen C
Weiß jemand wie man das lösen könnte? Aufgabe: Liefert Zeiger auf den Wert (station_name aus der Struktur element der Listenimplementierung) zum übergebenen Schlüssel zurück. char &#x2A; ht_get&#40;hashtable &#x2A; ht, char &#x2A;...
[Allgemein]von cprogrammierung2022
0
131
16.06.2022, 07:29
cprogrammierung2022
anzeige der Funktionen unterdrücken
Hallo, ich verwende die parserextensions und mache für die Benutzer eine Vorlage. Dabei kann ein Benutzerbild angegeben werden. Ist keines Vorhanden, dann soll ein Standardbild verwendet werden. Mein Problem ist nun, dass der Code mit angezeigt...
von Enigmus
2
222
13.05.2009, 18:29
Enigmus
S2000 Tacho Funktionen
Hi S2000 Fahrer, ich habe den S2000 Tacho bei mir im Civic verbaut, alles funktioniert soweit. Könnt ihr mich mal über die Funktionen aufklären oder gibt es da ne Anleitung oder ne Quelle wo man sich einlesen kann? Was ist zB. Trip A, Trip B? Hab schon...
von fago88
16
1.890
03.02.2013, 14:49
speedeg
Komfort Funktionen wie aktivieren / deaktivieren ?!
Hallo, Wie aktiviere ich die Komfort funktion? Manchmal klappt es und manchmal nicht... Im Handbuch steht auf der Fernbedienung 1x kurz und dann gedrückt halten... Die andere frage ist, Kann ich damit immer nur beides zusammen aktivieren (Spiegel +...
von EJ9 Power
2
496
08.12.2018, 09:39
Lori-DC2
Tacho justieren und Funktionen nachrüsten
Moin Moin also ich hab da nen kleines Problem . Hab jetzt nen Facelift Tacho in mein Preface model eingebaut und wollte mal fragen ob man 1 den Tachostand justieren kann(will die orginalen KM wieder haben und wenn ja wie? 2 Kann mann den...
von R.O.N.I.N
0
381
09.02.2011, 20:43
R.O.N.I.N
Del sol ausfall einzelner elektrischer funktionen
Hay Leute. Nach längerer standzeit wollte ich den del sol starten. Batterie ist aber schon alt und etwas schwach.. Normalerweise überbrücke ich dann. Diesmal hab ich sie ausgebaut und Zuhause vollgeladen. Eingebaut,angschlossen, gestartet... Gut...
von ichbinmike92
1
133
28.01.2016, 12:33
Ninja II
Sitzheizungen fallen aus, Zussätzliche Funktionen gestört!
Sporadisch lassen sich die Sitzheizungen nicht einschalten, nach Betätigung erscheinen kurz die Symbole, dann hört man ein Relais abfallen und beide Symbole verschwinden, gleichzeitig ist das Sitzklima auf der Fahrerseite ohne Funktion, weiter...
von walter vedlin
2
451
29.01.2012, 18:32
walter vedlin
 Beispiel für die Erzeugung von Zufallszahlen, mit Quellcode für die Funktionen!
Hallo Leute! Hoffentlich habt Ihr den Rutsch ins neue Jahr gut überstanden. Da ich mich momentan mit meinen alten Programmen beschäftige, wie ich diese nach C++ und ins Linux Leap 42,1-Zeitalter portieren kann, habe ich tief in meiner...
[C++]von Guiseppe
0
498
12.01.2016, 09:40
Guiseppe
© 2004 - 2024 www.programmierer-forum.de | Communities | Impressum |