I've been running into this problem with pyfolio where I just want to try out the example their github has here: https://quantopian.github.io/pyfolio/notebooks/bayesian/
the program runs through everything and takes awhile to calculate everything but in the end I get this
ValueError: Wrong number of items passed 4, placement implies 40
here is my current code:
import pandas_datareader as web
import pyfolio as pf
import datetime
start = datetime.datetime(2018, 10, 1)
end = datetime.datetime(2018, 12, 1)
stock_rets = web.DataReader("AAPL", 'yahoo',start , end)['Adj Close']
out_of_sample = stock_rets.index[-40]
pf.create_bayesian_tear_sheet(stock_rets, live_start_date=out_of_sample)
I even tried changing the out_of _sample value to less than 40 and then that sometimes would show an error of:
Bad initial energy, check any log probabilities that are inf or -inf,
nan or very small
any idea what might be causing this problem?
Workaround: Downgrade to PyMC3 v3.4.1
I tried running the tutorial that you indicated and also hit a similar error. Searching the GitHub repository, I saw someone reported a similar issue. Seems to be a problem with the recent versions of PyMC3 (3.5+). The suggested workaround is to downgrade to PyMC3 v3.4.1. Hopefully, someone eventually reworks the Pyfolio tutorial (the current version is from summer 2017).
Related
for some reason Google colab keeps on throwing errors everytime in code that has no errors,
an example is
import tensorflow as tf
print(tf.__version__)
scalar = tf.constant(7)
scalar
It throws an error for this which is TypeError: 'tensorflow.python.framework.ops.EagerTensor' object is not callable
I tried re-running it several times but that did not fix it, despite the code being correct. When I restarted and re-ran the code it worked but the code i had in my last line which was
matrix1 = tf.constant([[10, 7],
[7, 10]])
matrix1
does not work despite having the code
matrix = tf.constant([[10, 7],
[7, 10]])
matrix
which did work several lines above from this one.
I tried again to restart it and re-run the code but it still did not work.
I tried to update my google chrome because but my google chrome is at the latest version.
I made a new notebook and tried the code and it works! but I need to use the notebook I have because I have already written a lot of code there.
I also tried to update my operating system and now it is up to date. macOS Monterey 12.4 but it still did not fix anything.
The problem also occurs when i try to change a numpy array to a tensor
import numpy as np
numpy_A = np.arange(1, 25, dtype = np.int32) #create a Numpy arrary between 1 and 25
A = tf.constant(numpy_A, shape = (2, 3, 4))
and it is always TypeError: 'tensorflow.python.framework.ops.EagerTensor' object is not callable
that keeps getting thrown as the error.
and the code above is correct, I am following an online tutorial, and it worked and I kept checking my code over and over again but it is the same exact thing that was written. I am using the most up to date version of tensorflow as well which is 2.8.2
I have looked up various ways to fix this but there is nothing I have found.
I am using colab.research.google.com for this code. And I have to keep using colab for the online course, in order to take notes, and run several things individually and follow along with the instructor.
I have struggled with the problem "Perket" from Kattis:
https://open.kattis.com/problems/perket
I succeed locally when i manually write the samples, getting the correct output. I get "runtime error" when i try to submit to Kattis though. Does any of you see the mistake? Is my program too slow for the requirements? Thank you very much and have a nice day.
import math
n = int(input())
ingredients = []
for i in range(n):
new = [int(k) for k in input().split()]
ingredients.append(new)
bitter = [val[0] for val in ingredients]
sour = [val[1] for val in ingredients]
diffs = []
for i in range(len(bitter)):
for j in range(len(sour)):
diff = abs(math.prod(bitter[i:j+1]) - sum(sour[i:j+1]))
diffs.append(diff)
print(min(diffs))
I went ahead, and did a lot of black-box bisecting to debug the issue.
It turns out the validation breaks (with a Run Time Error) on math.prod.
math.prod was introduced in Python 3.8, so the website is likely using an older version of Python 3. You should try and create your own version of math.prod (or find another way to solve the problem).
Found the Python version, from https://open.kattis.com/help/python3 :
For Python 3, we use PyPy version Python 3.6.9 (7.3.1+dfsg-4, Apr 22 2020, 05:15:29) with the following flags: {files}.
For the record, CPython 3.6 has recently reached end of life (in December 2021). I'm not sure about the PyPy's version, but version 3.6 can't be downloaded from PyPy's website; only later versions.
I'm trying to convert a pandas dataframe into an R dataframe using the guide here. The code I have so far is:
import pandas as pd
import rpy2.robjects as ro
from rpy2.robjects import pandas2ri
from rpy2.robjects.conversion import localconverter
pd_df = pd.DataFrame({'int_values': [1, 2, 3],
'str_values': ['abc', 'def', 'ghi']})
with localconverter(ro.default_converter + pandas2ri.converter):
r_from_pd_df = ro.conversion.py2rpy(pd_df)
However, this is giving me the following error:
Traceback (most recent call last):
File <my_file_ref>, line 13, in <module>
r_from_pd_df = ro.conversion.py2rpy(pd_df)
AttributeError: module 'rpy2.robjects.conversion' has no attribute 'py2rpy'
I have found this relevant question where the OP refers to function names being changed however doesn't explain what the changes are. I've tried looking at the module but I'm not quite advanced enough to be able to make sense of it.
The accepted answer refers to checking versions which I've done and I'm definitely using rpy2 v3.3.3 which is the same as the guide I'm following.
Has anyone encountered this error and found a solution?
The section of the rpy2 documentation you are pointing out is built by running the code example. This is means that the example did work with the corresponding version of rpy2.
I am not sure you are using that version of rpy2 at runtime? For example, add print(rpy2.__version__) to check that this is the case.
For what is worth, the latest release in the rpy2 3.3.x series is 3.3.6 and there is probably no good reason to stay with 3.3.3. Otherwise rpy2 3.4.0 was just released; if using R 4.0 or greater, or the latest release of the R packages dplyr or ggplot2 together with their rpy2 wrapper, I would recommend to use that release.
[PS: I just tried your example with rpy2-3.4.0 and it runs without error]
I am trying to build a score matching using pymatch. Unfortunately I am getting the following error
Fitting Models on Balanced Samples: 1\200Error: Unable to coerce to Series, length must be 1: given 1898
Here is my code
from sklearn.datasets.samples_generator import make_blobs
from pymatch.Matcher import Matcher
import pandas as pd
import numpy as np
X, y = make_blobs(n_samples=5000, centers=2, n_features=2, cluster_std=3.5)
df = pd.DataFrame(dict(x=X[:,0], y=X[:,1], label=y))
df['population'] = np.random.choice([1, 0], size=len(df), p=[0.8, 0.2])
control = df[df.label == 1]
test = df[df.label == 0]
m = Matcher(test, control, yvar="population", exclude=['label'])
m.fit_scores(balance=True, nmodels=200)
if I ran this code I will get the error. I am quite sure that I was able to run this before, but after changing some versions, this doesn't work anymore. Unfortunately I wasn't able to fix it by going back to previous versions, so not sure what's going on here...
Downgrading pandas did not work for me, but I found where the problem is.
It is an error in the method _scores_to_accuracy() of Matcher.py. I downloaded the source file, edited the function on my local machine, and now it works fine.
https://github.com/benmiroglio/pymatch/issues/23
Please downgrade your pandas, to version 0.23.4.
Use the code:
pip install pandas==0.23.4
I am trying to start using the AR models in statsmodels. However, I seem to be doing something wrong. Consider the following example, which fails:
from statsmodels.tsa.ar_model import AR
import numpy as np
signal = np.ones(20)
ar_mod = AR(signal)
ar_res = ar_mod.fit(4)
ar_res.predict(4, 60)
I think this should just continue the (trivial) time series consisting of ones. However, in this case it seems to return not enough parameters. len(ar_res.params) equals 4, while it should be 5. In the following example it works:
signal = np.ones(20)
signal[range(0, 20, 2)] = -1
ar_mod = AR(signal)
ar_res = ar_mod.fit(4)
ar_res.predict(4, 60)
I have the feeling that this could be a bug but I am not sure as I have no experience using the package. Maybe someone with more experience can help me...
EDIT: I have reported the issue here.
It works after adding a bit of noise, for example
signal = np.ones(20) + 1e-6 * np.random.randn(20)
My guess is that the constant is not added properly because of perfect collinearity with the signal.
You should open an issue to handle this corner case better. https://github.com/statsmodels/statsmodels/issues
My guess is also that the parameters are not identified in this case, so there might not be any good solution.
(Parameters not identified means that several parameter combinations can produce exactly the same fit, but I think they should all produce the same predictions in this case.)