jeudi 21 décembre 2017

How to poperly sample truncated distirbutions

I am trying to learn how to sample truncated distributions. To begin with I decided to try a simple example I found here example

I didn't really understand the division by the CDF, therefore I decided to tweak the algorithm a bit. Being sampled is an exponential distribution for values x>0 Here is an example python code:

# Sample exponential distribution for the case x>0
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

def pdf(x):
        return x*np.exp(-x)

xvec=np.zeros(1000000)
x=1.
for i in range(1000000):
      a=x+np.random.normal()
      xs=x
      if a > 0. :
        xs=a
      A=pdf(xs)/pdf(x)
      if np.random.uniform()<A :
        x=xs
        xvec[i]=x

x=np.linspace(0,15,1000)
plt.plot(x,pdf(x))
plt.hist([x for x in xvec if x != 0],bins=150,normed=True)
plt.show()

Ant the output is: Correctly sampled pdf with the condition a > 0.

The code above seems to work fine only for when using the condition if a > 0. :, i.e. positive x, choosing another condition (e.g. if a > 0.5 :) produces wrong results.

Wrong sampling with the condition a>0.5

Since my final goal was to sample a 2D-Gaussian - pdf on a truncated interval I tried extending the simple example using the exponential distribution (see the code below). Unfortunately, since the simple case didn't work, I assume that the code given below would yield wrong results.

I assume that all this can be done using the advanced tools of python. However, since my primary idea was to understand the principle behind, I would greatly appreciate your help to understand my mistake. Thank you for your help.

# sample truncated Gaussian 
import numpy as np
from scipy.stats import multivariate_normal
RANGE=100000

a=3.01467E-02
b=1.32786E+00
a_range=[0.001,0.5]
b_range=[0.01, 2.5]
cov=[[8.8127990E-06,  5.3212327E-04],[  5.3212327E-04,  3.2322653E-02]]
cov_2=[[8.8127990E-06,  0.],[ 0.,  3.2322653E-02]]
x=a
y=b
for i in range(RANGE):
    dx,dy=np.random.multivariate_normal([a,b],cov_2)
    a_t=x+dx
    b_t=y+dy
    xs=x
    ys=y
# accept if within bounds
    if a_range[0]<a_t and a_t<a_range[1] and b_range[0]<b_t and b_t<b_range[1]:
        xs=a_t
        ys=b_t
    fr=multivariate_normal([a,b],cov)
    A=fr.pdf([xs,ys])/fr.pdf([x,y])
    if  np.random.uniform()<A :
        x=xs
        y=ys




Concept-Lite for random distribution engine (Cf. UniformRandomBitGenerator for raw bits)

I have some decades-old code for random number generation code that I wrote based on the work of George Marsaglia. I ran some tests and found that the "Mars" code, using his "Monty Python" scheme for standard normal deviates was 3 1/2 times faster than the std::normal_distribution that comes with Visual C++ 2017. I decided to make that code play nicely with std::random. (A generator for random bits is easy. There is an official concept called UniformRandomBitGenerator that fits the bill perfectly. Create custom distribution compatible with C++11 random header Freebie example below.)

My dj::normal_distribution "Monte Python" class is a drop-in replacement for std::normal_distribution, except I did not fancy implementing all the stuff that no-one will ever use.

I have read that just as UniformRandomBitGenerator is a subset of RandomNumberEngine that contains only the useful stuff, there is

[Q] a concept for a distribution generator that does not have all the tedious (and seldom needed) boilerplate of concept RandomNumberDistribution. Is there one?

I have searched in vain. If there is, I would be happy to publish the Monty Python code that satisfies the concept.

Freebie

namespace dj {

// George Marsaglia's long period (2^60) random number generator
// Satisfies UniformRandomBitsGenerator concept
class Mars_URNG {
public:

    Mars_URNG(uint32_t s1 = 0, uint32_t s2 = 0) {
        seed(s1, s2);
    }

    void seed(uint32_t s1 = 0, uint32_t s2 = 0) {
        z = good(s1); w = good(s2);
    }

    // The following four definitions satisfy the concept
    inline uint32_t operator() () {
        return znew() | wnew();
    }
    constexpr uint32_t min() { return 0; }
    constexpr uint32_t max() { return 0xffffffff; }
    using result_type = uint32_t;

private:
    static const uint32_t arby = 0xA5A5A5A5; // Arbitrary default seed
    // Zero is a baaad seed.
    static uint32_t good(uint32_t s) { return s ? s : arby; }
    uint32_t z;
    uint32_t w;
    inline uint32_t znew() { z = 36969 * (z & 0xffff) + (z >> 16); return z << 16; }
    inline uint32_t wnew() { w = 18000 * (w & 0xffff) + (w >> 16); return w & 0xffff; }
};
}




Android random Fragment load

I am currently programming my first Android app as part of a project at my university. Our group needs some help.

We want to program a game in which you have to ask interactively to answer questions. For this we have considered the following system.

Our questions are loaded from a server as JSON and stored in a list. To load the different questions with the different answer functions we thought we use fragments.

Our current structure looks like this:

-activities
--- GameActivity

-fragment
--- game1 (SimpleTap)
--- game2 (Compass)
--- game3 (Ball)

In the GameActivity the questions and the games(fragments) are loaded. Thereafter, a question is randomly selected and a fragment is randomly assigned and displayed for that question.

At the moment the fragments all looks like this: Example Fragment

The GameActivity looks like this:

public class GameActivity extends FragmentActivity {
private List<Question> questionList;
private Fragment fragmentGame;
private Integer randomQuestion;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    questionList = (List<Question>) getIntent().getSerializableExtra(TopicActivity.QUESTIONS);
    randomQuestion = randomQuestion();

    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.game_placeholder, randomGame(), "Game");
    fragmentTransaction.commit();
}

public Integer randomQuestion() {
    int random = ThreadLocalRandom.current().nextInt(0, questionList.size() - 1);
    return random;
}

public Fragment randomGame() {
    List<Fragment> questionGames = new Vector<Fragment>();
    questionGames.add(Fragment.instantiate(this, SimpleTap.class.getName()));
    questionGames.add(Fragment.instantiate(this, Compass.class.getName()));
    Collections.shuffle(questionGames, new Random(System.nanoTime()));
    fragmentGame = questionGames.get(1);
    return fragmentGame;
}

public List<Question> getQuestionList() {
    return questionList;
}

public Integer getRandomQuestion() {
    return randomQuestion;
}
}

One of the Fragments looks like:

public class SimpleTap extends Fragment {

private FragmentActivity listener;
private GameActivity activity;
private List<Question> questionList;
private TextView questionView;
private Button answer1Button, answer2Button, answer3Button, answer4Button;
private TextView answerRView;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof Activity){
        this.listener = (FragmentActivity) context;
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.game_simpletap, parent, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    activity = (GameActivity) getActivity();
    questionList = activity.getQuestionList();

    questionView = (TextView) view.findViewById(R.id.question);
    answerRView = (TextView) view.findViewById(R.id.answerR);

    String question = questionList.get(activity.getRandomQuestion()).getQuestion();
    String answer1 = questionList.get(activity.getRandomQuestion()).getAnswer1();
    String answer2 = questionList.get(activity.getRandomQuestion()).getAnswer2();
    String answer3 = questionList.get(activity.getRandomQuestion()).getAnswer3();
    String answer4 = questionList.get(activity.getRandomQuestion()).getAnswer4();

    answer1Button = (Button) view.findViewById(R.id.answer1);
    answer2Button = (Button) view.findViewById(R.id.answer2);
    answer3Button = (Button) view.findViewById(R.id.answer3);
    answer4Button = (Button) view.findViewById(R.id.answer4);

    questionView.setText(question);
    answer1Button.setText(answer1);
    answer2Button.setText(answer2);
    answer3Button.setText(answer3);
    answer4Button.setText(answer4);
}

@Override
public void onDetach() {
    super.onDetach();
    this.listener = null;
}

public void setAnswerR(String answerR){
    answerRView.setText(answerR);
}

public void questionAnswerd(View v) {
    String right = questionList.get(activity.getRandomQuestion()).getAnswerR();
    switch (v.getId()) {
        case R.id.answer1:
            String answer1 = "1";
            if(answer1.equals(right)){
                setAnswerR("Richtig");
            }
        case R.id.answer2:
            String answer2 = "2";
            if(answer2.equals(right)){
                setAnswerR("Richtig");
            }
        case R.id.answer3:
            String answer3 = "3";
            if(answer3.equals(right)){
                setAnswerR("Richtig");
            }
        case R.id.answer4:
            String answer4 = "4";
            if(answer4.equals(right)){
                setAnswerR("Richtig");
            }
        default:
            throw new RuntimeException("Diese Antwort gibt es nicht!");
    }
}
}

So what we need now is a random number generator that both randomly invites random fragments as a game but does not invite them again in one go.

Similarly, we need a query if the question is answered correctly. An approach to this can be seen in the fragment code. However, the output of the response must be passed to the GameActivity for the Random Generator to load a new fragment again.

It's really hard to explain exactly what we want to do because we do not really know if that makes sense at all. Maybe someone will understand us and can help us.

Thanks and best regards




How to create file in random directory

I'm trying to make the terminal make a bunch of files (it's on a loop), with random names, random size, random data, in random directories. By random directories I mean they could be anywhere on the computer




How to create a hill climbing algorithm

I have been following a book for learning python, and the book has one of the following challenge:

Self Check

Here’s a self check that really covers everything so far. You may have heard of the infinite monkey theorem? The theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type a given text, such as the complete works of William Shakespeare. Well, suppose we replace a monkey with a Python function. How long do you think it would take for a Python function to generate just one sentence of Shakespeare? The sentence we’ll shoot for is: “methinks it is like a weasel”

You’re not going to want to run this one in the browser, so fire up your favorite Python IDE. The way we’ll simulate this is to write a function that generates a string that is 28 characters long by choosing random letters from the 26 letters in the alphabet plus the space. We’ll write another function that will score each generated string by comparing the randomly generated string to the goal.

A third function will repeatedly call generate and score, then if 100% of the letters are correct we are done. If the letters are not correct then we will generate a whole new string.To make it easier to follow your program’s progress this third function should print out the best string generated so far and its score every 1000 tries.

I was able to implement this part of the challenge, with the following code: (I am new to python)

import random
target = 'methinks it is like a weasel'
target_len = 28

def string_generate(strlen):
 alphabet = 'abcdefghijklmnopqrstuvwxyz ' #26 letters of the alphabet + space
 res = ''
 for i in range(strlen):
  res += alphabet[random.randrange(27)]

 return res

def score_check(target,strlen):
 score = 0
 res = string_generate(strlen)
 for i in range(strlen):
  if res[i] == target[i]:
   score += 1
 return score, res

def progress_check():
 counter = 0
 score = 0
 res = ''
 while score != 28:
  score_temp, res_temp = score_check(target, target_len)
  counter += 1
  if score_temp > score:
   score, res = score_temp, res_temp
   print(res, score)
  else:
   score, res = score, res

 return res, score

progress_check()

It then have the following extra challenge:

Self Check Challenge

See if you can improve upon the program in the self check by keeping letters that are correct and only modifying one character in the best string so far. This is a type of algorithm in the class of ‘hill climbing’ algorithms, that is we only keep the result if it is better than the previous one.

However, I am not able to figure out what this hill climbing algorithim is, and how I would implement it into my existing piece of code.

Please point out any potential improvements to my code, and explain how to implement this hill climbing algorithm, thank you all so much!




Is it possible to force matlab to use a cryptographic PRNG?

I want to do simple simulation of random walk, however I want the random step to be as random as possible. Is it possible to use rand to create bits using crypto secrued PRNG?




Randomly picking by odds python [duplicate]

This question already has an answer here:

I want to make a program of AI with Thompson sampling. anyway lets say I have an array of functions [do1, do2, do3] and an array of probability: [prob1, prob2, prob3] (prob1 + prob2 + prob3 = 1) how do I it so I run a function that will pick randomly a function with different odds? (so if we run the program say 100000000 times so about prob1*100% of the time it will run do1) Thanks a lot!