So I'm trying to plot this function using PyPlot in julia
here's what I did:
using PyPlot
theta0 = 160
theta = range(0,stop=180,length = 50)
m1 = 4
m2 = range(1,stop = 200,length = 50)
d = pi/180
function km(m,th)
((sqrt((m/m1)^2-sin(th*d)^2)+cos(th*d))/(1+(m)))^2
end
PyPlot.plot(m2,km(m2,theta0))
and I get this error
I can't find anything wrong, please help.
Related
x = np.linspace(-np.pi, np.pi, 100)
x = torch.tensor(x, requires_grad=True)
y = torch.sin(x)
This is the given problem. Trying to find its derivative and plot the function
I am ask to sample and then do a marginal distribution $f(x_1,x_5)$ and plot it. I have the following code which works but dnorm is used for one dimension so I was wondering if i need to change it to dmvnorm.
If so, i change mu=mu.marginal, sigma=sigma.marginal, added a y sample, but dmvnorm says error because of non array input. Anye help is appreciated.
Model of multivariable normal:
mu = c(1,2,6,2,-4)
sigma = c(1,2,1,0.5,2)
rho = diag(rep(1,5))
rho[1,2] = rho[2,1] = 0.4
rho[1,3] = rho[3,1] = -0.3
rho[1,4] = rho[4,1] = -0.7
rho[3,5] = rho[5,3] = 0.2
rho[4,5] = rho[5,4] = 0.5
Sigma = rho * (sigma %o% sigma)
my code:
p = c(1,5)
(mu.marginal = mu[p])
(Sigma.marginal = Sigma[p,p])
# p is one-dimensional: use dnorm() to compute marginal pdf
x = seq(-1,6,by=0.01)
fx = dnorm(x,mean=mu.marginal,sd=sqrt(Sigma.marginal))
ggplot(data=data.frame(x=x,y=fx),mapping=aes(x=x,y=y)) + geom_line(col="blue")
It seems to me you were on the right track with mvtnorm and came close to a solution... I'm not sure how you ran into a non-array input error, but here's what I got with using mvtnorm:
set.seed(123)
dat <- mvtnorm::rmvnorm(1e4, mean = mu.marginal, sigma = Sigma.marginal)
dat <- as.data.frame(dat)
ggplot(dat, aes(x = V1, y = V2)) +
geom_bin2d()
You can see it's fairly spherical, which is what you'd expect since the off-diagonal elements of Sigma.marginal are 0 (which means that x_1 and x_5 are marginally independently normally distributed...)
In Julia, I've managed to get a boxplot with the following minimal working code:
using Plots
using DataFrames
function boxplot_smaa_similarity(arr_nb_alternative::Vector{Int},
arr_nb_montecarlo::Vector{Int},
nb_criteria::Int, nb_simulations::Int)
# Create a fill dataframe
df = DataFrame(NbAlternative = Int[], NbMonteCarlo = Int[], Similarity = Float64[])
for na in arr_nb_alternative
#show na
for mt in arr_nb_montecarlo
println()
println("...$mt")
append!(df, (NbAlternative=ones(Int, nb_simulations)*na,
NbMonteCarlo=ones(Int, nb_simulations)*mt,
Similarity=rand(Float64, nb_simulations)))
end
end
# Boxplot dataframe data
p = Plots.boxplot(df[:NbMonteCarlo],
df[:Similarity],
group = df[:NbAlternative],
ylims = (0.0, 1.1),
xlabel ="Nb Simulations Monte Carlo",
ylabel = "Similarity",
dpi = 500)
# Save figure to path, do not hesitate to change path if necessary
Plots.savefig("../output/plot_compare_SMAA-TRI-AD_crit$(nb_criteria)"*
"_nb_alternative_$(arr_nb_alternative[1])-$(arr_nb_alternative[end])"*
"_nb_MC$(arr_nb_montecarlo[1])-$(arr_nb_montecarlo[end]).png")
return p
end
boxplot_smaa_similarity([50,100,150], [2,4,6,8,10], 5, 10)
However, the result is not good to me as the three boxplots are overlapping. Is there a fix with Plots.jl or should I move to PyPlot or another Julia librairy?
Felipe's comment is correct - you should use StatsPlots.jl, which has all the statistical recipes for Plots.jl. There's a groupedboxplot recipe which seems not to be in the readme
a = rand(1:5, 100)
b = rand(1:5, 100)
c = randn(100)
using StatsPlots
groupedboxplot(a, c, group = b, bar_width = 0.8)
I am trying to replicate Matlabs csaps function in r. I have successfully achieved this with this question.
However problems arise when I try to change the weightings in both cases. In MATLAB the weightings are based on the error measure however I am unsure what the w values correspond to in pspline function in r and it gives me a different result
https://uk.mathworks.com/help/curvefit/csaps.html
Here is some example code:
----------------IN MATLAB------------------
% Run spline with weights = 1
Axis = 1:100;
Yval = sin((1:100)/20 *(2*pi));
weights = ones(size(Yval));
p = 0.0005;
pp = csaps(Axis, Yval, p, [], weights);
smoothed = fnval(pp, Axis)';
% Run spline with some select weights = 0
weights([5,8,10,15,17,19,24,...
26,28,33,36,40,44,46,...
49,50,55,60,64,68,74,...
79,81,85,88,93,99,100])=0
pp2 = csaps(Axis, Yval, p, [], weights);
smoothed2 = fnval(pp2, Axis)';
plot(Axis,Yval)
hold on
plot(Axis,smoothed)
plot(Axis,smoothed2)
----------------IN R------------------
% Run spline with weights = 1
Axis <- 1:100
Yval <- sin((1:100)/20 *(2*pi))
weights <- rep(1,length(Yval))
p <- 0.0005
pp <- pspline::smooth.Pspline(
x = Axis, y =Yval, w = weights, spar = (1-p) / p )
smoothed <- c(predict(pp,Axis))
% Run spline with some select weights = 0
weights[c(5,8,10,15,17,19,24,
26,28,33,36,40,44,46,
49,50,55,60,64,68,74,
79,81,85,88,93,99,100)] <- 0.00000000000000001
pp2 <- pspline::smooth.Pspline(
x = Axis, y =Yval, w = weights, spar = (1-p) / p )
smoothed2 <- c(predict(pp2,Axis))
plot(Yval,t='l')
lines(smoothed,col = 2)
lines(smoothed2,col = 3)
The results in R and Matlab are identical (within rounding) without weightings, but when weights are introduced the results now differ. Both are running FORTRAN spline functions under the hood. I cant work out how to run pspline in R to get the same results with these weightings.
Thanks in advance!
Is there an auto-thresholding algorithm such as 'Otsu' available for R raster object. I have tried using the "authothresholder" package, however it is inefficient as it works on matrix and doesn't work with 32 bit tif files. I am trying to convert an NDWI image into a binary layer.
This is implemented in the EBImage package available from Bioconductor. Here is an example use:
library(EBImage)
img <- readImage(system.file("images", "sample.png", package = "EBImage"))
thr <- img > otsu(img)
display(img)
display(thr)
The implementation is essentially the following (pulled from the function definition of EBImage::otsu i.e. not my work), so you should be able to adapt the following for whatever image analysis toolset you are using:
img # assuming img is a numeric matrix or vector
range = c(0, 1) # assuming values in the matrix range from 0 to 1
levels = 256L
breaks = seq(range[1], range[2], length.out = levels + 1)
h = hist.default(img, breaks = breaks, plot = FALSE)
counts = as.double(h$counts)
mids = as.double(h$mids)
len = length(counts)
w1 = cumsum(counts)
w2 = w1[len] + counts - w1
cm = counts * mids
m1 = cumsum(cm)
m2 = m1[len] + cm - m1
var = w1 * w2 * (m2/w2 - m1/w1)^2
maxi = which(var == max(var, na.rm = TRUE))
(mids[maxi[1]] + mids[maxi[length(maxi)]])/2