GithubHelp home page GithubHelp logo

multinomial about mir-random HOT 23 CLOSED

libmir avatar libmir commented on May 29, 2024
multinomial

from mir-random.

Comments (23)

jmh530 avatar jmh530 commented on May 29, 2024 1

from mir-random.

9il avatar 9il commented on May 29, 2024

We don't, but we can review and accept a pull request with this random variable.

from mir-random.

jmh530 avatar jmh530 commented on May 29, 2024

This shouldn't be that hard to implement on your own given BinomialVariable. According to the r function's documentation [1], they calculate it as:

The rmultinom() algorithm draws binomials X[j] from Bin(n[j], P[j]) sequentially, where n[1] = N (N := size), P[1] = p[1] (p is prob scaled to sum 1), and for j ≥ 2, recursively, n[j] = N - sum(k=1, …, j-1) X[k] and P[j] = p[j] / (1 - sum(p[1:(j-1)])).

[1] https://stat.ethz.ch/R-manual/R-devel/library/stats/html/Multinom.html

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

Ok,

I'll be glad to contribute. I have started an implementation ported straight from GSL.
the code is here: https://gist.github.com/run-dlang/edc536446341a957256d1bfeeefaf1da
However, there is still some bug in it, if someone could have a look I'd greatly appreciate it.
After we get this working, It'll still need to be modified to conform with mir.random's API, but I believe that as is it is easier to test and make sure is working properly.

from mir-random.

jmh530 avatar jmh530 commented on May 29, 2024

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

Thanks @jmh530, for spotting the errors! It seems to be working fine now, I'll come up with some unit tests now to make sure it is probabilistically correct, i.e., the sampled values match theoretical expectations.

Meanwhile, should I use this as a code template, for the pull request?
I am new to D so I'll have to take some time to digest some of the specifics of the mir-random code.

from mir-random.

jmh530 avatar jmh530 commented on May 29, 2024

@fccoelho I would definitely take a look at variable.d to get a sense of how each of these are written, but ultimately it should be in ndvariable.d and follow what is going on there.

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

Thanks, it makes sense.

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

I have got some preliminary implementation done, but as it stands it is not compiling because I can't figure out how templates work to deduce argument types.

I could use some help here.

from mir-random.

jmh530 avatar jmh530 commented on May 29, 2024

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

Ok, I got it to work, thanks again @jmh530 .
For some reason I can get the test assert(x[0]+x[1]+x[2] == s); to work any Ideas of how I can get this simple line to run? It seems to be related to the way result is declared in the opCall (with scope)

from mir-random.

jmh530 avatar jmh530 commented on May 29, 2024

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

I should have added more information: it is not that the test fails. it fails to compile
here is the test as it currently stands:

nothrow @safe version(mir_random_test) unittest
{
    Random* gen = threadLocalPtr!Random;
    ulong s = 1000;
    auto rv = MultinomialVariable!(ulong,double)(s, [1.0, 5.7, 0.3]);
    ulong[3] x;
    rv(gen,x);
    assert(x[0]+x[1]+x[2] == s);
    assert(1==1);
}

when I try to dub test the package this is what I get:

Running ./mir-random-test-default 
core.exception.AssertError@source/mir/random/ndvariable.d(397): unittest failure
----------------
??:? _d_unittestp [0x559ccc585601]
source/mir/random/ndvariable.d:397 nothrow @safe void mir.random.ndvariable.__unittest_L390_C40() [0x559ccc576b35]
??:? void mir.random.ndvariable.__modtest() [0x559ccc578218]
??:? int core.runtime.runModuleUnitTests().__foreachbody2(object.ModuleInfo*) [0x559ccc5a0d23]
??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)).__lambda2(immutable(object.ModuleInfo*)) [0x559ccc583d7e]
??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))).__foreachbody2(ref rt.sections_elf_shared.DSO) [0x559ccc58b6d5]
??:? int rt.sections_elf_shared.DSO.opApply(scope int delegate(ref rt.sections_elf_shared.DSO)) [0x559ccc58b868]
??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))) [0x559ccc58b661]
??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)) [0x559ccc583d55]
??:? runModuleUnitTests [0x559ccc5a0b01]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x559ccc5875e0]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x559ccc58756b]
??:? _d_run_main [0x559ccc5874d1]
??:? main [0x559ccc4f9015]
??:? __libc_start_main [0x7f263687a09a]
1/16 unittests FAILED
Program exited with code 1

Line 397 is exactly that assert.

from mir-random.

jmh530 avatar jmh530 commented on May 29, 2024

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

Fixed everything apparently. I've just created a PR.

from mir-random.

9il avatar 9il commented on May 29, 2024

The PR should pass before review

from mir-random.

9il avatar 9il commented on May 29, 2024

Ah, ok, we need to update CI first

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

From what I could find from the Travis error log, the tests are failing when building for x86 architecture:

 dub test -ax86
Generating test runner configuration 'mir-random-test-default' for 'default' (library).
Excluding package.d file from test due to https://issues.dlang.org/show_bug.cgi?id=11847
Excluding package.d file from test due to https://issues.dlang.org/show_bug.cgi?id=11847
Excluding package.d file from test due to https://issues.dlang.org/show_bug.cgi?id=11847
Performing "unittest" build using dmd for x86.
mir-core 1.0.2: target for configuration "library" is up to date.
mir-algorithm 3.5.6: target for configuration "default" is up to date.
mir-linux-kernel 1.0.1: target for configuration "library" is up to date.
mir-random 2.2.6+commit.5.gee30a11: building configuration "mir-random-test-default"...
source/mir/random/ndvariable.d(344,38): Error: template mir.random.variable.binomialVar cannot deduce function from argument types !()(ulong, double), candidates are:
source/mir/random/variable.d(1750,20):        mir.random.variable.binomialVar(T)(size_t r, in T p) if (isFloatingPoint!T)
source/mir/random/ndvariable.d(388,11): Error: template instance `mir.random.ndvariable.MultinomialVariable!(ulong, double).MultinomialVariable.opCall!(MersenneTwisterEngine!(uint, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u))` error instantiating
source/mir/random/ndvariable.d(400,11): Error: template instance `mir.random.ndvariable.MultinomialVariable!(ulong, double).MultinomialVariable.opCall!(MersenneTwisterEngine!(uint, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u))` error instantiating
dmd failed with exit code 1.

I can't decifer what is going on here, if anyone can point me to what this error is about, I can make a quick commit, fixing this issue.

from mir-random.

9il avatar 9il commented on May 29, 2024

ulong is always 64 bit. size_t is 32 bit on x86 and can't accept ulong argument.

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

I suspected as much. I have changed the N argument in the MultinomialVariable class frm ulong to uint and now I am getting an issue building for x86_64:

dub test
Generating test runner configuration 'mir-random-test-default' for 'default' (library).
Excluding package.d file from test due to https://issues.dlang.org/show_bug.cgi?id=11847
Excluding package.d file from test due to https://issues.dlang.org/show_bug.cgi?id=11847
Excluding package.d file from test due to https://issues.dlang.org/show_bug.cgi?id=11847
Performing "unittest" build using dmd for x86_64.
mir-core 1.0.2: target for configuration "library" is up to date.
mir-algorithm 3.5.6: target for configuration "default" is up to date.
mir-linux-kernel 1.0.1: target for configuration "library" is up to date.
mir-random 2.2.6+commit.5.gee30a11: building configuration "mir-random-test-default"...
source/mir/random/ndvariable.d(332,17): Error: no type for typeid(threadLocal)
source/mir/random/ndvariable.d(344,26): Error: cannot implicitly convert expression rv.opCall(threadLocal()) of type ulong to uint
source/mir/random/ndvariable.d(387,11): Error: template instance `mir.random.ndvariable.MultinomialVariable!(uint, double).MultinomialVariable.opCall!(MersenneTwisterEngine!(ulong, 64LU, 312LU, 156LU, 31LU, 13043109905998158313LU, 29LU, 6148914691236517205LU, 17LU, 8202884508482404352LU, 37LU, 18444473444759240704LU, 43LU, 6364136223846793005LU))` error instantiating
source/mir/random/ndvariable.d(399,11): Error: template instance `mir.random.ndvariable.MultinomialVariable!(uint, double).MultinomialVariable.opCall!(MersenneTwisterEngine!(ulong, 64LU, 312LU, 156LU, 31LU, 13043109905998158313LU, 29LU, 6148914691236517205LU, 17LU, 8202884508482404352LU, 37LU, 18444473444759240704LU, 43LU, 6364136223846793005LU))` error instantiating
dmd failed with exit code 1

It seems that the MersennTwisterEngine is expecting an ulong !

this seems to be a catch-22 type of situation..... If I set N to ulong I can't run it on x86 if I set it to uint, I cant run on x86_64

from mir-random.

jmh530 avatar jmh530 commented on May 29, 2024

@fccoelho Where the ulongs or uints are causing errors, try changing them to size_t.

from mir-random.

fccoelho avatar fccoelho commented on May 29, 2024

Thanks, @jmh530. That solved it.

Now the PR is passing the Appveyor test but failing on Travis, but the log shows that the error is not about the D code but the build script.

from mir-random.

9il avatar 9il commented on May 29, 2024

released in v2.2.8

from mir-random.

Related Issues (20)

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.