VaR and Expected shortfall with Garch e VT-ARMA - var

I am conducting an analysis using the garch model and the VT-ARMA model with R's rugarch and tscopula package respectively.
Now with the quantile function I have calculated the daily VaR via both VT-ARMA and garch. Now I would like to calculate the expectedshortfall of both VaRs. With the VT-ARMA model I have no problem(in the last plot you can see it in yellow) while with the ES code for garch it returns NaN or gives me a vector of constants, as if calculating the expectedshortfall a single time and not daily. How could I do this? Below is a picture of the code.
Red: Var VT-ARMA Black: VaR garch Yellow: ES with VT-ARMA

Related

Numpy Median Value Calculated not represented on BarPlot, How can I represent values according

Hi and thank you for visiting my post.
Here is working code that produces the median values
Wall_Median = pd.pivot_table(cleaned_pokedex, values="Wall", index ='Primary Type',aggfunc={"Wall": np.median})
Final_Wall_Median = Wall_Median.nlargest(18,'Wall')
print(Final_Wall_Median)
E.g Poison is 193 and the bar chart shows over 200
1. Wall Primary Type
Steel 259.0
Fairy 244.0
Dragon 237.0
Rock 235.5
Ground 235.0
Ice 230.0
Flying 220.0
Fighting 216.0
Ghost 215.0
Psychic 215.0
Grass 209.5
Water 208.0
Fire 204.0
Electric 201.0
Dark 200.0
Normal 194.0
Poison 193.0
Bug 180.0
Plotting the values using a seaborn bar chart does not produce the numeric value I receive from the code
fig = plt.gcf()
fig.set_size_inches(20,18)
ax = sns.barplot(x= cleaned_pokedex["Wall"],y= cleaned_pokedex["Primary Type"],data= Final_Wall_Median,palette = pkmn_type_colors)
Output
The bar values don't represent the medians printed. What can I do to fix this ?
It seems that you are actually plotting the mean with a CI band instead of the median as you intend to. That is because there is a small contradiction in your code:
ax = sns.barplot(x= cleaned_pokedex["Wall"],y= cleaned_pokedex["Primary Type"],data= Final_Wall_Median,palette = pkmn_type_colors)
you are telling seaborn to get the x and y values from cleaned_pokedex dataframe,
however, then you tell it to use data from the Final_Wall_Median dataframe.
So it seems that seaborn is arbitrarily choosing to use your y~x provided data, instead of the pre-aggregated Final_Wall_Median that you pass into data. Typically, you would use only x and y attributes if you just want to pass any two arrays (they don't need to be from the same dataframe), OR you can profile data as the dataframe you can't to use, and x and y as string column names (e.g. (x="Wall", y="Primary Type", data=cleaned_pokedex))
However, as pointed out, if you simply pass the "Wall", "Primary Type" dimensions into the x and y values of a barplot, seaborn will by default use the "mean" as the estimator.
The two options you have are:
sns.barplot(x=cleaned_pokedex["Wall"], y=cleaned_pokedex["Primary Type"], estimator=np.median)
# or
sns.barplot(x=Final_Wall_Median.Wall, y=Final_Wall_Median.index)
Since you've already pre-aggregated the medians, you can use Final_Wall_Median directly. The only difference is that you cannot get CI bands if you don't supply the raw data (the whole cleaned_pokedex dataframe, as in the first option).
barplot() takes a parameter estimator= that defines how the bar height is calculated. By default, this is done using mean(), but you can pass median if that's what you want:
ax = sns.barplot(..., estimator=np.median)

Flopy MF6 Plotting specific discharge in cross-section view

How to plot discharge vectors in cross-section view in Flopy MF6?
I know this plots for plan view:
quiver = mapview.plot_specific_discharge(spdis[0])
I tried to get specific discharge using the following code but got error:
AttributeError: module 'flopy.utils.postprocessing' has no attribute 'get_specific_discharge'
Code:
hds_file = os.path.join(workspace, headfile)
hds = flopy.utils.binaryfile.HeadFile(hds_file)
cbb_file = os.path.join(workspace, budgetfile)
cbb = flopy.utils.CellBudgetFile(fname, precision='double')
q = flopy.utils.postprocessing.get_specific_discharge(gwf,cbb_file, precision='single', kstpkper=(0,1),
hdsfile=hds_file, position='centers')
For me (using mf6) plotting specific discharge on cross sections works like this:
Reading the cbc output:
cbcdobj = flopy.utils.binaryfile.CellBudgetFile(path, precision='double', verbose=True)
SPDIS = cbcdobj.get_data(kstpkper=kstpkper, text='DATA-SPDIS')[0]
You might need to use 'verbose=False' and 'precision=single' when using mf2005.
Then the cross section:
cros_mp=flopy.plot.PlotCrossSection(model=gwf, line={'row': 200})
cros_mp.plot_specific_discharge(SPDIS)
Remark that plotting specific discharges on an irregular cross section ('line', not 'row' nor 'column') is not possible.

python pdpbox plot with unscaled feature values

I'm trying to plot the Partial Dependence and ICE plots for a Multi-layer perceptron classifier. I'm using the UCI Adult dataset. I have Label Encoded the categorical features and Scaled the overall dataframe and then performed a test-train split on the scaled dataframe.
Now when I'm trying to plot the PDP and ICE plots I get the Age values (column in the X Axis of the plot) as scaled and hence not comprehend-able. I want the age values to be the original values before scaling was performed on the data. How can I achieve this?
This is the code for the plots:
from pdpbox import pdp, info_plots
pdp_age = pdp.pdp_isolate(model=mlp, dataset=X_train, model_features=X_train.columns, feature='Age')
#PDP Plot
fig, axes = pdp.pdp_plot(pdp_age, 'Age', plot_lines=False, center=False, frac_to_plot=0.5, plot_pts_dist=True,x_quantile=True, show_percentile=True)
#ICE Plot
fig, axes = pdp.pdp_plot(pdp_age, 'Age', plot_lines=True, center=False, frac_to_plot=0.5, plot_pts_dist=True,x_quantile=True, show_percentile=True)
You can see from the plot that the Age values cannot be comprehended, I want the Age values to be in their true form. How can I do this ?
I was able to solve the above problem by using a Pipeline object. I used one hot encoding for the categorical variables and then pushed the scaling and the classifier operations into the Pipeline object. I was then able to use the encoded X_train without any issues for the partial dependence plot and I got the actual Age value ranges, what I was looking for.

How to add residuals values in my data frame?

I've residuals value in my model result. I would like to transform and add them in my data frame.That's means, to have results with low residuals values. I'm using FE model. So, i've tried to add but my method doesn't work. Here my R details:
fixed = plm(sp ~lag(debt)+lag(I(debt^2))+outgp++bcour+gvex+vlexp+vlimp, data=bdata, index=c("country", "year"), model="within")
method for adding my residuals values:
bdata$resid = fixed$resid
thanks for your hand

Carrying out multiple piecewise regressions with variables from same dataframe (but varying columnpair lengths)

I'm trying to analyse and plot piecewise regressions for daily temperature and gas use. I have six columns (two corresponding to each year) within a csv which I am pulling in using pandas then defining each column as a seperate variable.
I found one of the answers on How to apply piecewise linear fit in Python? extremely helpful and was able to use the following code to run a breakpoint analysis and also plot a graph:
import matplotlib.pyplot as plt
import pwlf
# Importing the csv and defining columns as variables
df = pd.read_csv(PATH)
Y_A = df.Column1
X_A = df.Column2
Y_B = df.Column3
X_B = df.Column4
# Analysing breakpoints
my_pwlf_a = pwlf.PiecewiseLinFit(X_A, Y_A)
breaks_a = my_pwlf_a.fit(2)
print(breaks_a)
# Graphing
x_hat = np.linspace(X_A.min(), X_A.max(), 100)
y_hat = my_pwlf.predict(x_hat)
plt.figure()
plt.plot(X_A, Y_A, 'o')
plt.plot(x_hat, y_hat, '-')
plt.xlabel('X'); plt.ylabel('Y');
plt.show()
This runs with no problems and gives the results the desired.
When I try to repurpose the code using my next pair of variables (Y_B and X_B) I run into problems:
my_pwlf_b = pwlf.PiecewiseLinFit(X_B, Y_B)
breaks_b = my_pwlf_b.fit(2)
print(breaks_b)
The error returned is:
ValueError: bounds should be a sequence containing real valued (min, max) pairs for each value in x
All variables are float64 and each column contains 366 rows. Thanks for any help in spotting what I'm missing!
Thansk to Zionsof for the nudge back towards the data!
Further testing shows that unequal lengths of the column pairings was the problem (e.g. Columns 1 & 2 contained 366 while Columns 3 & 4 contained 365). I had foolishly thought that seperating the columns into seperate variables may fix this but I was incorrect. Here is what I used to fix it (numpy.isfinite):
# Remove any blanks by ensuring the values are finite
Y_A = df.Column1[np.isfinite(df['Column1'])]
X_A = df.Column2[np.isfinite(df['Column2'])]
Y_B = df.Column3[np.isfinite(df['Column3'])]
X_B = df.Column4[np.isfinite(df['Column4'])]

Resources