use cells in one dataframe as inputs in another
I want to simulate a portfolio performance. Currently just trying to make one iteration work:
df looks like this:
Wincent Systema Nickel Arb Stratosphere Tellurian
1 6.78 0.8 0.99 13.52 14.89
2 4.54 1.09 0.83 8.94 8.27
3 13 70 10 6 1
where row:
1 is volatility (or SD)
2 is return (ER)
and 3 is portfolio weight (in percent)
What I would like to get to is another df, let's call it onesim
with the same column names, just onerow under each one and a randomly generated number according to the SD and ER multiplied by the weight
I tried something like this:
onesim=float(np.random.normal(loc=df.iloc[1],scale = df.iloc[0], size= 1) * (df.iloc[2]/100))
but am getting :
ValueError: Output size (1,) is not compatible with broadcast dimensions of inputs (5,).
Posted Apr 8, 2022, 2:09 PM UTC
03
I believe there are other issues afoot, or at least improvements that could be made given the description of what you want. But the ultimate cause of your error is that you're effectively passing in 5 means (
loc) and 5 standard deviations (scale) but telling it to generate just one number (size). So how is it to know which mean and standard deviation to use?If you remove
size=1, it works, and if you just pass in a single number tolocandscale, it works. It's the mismatch between those two things that is resulting in your dimensions mismatch error.oh. I took size to mean how many runs to generate so thought since I only wanted 1 (for each column) that's what I need to put. Thanks for your reply!
Nope. When passed a scalar (i.e., a single integer),
sizecontrols how many numbers get generated.