Code Monkey home page Code Monkey logo

randlib's Introduction

RandLib

Build Status CodeFactor

With RandLib one can easily work with probability distributions. One of the major advantages of this library (apart from being free and open-source) is that it doesn't require any additional packages. All you need is C++17 compiler support.

What can be done via RandLib? Here are some useful examples:

  • Fast sampling. For instance, generate million variates from standard normal distribution:
NormalRand X(0, 1);
std::vector<double> data(1e6);
X.Sample(data);

alt tag

  • Calculate moments and other properties:
LogNormalRand X(1, 1);
std::cout << " Mean = " << X.Mean()
          << " and Variance = " << X.Variance()
          << "\n Median = " << X.Median()
          << " and Mode = " << X.Mode()
          << "\n Skewness = " << X.Skewness()
          << " and Excess kurtosis = " << X.ExcessKurtosis();

alt tag

Mean = 4.48169 and Variance = 34.5126
Median = 2.71828 and Mode = 1
Skewness = 6.18488 and Excess Kurtosis = 110.936
  • Fitting parameters using different estimators:
using std::cout;

NormalRand X(0, 1);
std::vector<double> data(10);
X.Sample(data);
cout << "True distribution: " << X.Name() << "\n";
cout << "Sample: ";
for (double var : data)
    cout << var << "  ";
cout << "\n";

/// Bayesian estimation
NormalInverseGammaRand prior(0, 1, 1, 1);
NormalInverseGammaRand posterior = X.FitBayes(data, prior);
cout << "Bayesian estimator: " << X.Name() << "\n";
cout << "(Posterior distribution: " << posterior.Name() << ")\n";

/// Uniformly minimum variance unbiased estimator
X.Fit(data, true);
cout << "UMVU estimator: " << X.Name() << "\n";

/// Maximum-likelihood estimator
X.Fit(data);
cout << "Maximum-likelihood estimator: " << X.Name() << "\n";

alt tag

True distribution: Normal(0, 1)
Sample: -0.328154  0.709122  -0.607214  1.11472  -1.23726  -0.123584  0.59374  -1.20573  -0.397376  -1.63173
Bayesian estimator: Normal(-0.283042, 0.951348)
(Posterior distribution: Normal-Inverse-Gamma(-0.283042, 11, 6, 4.75674))
UMVU estimator: Normal(-0.311347, 0.82504)
Maximum-likelihood estimator: Normal(-0.311347, 0.742536)

For documentation look up here

randlib's People

Contributors

astralord avatar pashaosipyants avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

randlib's Issues

Some of the functions declared constexpr are not cross-compilable

I don't know if this is directly pertinent to you, depending on what platforms you want to support. If it's not feel free to close the issue.

There are a multitude of places throughout the code base that use the identifier constexpr, but make calls to std::pow, std::log, and std::sqrt in the definitions. These are not actually defined as constexpr in the c++ standard (see this stackoverflow post) and as such any standard compiler other than GCC will throw errors when trying to compile the library.

I have made a workaround for this on my personal fork of this repository using a few header only files from the kthohr/gcem library. I don't know if you want to bring in external libraries but it is at least worth looking at if you need a launch point. Likewise, you are welcome to review my fork for reference. If you want I will be happy to open a pull request from that fork here, but there are significant modifications to the structuring and include paths on mine (It's currently a work in progress).

Implement log-stable distribution

Most of the properties of log-stable distribution are easy to derive using the relationship with stable distribution. However, the problem arises with characteristic function, which can't be expressed analytically and is hard to compute numerically due to singularity of probability density function at point 0 in some cases (e.g. Log-Cauchy distribution). All ideas are welcome.

Change Fit interface

Use Fit() for MLE and Fit(unbiased = true) for UMVU. Remove method of moments

Problems

cloned this repository.
But in Qt creator has problem : no viable constructor or deduction guide for deduction of template arguments of 'NormalRand';
Wrote code according your manual
NormalRand X(0, 1);
std::vector data(1e6);
X.Sample(data);

Implement pdf and cdf for stable distribution

The problems are with

  • alpha close to 1 with non-zero beta
  • beta close to 0, when alpha = 1
  • asymptotic series expansion for alpha = 1 and non-zero beta
  • cdf representation for short tails (beta = 1)

Severe integer overflow occuring, leading to undefined behavior

200896062499134299656951336898466838917540340798867777940435335160044860953395980941180138112097309735631594101037399609671032132186331495273609598531966730972945653558819806475064353856858157445040809209560358463319644664891114256430017824141796753818192338642302693327818731986039603200000000000000000000000000000000000000000000.l,
9680322675255249156123346514615331205418161260462873360750859919944104623425228207640470674933540169424682360525991982916161596983449594045525553704253602287443197783274656957056546338783001340434094795097553229620273057440272298773179365935914105128629426348958748638226084106818484328004851174161755668480000000000000000000000000000000000000000000000.l,
788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000.l,
105823620292236563784274284243348353057589905787169019562352737522144487532400210147849369011714673954768265316577892528273760626189481169051055226066650741189573897273684791411180134039439160066561895838501000817711682625725670477616267598661259194975646029749546282594356217374097544153589482020891750774735012558313460846824864172030239122128896000000000000000000000000000000000000000000000000000.l,
22838603359146414573972658651153337270429730715462287017736347161260276926030248458777765497919211029457065581960747795750095505232241970499561769723020565876672261660609763234049775547325430135571331468257475537994508495233770658945310210552725163342784668756149049213658078338458534285571551800849578848226429898670032945513859929938621783523490272646966918544936140800000000000000000000000000000000000000000000000000000.l,
7758587304686725201813174298892781442413952130995533365303964524344944412641389739603152000644515957408814002319492032321234250506968028455594445689972313374305301019340949789291189972149450405025159624155827152329676580440959428615802893638146558163235483142136540783687811997927615346859658417205832954125915861983307177232587595821512723429698627780530255874167602077755356592824804966400000000000000000000000000000000000000000000000000000000.l,
4067885363647058120493575921486885310172051259182827146069755969081486918925585104009100729728348522923820890245870098659147156051905732563147381599098459244752463027688115705371704628286326621238456543307267608612545168337779669138759451760395968217423617954330737034164596496963986817722252221059768080852489940995605579171999666916004042965293896799800598079985264195119506681577622056215044851618236292136960000000000000000000000000000000000000000000000000000000000.l,
3232856260909107732320814552024368470994843717673780666747942427112823747555111209488817915371028199450928507353189432926730931712808990822791030279071281921676527240189264733218041186261006832925365133678939089569935713530175040513178760077247933065402339006164825552248819436572586057399222641254832982204849137721776650641276858807153128978777672951913990844377478702589172973255150283241787320658188482062478582659808848825548800000000000000000000000000000000000000000000000000000000000000.l,

There is no way these values could be properly represented as the long double type you defined them as. See this article from GeeksforGeeks for a simple reference on why this occurs and how this could be affecting your intended values. And this StackOverflow post is pertinent because of the type declaration.

Implement special functions

  • logarithm of modified Bessel function of the first and second kind

  • lower and upper incomplete gamma functions (and their logarithms)

  • Marcum-Q and Marcum-P functions

  • erfinv and erfcinv

  • incomplete beta function

Unit-testing

  • maximum likelihood fit: f(a-eps) < f(a) < f(a+eps)

  • mode value: f(x-eps) < f(x) < f(x+eps)

  • pdf and logpdf coherence: f(x) = exp(logf(x))

Use C++17 simplifications

  • Use structured bindings, e.g.
    auto [a, b, c] = array
    instead of
    std::tie(a, b, c) = array
    Search by DoublePair or std::tuple. This could also be useful for loops through tuple arrays (if one uses std::map in pdf for example)
  • Init statements for if / switch (look for temp variables)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.