Giter VIP home page Giter VIP logo

randomvectorwithfixedsum's Introduction

RandomVectorWithFixedSum

Python implementation of the random vector with fixed sum.

Python的随机数很容易获得,只需调用numpy.random里的函数,常用的如

numpy.random.rand(d0, d1, …, dn) # 产生均匀分布的随机数
numpy.random.randn(d0, d1, …, dn) # 产生标准正态分布随机数
numpy.random.randint(low[, high, size, dtype]) # 产生随机整数

该模块也可以产生具有分布的随机数,常用的如

beta(a, b[, size]) # 贝塔分布
binomial(n, p[, size]) # 二项分布
chisquare(df[, size]) # 卡方分布

但如果想要具有约束的随机数,就比较困难。该仓库实现了带和约束的随机向量产生方法,并且向量的元素也是具有范围约束的,具体地说,即:

返回m个长度为n随机向量,每个随机向量元素的值在[a, b]范围内,并且每个随机向量元素的和为s;方法的返回值为大小为nxm的矩阵X,该矩阵满足numpy.sum(X, 0)=[s, s, s,...]。

具有范围约束的带和约束的随机向量产生方法在很多问题中会被用到,但是numpy并没有将其集成进来。本实现具有高效、快速的特点,算法复杂度低,能够很快产生很大的随机矩阵X,本算法是基于该MATLAB实现的改写。

此外,本仓库还附带了该算法的一个数学应用,描述如下:

个在上均匀分布的随机变量,它们满足约束,其中是某个常数,求概率

该问题对于一般的情形无法使用解析解有效描述,只能依赖于数值解,对于n=10的情形数值解如下:

randomvectorwithfixedsum's People

Contributors

gasharper avatar

Stargazers

Gexin Huang avatar

Watchers

James Cloos avatar  avatar Gexin Huang avatar

Forkers

gexinh

randomvectorwithfixedsum's Issues

there is a issue I have encountered

image
Thank u for your script, it saves a lot of time for me to drain my brain to code this function! However, I have encountered a problem recently. When I generated a series of four random-fixed-sum values, the first value of every vector will intend to be very small and the console will report some issue like this:

72: RuntimeWarning: invalid value encountered in multiply tmp1=w[i-2, 1:i+1] * s1[0:i] / i
77: RuntimeWarning: invalid value encountered in true_divide t[i-2, 0:i] = (tmp2/tmp3)tmp4 + (1-tmp1/tmp3)(1-tmp4)
92: RuntimeWarning: invalid value encountered in less_equal e = rt[n-i-1, :] <= t[i-1, j-1]

I'm willing to fix this issue but I'm not familiar with the theory of this random-fixed-sum code. Thus, I would be very appreciative for your fix and update!

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.