if 𝑓(𝑥)=sin(𝑥) . I want to find 𝑑𝑓(𝑥)𝑑𝑥 and plot f(x) using the backwards function - plot

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

Related

Getting a list of points (in x, y form) in a Julia graph

If I have a Julia plot with any number of points, would it be possible for me to get a list of all of the data points within the graph (using the Plots library)?
EDIT: I am working with GeoStats.jl to create temporal variograms, and I just wanted to calculate the error (using RMSE and MAE) of the model's fit. To do this, I thought I had to compare the points within model's curve with the original semivariogram. The current code I have running is:
using GeoStats, Plots, DataFrames, CSV, Dates, MLJ
data_frame = CSV.read("C:/Users/VSCode/MINTS-Variograms/data/MINTS_001e06373996_IPS7100_2022_01_02.csv", DataFrame)
ms = [parse(Float64,x[20:26]) for x in data_frame[!,:dateTime]]
ms = string.(round.(ms,digits = 3)*1000)
ms = chop.(ms,tail= 2)
data_frame.dateTime = chop.(data_frame.dateTime,tail= 6)
data_frame.dateTime = data_frame.dateTime.* ms
data_frame.dateTime = DateTime.(data_frame.dateTime,"yyyy-mm-dd HH:MM:SS.sss")
ls_index = findall(x-> Millisecond(500)<x<Millisecond(1500), diff(data_frame.dateTime))
df = data_frame[ls_index, :]
#include calculation for average lag
#initialize georef data
𝒟 = georef((Z=df.pm2_5, ))
#empirical variogram - same thing as semivariogram
g = EmpiricalVariogram(𝒟, :Z, maxlag=300.)
plot(g, label = "")
γ = fit(Variogram, g)
plot!(γ, label = "")
hline!([γ.nugget], label = "")
hline!([γ.sill], label = "")
println("nugget: " * string(γ.nugget))
println("sill: " * string(γ.sill))

How to add weightings in pspline:: smooth.Pspline R equivalent of MATLAB's csaps()

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!

How to calculate y value on a Graph with known x value

I have not needed to do things like this for years and was also never good at it. Below is my graph :
Looking at my artistic numbering on the graph :
I have the X and Y Values : X = 7282, Y = 235
I have the X and Y values : X = 8178, Y = 173
I have the X but not the Y : X = 7882, Y = ?
I need to calculate Y, and im sure it is pretty simple, but I cant seem to figure it out. Ive googled a lot, but all my calculations never work(i.e the new Y point is never on the line, always above or below), so im clearly missing something.
Can anyone help with the formula of how to calculate the new Y value ?
Thanks!
Try this:
Y = (Ymax - Ymin)/(Xmax - Xmin) * K + Ymin;
If slope goes up: K = X - Xmin, another way K = Xmax - X.
Since you know the coordinate values of 2 points
m = ((y2 - y1)/(x2 - x1))
y3 = m*x3 + c
where c is a constant
to calculate c use x = x1 and y = y1
c = y-m*x
apply this c in the above equation we get y3
so for this case
m = (235 - 173)/(7282 - 8178)
c = 8178 - m*173
y3 = m*7882 + c

From numerical pdf to numerical CDF

I've estimated a pdf numerically at a set of grid points, and I would like to determine the CDF at this point.
Is there a function to integrate the pdf numerically? Is the function cumsum() enough? When I use cumsum() the pdf sums to 1.05 instead of 1.
I want to compare two densities over a fine grid using K-L divergence to see if they are the same. I observe the vector Y, which depends on the matrix X (also observed), and I'm interested in g(y), the unconditional distribution of Y. In order to get g(y), I first find the joint density, g(X, Y), and then I divide it by the conditional density g(X | Y). The two densities are determined using the rule-of-thumb method in the np package in R (npundens, npudensbw, npcdens, npcdensbw). So, g(y) = g(X, Y) / g(X | Y). However, when I sum the entries in g(y), I get a number larger than 2. Since I want g(y) to be evaluated on a fine grid I compute the kernel regression of g(y) (using npreg with exdat = grid) and save the fitted value from this regression. When I sum them I get a number around than 1.05.
Is this method correct to find the unconditional pdf? Why doesn't the pdf sum to 1?
Method:
# Compute the joint pdf of Y and X
Z <- data.frame(Y, X)
jbw <- npudensbw(dat = Z, bwmethod = 'normal-reference',
xtrim = trim, ytrim = trim)
jpdf <- npudens(bws = jbw)
# Determine the conditional pdf of the covariates X with respect to the vector Y
bw <- npcdensbw(xdat = Y, ydat = X,
bwmethod = 'normal-reference', xtrim = trim, ytrim = trim)
cpdf <- npcdens(bws = bw, xdat = Y, ydat = X)
# Determine the unconditional pdf of Y
ft = jpdf$dens / cpdf$condens
print(sum(ft))
# Nonparametric regression of w_ft over Y in order
to get the extended pdf evaluated in the points in grid
sigma = min(sd(Y, na.rm = TRUE),
mad(Y, center = median(Y), constant = 1.4826,
na.rm = TRUE, low = FALSE, high = FALSE) / 1.4826,
IQR(Y, na.rm = TRUE, type = 7)/1.349)
computed_bw = 1.06 * sigma * length(Y)^(-1.0/(2.0*2+1))
extended_pdf <- fitted(npreg(bws = computed_bw, tydat = ft, txdat = Y, exdat = grid))

predict points on grid over time?

I have a, hopefully, simple question. Im using Nuke to do a linear animation and I have 2 points.
point1 # frame 1 is (5,90)
point2 # frame 10 is (346,204)
Using a linear interpolation type, I want to fiqure out where the x and y point is at frame 30.
The way i tried is using the slope formula and then finding the y intercept.
m = (204 - 90) / (346 - 5)
m = 114/341 = .3343
then I got the intercept by:
Y = Mx + b
90 = .3343(5) + b
90 = 1.6715 + b
88.3285 = b
so...I got the formula for my line. y = .3343X + 88.3285
Can someone help me figure out where the point is going to be at any given frame?
If you'd please refer to the image attached... you can see image of my graph.
I guess the problem I'm having is relating the time to the coord points.
Thanks
Just consider x as a function of time (t).
Here's some coordinates:
(t, x)
(1, 5)
(10, 346)
and some calculation of the line equation:
x = mt+b
m = (346-5) / (10-1)
m = 341/9
b = 5 - (341/9)*1
b = - 296/9
x = (341t - 296)/9
And using my formula (t -> x) and your formula (x -> y), I can calculate where things are at t=30
t = 30
x = 1103 + 7/9
y = 457.3214

Resources