mardi 20 juillet 2021

Efficient block bootstrap of integer sequences

I'm trying to block bootstrap samples for Monte-Carlo simulation and need to generate a large array of index values (integers) containing blocks in Python. I need this to be very fast but cannot figure out how to vectorize it.

I want to generate a large number of paths, where each path contains a sequence of integers of length L. Suppose I have an array of integers (representing an index) form 0 to N, from which I will sample randomly to construct each path. When I sample, I choose a random integer i from 0 to N, and then populate the path with i,i+1,i+2..,i+w for some window w. I then choose another random starting index value and continue to populate the path with the new window, repeating until the path is fully populated. I do this for all paths.

I'm wondering if there is a way to speed this method up without having to loop over each path, since I intend to generate a very large number of paths (millions)

An example of my for loop method is below:

paths = 10000
path_length = 500
window_length = 5
index = np.arange(0,5000)
simulated_values = np.zeros([paths,path_length])
n_windows = int(np.ceil(path_length/window_length))
for i in range(0, paths):
    temp=[]
    for n in range(0, n_windows):
        random_start = random.randint(0, len(index) - path_length)
        temp.extend(range(random_start, random_start + window_length))
    simulated_values[i,:] = temp
print(simulated_values) 



How can I assign the pen color of a turtle using a web-safe color stored as a string in a list in a dictionary?

I'm using Python v3.9.5 to write a plotting program, something I've posted on before. I have most of it done, but I'm running into a lot of snags with color scheme selection, something I really want to add to the program since not all colors are good for plotting points, and I want my plotter to be as customizable as possible. Like I did in my previous question, I'll be demonstrating everything with a color scheme called 'Red'. To start off, the guys in the last question wanted me to define the color scheme 'Red' like this:

ColorSchemes = {}
ColorSchemes['Red'] = ["IndianRed", "LightCoral", "Crimson", "Red", "FireBrick"]

This is defined within a function that takes the parameter "scheme", which is defined in a global variable called "colorscheme".

colorscheme = input("What color scheme would you like to use? ")
#Later...
getColors(colorscheme)

Here is the getColors() function:

def getColors(scheme):
    ColorSchemes = {}
    ColorSchemes['Red'] = ["IndianRed", "LightCoral", "Crimson", "Red", "FireBrick"]
    random.shuffle(ColorSchemes[scheme])
    plotter.color(str(ColorSchemes[scheme(len([0]))]))

You can see that the place where I attempt to call the first element of the chosen color scheme is an absolute mess; the shell insists I can't call a string, even though the string should be a perfectly valid name of a web-safe color. It won't even allow me to use print() to check how the rest of the code is working. I'd hoped to get the colors to cycle through as well, i.e. going from Indian Red to Light Coral to Crimson in order, but I'd dropped that idea and tried to use random.shuffle() to at least make them random. I was originally going to pick a random color from the lists by putting a randint() where the index goes, except I couldn't do that with a range, because the largest color scheme list holds 50 colors and the program would pitch a fit if I chose a color scheme like 'Red' that only has five colors and then tried to find a color 17 that doesn't exist. Instead, I tried to shuffle the colors and pick the first color of the mixed-up list. This would run every time I plotted a point, in theory changing the point color each time.

Currently, the rest of my program works; before choosing the color, a function correctly draws a grid, and afterward, another function plots a point. Everything works smoothly, except for the fact that now my points stay black instead of being colored. I've already tried defining the color schemes elsewhere, integrating this function with the point plotting function (actually what I had before), and experimenting with other things like using randint(). If there's anything else I should try, another method to get randomized or cycling colors, or a better, different idea entirely, I'm all ears. Thanks in advance for your suggestions!




What's the best way to approach making a loot box program using C#?

I'm learning C#, and something that I really want to learn how to make is a loot box program. The best way I can think of to make this is making an enum and assigning numbers to the items in the enum, and then randomly generating a number. Then if that number is within the scope of one of the items in that enum, you receive the respective item.

Something else that I would love to learn how to make is something like a Pokémon game. If something happens, like running around in the grass, and then you encounter something, how does the program select which Pokémon to give you? Is it similar to what I described above?




Loop over to create new variables from uniform dataframe

My problem is the following

I want to create variables e_1, e_2, e_3, ... , e_50 which are all composed of 100 draws from the uniform[-1,1]

This means e_1 is a vector of 100 draws from U[-1.1], e_2, .., e_50 as well.

Here is what I thought I could do :

periods <- c(1:50)
people <- c(1:100)
for (t in periods){
sprint('e_', t) <- runif(100, -1,1)
}

This did not work, and i am really not sure how to change it to obtain what I want.

Thank you so much for your help!!




Python random seed that returns the same random results on all instances

I am currrently using:

>>> import random
>>> foo = [1,2,3,4,5,6,7,8,9]
>>> random.Random(123).shuffle(foo)
>>> foo
[7, 4, 2, 6, 5, 8, 3, 9, 1]

But when ran on different instances this will return different results. Is there any way to have them return the same results no matter the instance?




Javascript unique array [closed]

Suppose , I have an array , which has 21 items . I want to randomly separate them in three unique and different arrays . What the code will be ?




Can i sample sets of data within a dataframe without selecting the same set twice (without replacement)?

I am fairly new to python and i would like to sample sets of data in the following dataframe by their group, without selecting the same group twice. The code i have written does sample the sets of data correctly, however, it can select the same set twice.

please note: the following data is testing data and the actual data i am using the code on is much larger in size and therefore using indexes will not be possible.

DATA:

d={'group': ['A','A','A','B','B','B','C','C','C','D','D','D','E','E','E'], 'number': [1,2,3,1,2,3,1,2,3,1,2,3,1,2,3],'weather':['hot','hot','hot','cold','cold','cold','hot','hot','hot','cold','cold','cold','hot','hot','hot']}```
df = pd.DataFrame(data=d)
df
group   number  weather
A       1       hot
A       2       hot
A       3       hot
B       1       cold
B       2       cold
B       3       cold
C       1       hot
C       2       hot
C       3       hot
D       1       cold
D       2       cold
D       3       cold
E       1       hot
E       2       hot
E       3       hot

MY CODE

df_s=[]
for typ in df.group.sample(3,replace=False):
    df_s.append(df[df['group']==typ])
df_s=pd.concat(df_s)
df_s

OUTCOME

group   number  weather
E       1       hot
E       2       hot
E       3       hot
E       1       hot
E       2       hot
E       3       hot
D       1       cold
D       2       cold
D       3       cold

The outcome should give 3 different groups data however as can be seen there is only 2 (E & D) meaning the code can select the same group more than once.