Setting boundary limits to multiple operations with random number generators - python

I believe that my problem is really straightforward and there must be a really easy way to solve this issue, however as I am quite new with Python, I could not sort it out by my own.
I will post a made up example that I am using than the complex script which I am currently working on in case you want to test by yourself. Please, consider the following:
import numpy as np
nData = 100
sigma_alpha = np.array([1,1])
alpha = [-23,0]
data_alpha1 = np.random.randn(nData)*sigma_alpha[0]+alpha[0]
data_alpha2 = np.random.randn(nData)*sigma_alpha[1]+alpha[1]
My issue is that I have to limit data_alpha1 and data_alpha2 to -25 as lower limit and 25 as upper limit. That means, all the elements on both arrays have to be in between the aforementioned values. So, the solution that I am looking for has also to involve a case where all the elements of data_alpha1,2<25, as the following, where multiple values will be beyond 25:
nData = 100
sigma_alpha = np.array([1,1])
alpha = [25,0]
data_alpha1 = np.random.randn(nData)*sigma_alpha[0]+alpha[0]
data_alpha2 = np.random.randn(nData)*sigma_alpha[1]+alpha[1]
The variable alpha is in a loop, so it has a dynamic value and is constantly being update.
To sum up: what I have been trying to figure out is a way to make sure that data_alpha1 and data_alpha2 returns only values inbetween -25 and 25, and in case, any value doesn't respect the condition imposed, then it should be set to the closest boundary value which it surpasses. Like, if an element of alpha_data1 <-25, then it should be replaced by -25.
Hope that I managed to be succinct and precise. I would really appreciate your help on this one!

Like this:
data_alpha1[data_alpha1 > 25] = 25
data_alpha1[data_alpha1 < -25] = -25

Related

Unknown Length Array, Assigning Any Part Of The Array Any Time

I am working with Q-Learning and want a 3D policy gradient that is completely empty until the the AI needs to access it. This is because my state is three inputs that each could be any integer from 1 to infinity, each number above 1 being increasingly less probable.
Hopefully this is possible. I am also not looking for the code to be handed to me, just hope someone can point me in the right direction.
policyGradient = [][][]
policyGradient[5][10][15] = 0.5
print(policyGradient[5][10][15]) // Expected output being 0.5.
Thank you!
its a bit hacky
import collections
magic = lambda:collections.defaultdict(magic)
dd = magic()
dd[5] = 6
dd[6][7][7]=67
print(dd[6][7][7])
You could use a dict-of-dict-of-dicts, but if you don't need to index on any particular state input, you could just use a dict with tuples of keys:
gradient = dict()
gradient[5,10,15] = 0.5

Returning incorrect values with complex numbers in Python

I'm working a physics problem with complex numbers and think I'm setting everything up correctly but am obviously not doing something right along the way. It could be that I'm either not using the right equations or that I'm unfamiliar with how Python's handling the math, and I'm pretty sure I'm using the right stuff. I've already worked a problem using the same kind of procedure and got the correct value, so substituting my new values should
Given f = 1000, SWR = -5.9, L = 0.081, I apparently should be getting z = 1.4 - 0.23j.
Here's what I'm doing:
import numpy as np
import cmath
f = 1000 #frequency
SWR = -5.9
L = 0.081
w = 2*f*np.pi #angular frequency
c = 343 #speed of sound in air
k = w/c #wavenumber
BA = (SWR-1)/(SWR+1) #given
theta = 2*k*L-np.pi #given
z = (1+BA*np.e**(1j*theta))/(1-BA*np.e**(1j*theta)) #given
print(z)
This gives me z = (-4.699946746470462-2.3316919882323677j), obviously not what I'm being told is the correct value.
I've gone over this multiple times now and can't find anything wrong. I just again worked through the problem I already got correct and made the minor substitutions to fit these given values, and I'm still getting the returned value of z. I don't want to tell my professor his "check that your code is giving the correct results" result is wrong, but...
Am I missing something?
E: Apologies for the rough display, but I'm not sure I can type in LaTeX here. The following are what I'm working with. Furthermore, the final image shows that I worked basically the same problem correctly and that I should be able to just make some substitutions to work this one. Also note that in my code, z is actually z divided by the rhocS quantity. I'm after that, don't need to know their values.
Equation for z, BA, theta, and the worked similar problem

Geometric rounding with numpy/quantize?

I've got a pandas series of data which is a curve.
I want to round it in such a way as to make it 'stepped'. Furthermore, I want the steps to be roughly within 10% of the present value. (Another way of putting this is I want the steps to increase in increments of 10%, i.e. geometrically).
I've written something that's iterative and slow:
def chunk_trades(A):
try:
last = A[0]
except:
print(A)
raise
new = []
for x in A.iteritems():
if not last or np.abs((x[1]-last)/last) > 0.1:
new.append(x[1])
last = x[1]
else:
new.append(last)
s = pd.Series(new, index=A.index)
return s
I don't want to use this code.
I'm trying to find a faster, pythonic way of doing this. I've tried using numpy.digitize() but I don't think that's what I'm looking for. Any ideas for how best to approach this?
OK, I think the solution should be something like:
np.exp(np.around(np.log(np.abs(j)), decimals=1)) * np.sign(j)
Map to logarithmic space, do the rounding, transform back.

ChiSquare calculation returning all zeros

EDIT: after more trial and error, I figured out that for some reason, python says that 1/52 is 0, can anyone explain me why, so I can avoid this problem in the future?
I've been struggling with a script for a while now, mainly because me or my fellow students simply can't find out what's wrong with it.
Trying to keep things simple, we've got data and a model and we have to rescale some of the datapoints to the model and then do a chi2square minimalization in order to find the best rescaling factor.
I've tried multiple things already. Tried putting everything in 1 loop, when that didn't work, I tried splitting the loops up etc.
The relevant part of my code looks like this:
#Here I pick the values of the model that correspond to the data
y4 = np.zeros((len(l),1))
for x in range(0,len(l)):
if l[x] < 2.16:
for y in range(0,len(lmodel)):
if lmodel[y] == l[x]:
y4[x] = y2[y]
elif lmodel[y] < l[x] < lmodel[y+1]:
y4[x] = (y2[y] + y2[y+1])/2
else:
y4[x] = y1[x]
#Do Chi2 calculation
#First, I make a matrix with all the possible rescaled values
chi2 = np.zeros((200,1))
y3 = np.zeros((len(l),len(chi2)))
for z in range(0,len(chi2)):
for x in range(0,len(l)):
if l[x] < 2.16:
y3[x,z] = y1[x]*10**(0.4*Al[x]*z/100)
else:
y3[x,z] = y1[x]
#Here I calculate the chisquare for each individual column and put it in the chi2 array
dummy = np.zeros((len(l),1))
for x in range(0,len(chi2)):
for t in range(0, len(l)):
dummy[t] = (1/52)*((y3[t,x] - y4[t])/fle[t])**2
chi2[x] = np.sum(dummy)
The thing is that no matter what I try, for some reason, my dummy array is always all zeros, making every single chi square value 0.
I've tried making 'dummy' a matrix and summing afterwards, I've tried printing individual values for the calculation of the dummy[t]'s, and some of them were 0 (as expected), some weren't, so logically, if the individual values aren't all 0, neither should every value in dummy be.
I just can't find where I go wrong, and why I keep getting arrays of zeros.
In Python 2 (which most people are still using), 1 / 52 is an integer division, so returns 0. You can fix it by explicitly using floating point numbers, e.g. 1.0 / 52.
In Python 3, this is no longer true--dividing two integers can return a float.

fill missing values in python array

Using: Python 2.7.1 on Windows
Hello, I fear this question has a very simple answer, but I just can't seem to find an appropriate and efficient solution (I have limited python experience). I am writing an application that just downloads historic weather data from a third party API (wundergorund). The thing is, sometimes there's no value for a given hour (eg, we have 20 degrees at 5 AM, no value for 6 AM, and 21 degrees at 7 AM). I need to have exactly one temperature value in any given hour, so I figured I could just fit the data I do have and evaluate the points I'm missing (using SciPy's polyfit). That's all cool, however, I am having problems handling my program to detect if the list has missing hours, and if so, insert the missing hour and calculate a temperature value. I hope that makes sense..
My attempt at handling the hours and temperatures list is the following:
from scipy import polyfit
# Evaluate simple cuadratic function
def tempcal (array,x):
return array[0]*x**2 + array[1]*x + array[2]
# Sample data, note it has missing hours.
# My final hrs list should look like range(25), with matching temperatures at every point
hrs = [1,2,3,6,9,11,13,14,15,18,19,20]
temps = [14.0,14.5,14.5,15.4,17.8,21.3,23.5,24.5,25.5,23.4,21.3,19.8]
# Fit coefficients
coefs = polyfit(hrs,temps,2)
# Cycle control
i = 0
done = False
while not done:
# It has missing hour, insert it and calculate a temperature
if hrs[i] != i:
hrs.insert(i,i)
temps.insert(i,tempcal(coefs,i))
# We are done, leave now
if i == 24:
done = True
i += 1
I can see why this isn't working, the program will eventually try to access indexes out of range for the hrs list. I am also aware that modifying list's length inside a loop has to be done carefully. Surely enough I am either not being careful enough or just overlooking a simpler solution altogether.
In my googling attempts to help myself I came across pandas (the library) but I feel like I can solve this problem without it, (and I would rather do so).
Any input is greatly appreciated. Thanks a lot.
When I is equal 21. It means twenty second value in list. But there is only 21 values.
In future I recommend you to use PyCharm with breakpoints for debug. Or try-except construction.
Not sure i would recommend this way of interpolating values. I would have used the closest points surrounding the missing values instead of the whole dataset. But using numpy your proposed way is fairly straight forward.
hrs = np.array(hrs)
temps = np.array(temps)
newTemps = np.empty((25))
newTemps.fill(-300) #just fill it with some invalid data, temperatures don't go this low so it should be safe.
#fill in original values
newTemps[hrs - 1] = temps
#Get indicies of missing values
missing = np.nonzero(newTemps == -300)[0]
#Calculate and insert missing values.
newTemps[missing] = tempcal(coefs, missing + 1)

Categories